www.pudn.com > QQ2004_Source_0.01.rar > Communication.cpp
#include "stdafx.h" #include "communication.h" #include "xmlparser.h" #include "QQSocket.h" #includeusing namespace std; CCommunication::CCommunication(void) { _created = FALSE; _connected = FALSE; ::ZeroMemory(&_addrServer, sizeof(sockaddr_in)); } CCommunication::~CCommunication(void) { } HRESULT CCommunication::create(void) { if (_created) return S_FALSE; if (_sock.Create(0, SOCK_STREAM) == FALSE) { TRACE("ERROR: Can't create the client socket.\n"); return E_FAIL; } _created = TRUE; return S_OK; } HRESULT CCommunication::connect(LPCTSTR serverIP, short port/*=6000*/) { _ASSERTE(serverIP != NULL); if (serverIP == NULL) return E_INVALIDARG; CQQSocket::stringToIPAddr(serverIP, _addrServer); _addrServer.sin_port = htons(port); _ASSERTE(_connected == FALSE); if (_connected) return S_FALSE; if (_sock.Connect((SOCKADDR*)&_addrServer, sizeof(sockaddr_in)) == FALSE) { DWORD error = _sock.GetLastError(); TRACE("ERROR: Can't connect to the server (%ld).\n", error); return E_FAIL; } _connected = TRUE; return S_OK; } HRESULT CCommunication::init(LPCTSTR serverIP, short port/*=6000*/) { if (create() != S_OK) { AfxMessageBox(_T("错误:无法创建Socket.")); return E_FAIL; } if (connect(serverIP) != S_OK) { AfxMessageBox(_T("错误:无法连接服务器")); return E_FAIL; } return S_OK; } HRESULT CCommunication::sendLogonRequest(LPCTSTR userID, LPCTSTR password) { CXMLParser parser; CElement *root = parser.createElement(_T("REQUEST"), _T("")); root->AddChild(_T("type"), _T("LOGON")); root->AddChild(_T("userID"), userID); root->AddChild(_T("password"), password); parser.set_root(root); if (parser.BuildXML() != 0) return E_FAIL; CString sendOut = parser.get_xml(); int ret = _sock.Send((BYTE*)((LPCTSTR)sendOut), sendOut.GetLength()); if (ret == SOCKET_ERROR) { AfxMessageBox("发送请求失败."); return E_FAIL; } return S_OK; } HRESULT CCommunication::sendLogoffRequest(LPCTSTR userID) { CXMLParser parser; CElement *root = parser.createElement(_T("REQUEST"), _T("")); root->AddChild(_T("type"), _T("LOGOFF")); root->AddChild(_T("userID"), userID); parser.set_root(root); if (parser.BuildXML() != 0) return E_FAIL; CString sendOut = parser.get_xml(); int ret = _sock.Send((BYTE*)((LPCTSTR)sendOut), sendOut.GetLength()); if (ret == SOCKET_ERROR) { DWORD error = _sock.GetLastError(); TRACE("ERROR: Failed to send the LOGOFF request (%ld).", error); return E_FAIL; } return S_OK; } HRESULT CCommunication::sendRegisterRequest(LPCTSTR userID, LPCTSTR password, LPCTSTR nickname) { CXMLParser parser; CElement *root = parser.createElement(_T("REQUEST"), _T("")); root->AddChild(_T("type"), _T("REGISTER")); root->AddChild(_T("userID"), userID); root->AddChild(_T("password"), password); root->AddChild(_T("nickname"), nickname); parser.set_root(root); if (parser.BuildXML() != 0) return E_FAIL; CString sendOut = parser.get_xml(); int ret = _sock.Send((BYTE*)((LPCTSTR)sendOut), sendOut.GetLength()); if (ret == SOCKET_ERROR) { AfxMessageBox("发送请求失败."); return E_FAIL; } return S_OK; } HRESULT CCommunication::sendAddFriendRequest(LPCTSTR userID, LPCTSTR friendID) { CXMLParser parser; CElement *root = parser.createElement(_T("REQUEST"), _T("")); root->AddChild(_T("type"), _T("ADDFRIEND")); root->AddChild(_T("userID"), userID); root->AddChild(_T("friendID"), friendID); parser.set_root(root); if (parser.BuildXML() != 0) return E_FAIL; CString sendOut = parser.get_xml(); int ret = _sock.Send((BYTE*)((LPCTSTR)sendOut), sendOut.GetLength()); if (ret == SOCKET_ERROR) { AfxMessageBox("发送请求失败."); return E_FAIL; } return S_OK; } HRESULT CCommunication::sendDownloadFriendsRequest(LPCTSTR userID) { CXMLParser parser; CElement *root = parser.createElement(_T("REQUEST"), _T("")); root->AddChild(_T("type"), _T("DOWNLOADFRIENDS")); root->AddChild(_T("userID"), userID); parser.set_root(root); if (parser.BuildXML() != 0) return E_FAIL; CString sendOut = parser.get_xml(); int ret = _sock.Send((BYTE*)((LPCTSTR)sendOut), sendOut.GetLength()); if (ret == SOCKET_ERROR) { AfxMessageBox("发送请求失败."); return E_FAIL; } return S_OK; } HRESULT CCommunication::receiveResponse(CXMLParser &parser, DWORD timeOut, BOOL popupMessage) { DWORD lastTime = ::GetTickCount(); DWORD dataInBuffer = 0; while(dataInBuffer <= 0) { BOOL ret = _sock.IOCtl(FIONREAD, &dataInBuffer); if (ret != TRUE) { DWORD error = _sock.GetLastError(); TRACE("IOCtl() failed (%d)\n", error); return E_FAIL; } if (::GetTickCount() - lastTime > timeOut) { AfxMessageBox(_T("错误:连接服务器超时.")); return E_FAIL; } } auto_ptr receiveBuffer(new BYTE[dataInBuffer]); int actualReceived = _sock.Receive(receiveBuffer.get(), dataInBuffer); if (actualReceived == SOCKET_ERROR) { DWORD error = _sock.GetLastError(); CString msg; msg.Format("错误:无法连接服务器(%ld)", error); AfxMessageBox(msg); return E_FAIL ; } CString result; CElement *root = NULL; if (parser.LoadXML(receiveBuffer.get(), actualReceived) == 0) { root = parser.get_root(); _ASSERTE(root != NULL); if (root != NULL) { result = root->getChildContent(_T("result")); } } if (result.IsEmpty()) { AfxMessageBox(_T("错误:无法解析服务器响应.")); return E_FAIL; } int resultVal = _ttoi(result); if (resultVal != 0) { if (popupMessage == TRUE) { CString msg = root->getChildContent(_T("message")); CString error; if (msg.IsEmpty() == FALSE) error.Format("%s", msg); else error.Format("error = %d", resultVal); AfxMessageBox(error); } return E_FAIL; } return S_OK; } HRESULT CCommunication::sendQueryUserRequest(LPCTSTR userID) { CXMLParser parser; CElement *root = parser.createElement(_T("REQUEST"), _T("")); root->AddChild(_T("type"), _T("QUERYUSER")); root->AddChild(_T("userID"), userID); parser.set_root(root); if (parser.BuildXML() != 0) return E_FAIL; CString sendOut = parser.get_xml(); int ret = _sock.Send((BYTE*)((LPCTSTR)sendOut), sendOut.GetLength()); if (ret == SOCKET_ERROR) { DWORD error = _sock.GetLastError(); TRACE(_T("ERROR: Failed to send the QUERYUSER request (%ld).\n"), error); return E_FAIL; } return S_OK; } HRESULT CCommunication::sendOnlineRequest(CString userID, u_short port) { CXMLParser parser; CString csPort; csPort.Format("%d", port); CElement *root = parser.createElement(_T("REQUEST"), _T("")); root->AddChild(_T("type"), _T("ONLINE")); root->AddChild(_T("userID"), userID); root->AddChild(_T("port"), csPort); parser.set_root(root); if (parser.BuildXML() != 0) return E_FAIL; CString sendOut = parser.get_xml(); int ret = _sock.Send((BYTE*)((LPCTSTR)sendOut), sendOut.GetLength()); if (ret == SOCKET_ERROR) { TRACE("ERROR: Failed to send the ONLINE request.\n"); return E_FAIL; } return S_OK; }