int SSClient::RecvMsg( Packet & packet) { DataBuffer * buff = packet.GetBuffer(); //消息头接收 int iRecv = SockRecv( buff->GetWritePtr(), PACKET_HEADER_LENGTH); if( iRecv < 0 || iRecv != PACKET_HEADER_LENGTH ) return -1; //移动 写指针 buff->MoveWritePtr(iRecv); //解包头 packet.UnpackHeader(); //包体长度 int iLeft = packet.Length- PACKET_HEADER_LENGTH; if( 0 == iLeft ) return 0; //消息体接收 iRecv = SockRecv( buff->GetWritePtr(), iLeft ); if( iRecv != iLeft ) return -1; //移动 写指针 buff->MoveWritePtr(iRecv); return 0; }
LRESULT CLoginClient::Login(LPCTSTR lpstrAccount, LPCTSTR lpstrPswd, LPUSER_ACCOUNT_T lpUserAccountInfo) { assert(lpstrAccount); assert(lpstrPswd); assert(lpUserAccountInfo); LRESULT hr = S_OK; REQUEST_LOGIN_PKG_T stPkg = { 0 }; REPLY_LOGIN_PKG_T stPkgReply = { 0 }; MSGHEAD stReplyHead = { 0 }; int nReplyBufLen = 0; char* pReplyBuf = NULL; int nOffset = 0; _stprintf_s(stPkg.szAccount, MAX_ACCOUNT_LEN, _T("%s"), lpstrAccount); _stprintf_s(stPkg.szPswd, MAX_PASSWORD_LEN, _T("%s"), lpstrPswd); hr = SendCmd(CMD_USER_LOGIN_REQUEST, (char*)&stPkg, sizeof(stPkg)); CHECK_SOK(hr); // wait for server reply nReplyBufLen = sizeof(MSGHEAD)+sizeof(REPLY_LOGIN_PKG_T); pReplyBuf = (char*)malloc(nReplyBufLen); if (!SockRecv(pReplyBuf, nReplyBufLen)) { hr = ERR_CLIENT_SER_REPLY_NOREPLY; } CHECK_SOK(hr); nOffset = MemOP_Read(pReplyBuf, &stReplyHead, sizeof(stReplyHead), nOffset); nOffset = MemOP_Read(pReplyBuf, &stPkgReply, sizeof(stPkgReply), nOffset); if (stReplyHead.dwMsgType != CMD_USER_LOGIN_REPLY || stReplyHead.dwDataLen != sizeof(stPkgReply)) { hr = ERR_CLIENT_SER_REPLY_NOREPLY; } CHECK_SOK(hr); if (stPkgReply.nResult != 0) hr = ERR_CLIENT_SER_REPLY_FALSE; else hr = ERR_CLIENT_SER_REPLY_TRUE; CHECK_SOK(hr); // 最终登录成功,才会返回用户数据 lpUserAccountInfo->guidUser = stPkgReply.guidUser; memcpy(lpUserAccountInfo->szUserAlias, stPkgReply.szUserAlias, sizeof(stPkgReply.szUserAlias)); memcpy(lpUserAccountInfo->szUserName, stPkgReply.szUserName, sizeof(stPkgReply.szUserName)); lpUserAccountInfo->tmLastLogin = stPkgReply.tmLastLogin; done: SAFE_FREE(pReplyBuf); return hr; }
LRESULT CLoginClient::SignUp(LPCTSTR lpstrAccount, LPCTSTR lpstrPswd, LPCTSTR lpstrName, LPCTSTR lpstrAlias) { assert(lpstrAccount); assert(lpstrPswd); assert(lpstrAlias); assert(lpstrName); LRESULT hr = S_OK; REQUEST_SIGNUP_PKG_T stPkg = { 0 }; REPLY_SIGNUP_PKG_T stPkgReply = { 0 }; MSGHEAD stReplyHead = { 0 }; int nReplyBufLen = 0; char* pReplyBuf = NULL; int nOffset = 0; _stprintf_s(stPkg.szAccount, MAX_ACCOUNT_LEN, _T("%s"), lpstrAccount); _stprintf_s(stPkg.szPswd, MAX_PASSWORD_LEN, _T("%s"), lpstrPswd); _tcscpy_s(stPkg.szName, MAX_USER_NAME_LEN, lpstrName); _tcscpy_s(stPkg.szAlias, MAX_USER_ALIAS_LEN, lpstrAlias); hr = SendCmd(CMD_USER_SIGNGUP_REQUEST, (char*)&stPkg, sizeof(stPkg)); CHECK_SOK(hr); // wait for server reply nReplyBufLen = sizeof(MSGHEAD)+sizeof(REPLY_SIGNUP_PKG_T); pReplyBuf = (char*)malloc(nReplyBufLen); if (!SockRecv(pReplyBuf, nReplyBufLen)) { hr = ERR_CLIENT_SER_REPLY_NOREPLY; } CHECK_SOK(hr); nOffset = MemOP_Read(pReplyBuf, &stReplyHead, sizeof(stReplyHead), nOffset); nOffset = MemOP_Read(pReplyBuf, &stPkgReply, sizeof(stPkgReply), nOffset); if (stReplyHead.dwMsgType != CMD_USER_SIGNUP_REPLY || stReplyHead.dwDataLen != sizeof(stPkgReply)) { hr = ERR_CLIENT_SER_REPLY_NOREPLY; } CHECK_SOK(hr); if (stPkgReply.nResult != 0) hr = ERR_CLIENT_SER_REPLY_FALSE; else hr = ERR_CLIENT_SER_REPLY_TRUE; CHECK_SOK(hr); done: SAFE_FREE(pReplyBuf); return hr; }
static int SockProc(Ns_DriverCmd cmd, Ns_Sock *sock, struct iovec *bufs, int nbufs) { int n; switch (cmd) { case DriverRecv: n = SockRecv(sock->sock, bufs, nbufs); if (n < 0 && ns_sockerrno == EWOULDBLOCK && Ns_SockWait(sock->sock, NS_SOCK_READ, sock->driver->recvwait) == NS_OK) { n = SockRecv(sock->sock, bufs, nbufs); } break; case DriverSend: n = SockSend(sock->sock, bufs, nbufs); if (n < 0 && ns_sockerrno == EWOULDBLOCK && Ns_SockWait(sock->sock, NS_SOCK_WRITE, sock->driver->sendwait) == NS_OK) { n = SockSend(sock->sock, bufs, nbufs); } break; case DriverKeep: case DriverClose: /* NB: Nothing to do. */ n = 0; break; default: /* Unsupported command. */ n = -1; break; } return n; }
LRESULT CLoginClient::LogOut(const COS_GUID *pUserGuid) { assert(pUserGuid); LRESULT hr = S_OK; REQUEST_LOGOUT_PKG_T stPkg = { 0 }; REPLY_LOGOUT_PKG_T stPkgReply = { 0 }; MSGHEAD stReplyHead = { 0 }; int nReplyBufLen = 0; char* pReplyBuf = NULL; int nOffset = 0; //填充数据 stPkg.guidUser = *pUserGuid; hr = SendCmd(CMD_USER_LOGOUT_REQUEST, (char*)&stPkg, sizeof(stPkg)); CHECK_SOK(hr); // wait for server reply nReplyBufLen = sizeof(MSGHEAD)+sizeof(REPLY_LOGOUT_PKG_T); pReplyBuf = (char*)malloc(nReplyBufLen); if (!SockRecv(pReplyBuf, nReplyBufLen)) { hr = ERR_CLIENT_SER_REPLY_NOREPLY; } CHECK_SOK(hr); nOffset = MemOP_Read(pReplyBuf, &stReplyHead, sizeof(stReplyHead), nOffset); nOffset = MemOP_Read(pReplyBuf, &stPkgReply, sizeof(stPkgReply), nOffset); if (stReplyHead.dwMsgType != CMD_USER_LOGOUT_REPLY || stReplyHead.dwDataLen != sizeof(stPkgReply)) { hr = ERR_CLIENT_SER_REPLY_NOREPLY; } CHECK_SOK(hr); if (stPkgReply.nResult != 0) hr = ERR_CLIENT_SER_REPLY_FALSE; else hr = ERR_CLIENT_SER_REPLY_TRUE; CHECK_SOK(hr); done: SAFE_FREE(pReplyBuf); return hr; }
bool IrcBot::Login() { if (SendData(USER, IRC_USER)) { if (SendData(NICK, IRC_NICK)) { SockRecv(); return true; } else sLog->outError("<IrcBot> - There was an error in SendData(NICK, IRC_NICK)"); } else sLog->outError("<IrcBot> - There was an error in SendData(USER, IRC_USER)"); return false; }
void IrcBot::run() { // Let core breath out 500 ms after core startup :) ACE_Based::Thread::Sleep(500); sLog->outString("<IrcBot> - Starting IRC"); // Create a loop while the worldserver thread is running while (!World::IsStopped()) { // Initialize socket library if (InitSocket()) { sLog->outString("<IrcBot> - Connecting..."); if (Connect()) { sLog->outString("<IrcBot> - Connected."); sLog->outString("<IrcBot> - Logging in to the IRC server..."); if (Login()) { sLog->outString("<IrcBot> - Logged in sucessfully. Recieving data..."); while (IsConnected() && !World::IsStopped()) { SockRecv(); } } } sLog->outError("<IrcBot> - Connection has been lost. Disconnecting"); Disconnect(); ACE_Based::Thread::Sleep(30 * IN_MILLISECONDS); } else { sLog->outError("<IrcBot> - Couldn't initialize socket."); ACE_Based::Thread::Sleep(10 * IN_MILLISECONDS); } } }
// ZThread Entry This function is called when the thread is created in Master.cpp (mangosd) void IRCClient::run() { // Before we begin we wait a while MaNGOS is still starting up. ACE_Based::Thread::Sleep(500); /* Connection count */ int cCount = 0; // Create a loop to keep the thread running untill active is set to false while (m_bActive && !World::IsStopped()) { // Initialize socket library if (InitSock()) { // Connect To The IRC Server sLog.outString("MangChat: Connecting to %s Try # %d", m_sHost.c_str(), cCount); if (Connect(m_sHost.c_str(), m_iPort)) { sLog.outString("MangChat: Connected And Logging In"); // On connection success reset the connection counter cCount = 0; // Login to the IRC server if (Login(m_sNick, m_sUser, m_sPass)) { sLog.outString("MangChat: Logged In And Running!!"); // While we are connected to the irc server keep listening for data on the socket while (m_bConnected && !World::IsStopped()) { SockRecv(); } } sLog.outString("MangChat: Connection To IRC Server Lost!"); } // When an error occures or connection lost cleanup Disconnect(); // Increase the connection counter ++cCount; // if MAX_CONNECT_ATTEMPT is reached stop trying if (cCount == MAX_CONNECT_ATTEMPT) m_bActive = false; // If we need to reattempt a connection wait WAIT_CONNECT_TIME milli seconds before we try again if (m_bActive) ACE_Based::Thread::Sleep(WAIT_CONNECT_TIME); } else { // Socket could not initialize cancel m_bActive = false; sLog.outError("MangChat: Could not initialize socket"); } } // thread stays alive for calls from other threads }
/*----------------------------------------------------------------------------*/ BOOL RecvDataEDI(int cSock) /*----------------------------------------------------------------------------*/ { BOOL flag = TRUE; if (flag) { pLine = (MSG_LINE*) &sRecvEdiLine; if (flag) { /* * 수신한다. : <- MSG_HDR + sDataLine */ if (flag) { /* * 전문에서 길이부분을 읽는다. */ int ret = SockRecv(cSock, pLine->stHdr.sMsgLen, 4); if (ret <= 0) { CSockFDCLR(cSock); return FALSE; } } iRecvEdiLen = 4 + UtilToIntN( pLine->stHdr.sMsgLen, 4 ); if (flag) { /* * 전문에서 길이부분을 제외한 자료부분을 읽는다. */ int ret = SockRecv(cSock, pLine->stHdr.sBizKnd, iRecvEdiLen - 4); if (ret <= 0) { CSockFDCLR(cSock); return FALSE; } } sRecvEdiLine[iRecvEdiLen] = 0x00; /* * 수신받은 자료를 sDataLine에 복사한다. */ strcpy(sDataLine, pLine->sData); /* * 수신데이터를 출력한다. */ if (flag) { LOG(_FL_, 0, 0, "RECV(%d): EDI_HDR+DATA [%*.*s][%s]" , cSock, sizeof(MSG_HDR), sizeof(MSG_HDR), (char*)&pLine->stHdr , sDataLine); } } } return TRUE; }