UserList.cpp
上传用户:hysujiao87
上传日期:2007-12-02
资源大小:156k
文件大小:6k
- // UserRecord.cpp: implementation of the CUserList class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "UserList.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- IMPLEMENT_DYNCREATE(CUserData, CObject)
- IMPLEMENT_DYNCREATE(CUserList, CObject)
- CUserList::CUserList()
- {
- }
- CUserList::~CUserList()
- {
- CSingleLock lock(&_mutex, TRUE);
- _ASSERTE(lock.IsLocked() == TRUE);
- if (lock.IsLocked() != TRUE)
- return ;
- removeAll();
- }
- HRESULT CUserList::authenticate(LPCTSTR userID, LPCTSTR password)
- {
- _ASSERTE(userID != NULL && password != NULL);
- if (userID == NULL || password == NULL)
- return E_INVALIDARG;
- CSingleLock lock(&_mutex, TRUE);
- _ASSERTE(lock.IsLocked() == TRUE);
- if (lock.IsLocked() != TRUE)
- return E_FAIL;
- CUserData* userData = getUserByID(userID);
- if (userData == NULL)
- return E_FAIL;
-
- if (userData->_password == password)
- return S_OK;
-
- return E_FAIL;
- }
- CUserData* CUserList::getUserByID(LPCTSTR userID)
- {
- CSingleLock lock(&_mutex, TRUE);
- _ASSERTE(lock.IsLocked() == TRUE);
- if (lock.IsLocked() != TRUE)
- return NULL;
- for(int i = 0; i < _userList.GetCount(); i++)
- {
- CUserData *userData = getUserByIndex(i);
- _ASSERTE(userData != NULL);
- if (userData == NULL)
- continue ;
- if (userData->_clientID == userID)
- return userData;
- }
- return NULL;
- }
- CUserData* CUserList::getUserByIndex(INT_PTR index)
- {
- CSingleLock lock(&_mutex, TRUE);
- _ASSERTE(lock.IsLocked() == TRUE);
- if (lock.IsLocked() != TRUE)
- return NULL;
- if(index < 0 || index >= _userList.GetCount())
- return NULL;
- return _userList.GetAt(_userList.FindIndex(index));
- }
- void CUserList::removeAll()
- {
- CSingleLock lock(&_mutex, TRUE);
- _ASSERTE(lock.IsLocked() == TRUE);
- if (lock.IsLocked() != TRUE)
- return ;
- for(int i = 0; i < _userList.GetCount(); i++)
- {
- CUserData* pUserData = getUserByIndex(i);
- ASSERT(pUserData != NULL);
- delete pUserData;
- }
- _userList.RemoveAll();
- }
- void CUserList::addUser(CUserData* userData)
- {
- CSingleLock lock(&_mutex, TRUE);
- _ASSERTE(lock.IsLocked() == TRUE);
- if (lock.IsLocked() != TRUE)
- return ;
- CUserData *oldData = getUserByID(userData->_clientID);
- if(oldData != NULL)
- {
- *oldData = *userData;
- }
- else
- {
- CUserData *newData = new CUserData;
- *newData = *userData;
- _userList.AddTail(newData);
- }
- _mutex.Unlock();
- }
- void CUserList::Serialize(CArchive &ar)
- {
- CSingleLock lock(&_mutex, TRUE);
- _ASSERTE(lock.IsLocked() == TRUE);
- if (lock.IsLocked() != TRUE)
- return ;
- if( ar.IsStoring() )
- {
- INT_PTR count = _userList.GetCount();
- ar.Write(&count, sizeof(INT_PTR));
- for (INT_PTR i = 0; i < _userList.GetCount(); i++)
- {
- CUserData *userData = getUserByIndex(i);
- _ASSERTE(userData != NULL);
- if (userData == NULL)
- return ;
- userData->Serialize(ar);
- }
- }
- else
- {
- INT_PTR count = 0;
- ar.Read(&count, sizeof(INT_PTR));
- for(INT_PTR i = 0; i < count; i++)
- {
- CUserData *userData = new CUserData;
- _ASSERTE(userData != NULL);
- if (userData == NULL)
- return ;
-
- userData->Serialize(ar);
- _userList.AddTail(userData);
- }
- }
- }
- CUserData::CUserData()
- {
- _online = FALSE;
- _lastReport = 0;
- ::ZeroMemory(&_sockAddr, sizeof(sockaddr_in));
- }
- CUserData::~CUserData()
- {
- clear();
- }
- void CUserData::clear()
- {
- _clientID.Empty();
- _password.Empty();
- _nickname.Empty();
- _online = FALSE;
- _lastReport = 0;
- ::ZeroMemory(&_sockAddr, sizeof(sockaddr_in));
- FriendIterator begin = _friendList.begin();
- FriendIterator end = _friendList.end();
- for (FriendIterator it = begin; it != end; it++)
- {
- delete (*it);
- }
- _friendList.clear();
- }
- void CUserData::Serialize(CArchive &ar)
- {
- if( ar.IsStoring() )
- {
- ar << _clientID;
- ar << _password;
- ar << _nickname;
- ar << _friendList.size();
- FriendIterator begin = _friendList.begin();
- FriendIterator end = _friendList.end();
- for (FriendIterator it = begin; it != end; it++)
- {
- ar << (*it)->userID;
- }
- }
- else
- {
- ar >> _clientID;
- ar >> _password;
- ar >> _nickname;
-
- int size = 0;
- ar >> size;
- for (int i = 0; i < size; i++)
- {
- FRIEND_DATA *friendData = new FRIEND_DATA;
- _ASSERTE(friendData != NULL);
- if (friendData == NULL)
- return ;
- ar >> friendData->userID;
- _friendList.push_back(friendData);
- }
- }
- }
- void CUserData::operator = (CUserData &data)
- {
- clear();
- _clientID = data._clientID;
- _password = data._password;
- _nickname = data._nickname;
- _online = data._online;
- _lastReport = data._lastReport;
- memcpy(&_sockAddr, &data._sockAddr, sizeof(sockaddr_in));
- FriendIterator begin = data._friendList.begin();
- FriendIterator end = data._friendList.end();
- for (FriendIterator it = begin; it != end; it++)
- {
- FRIEND_DATA *friendData = new FRIEND_DATA;
- if (friendData == NULL)
- return ;
- friendData->userID = (*it)->userID;
- _friendList.push_back(friendData);
- }
- }
- HRESULT CUserData::addFriend(LPCTSTR friendID)
- {
- FriendIterator begin = _friendList.begin();
- FriendIterator end = _friendList.end();
- for (FriendIterator it = begin; it != end; it++)
- {
- if ((*it)->userID == friendID)
- {
- return S_FALSE;
- }
- }
- FRIEND_DATA *friendData = new FRIEND_DATA;
- if (friendData == NULL)
- return ERROR_NOT_ENOUGH_MEMORY;
- friendData->userID = friendID;
- _friendList.push_back(friendData);
- return S_OK;
- }