void SetAuthUser(unsigned int nUserId) { assert(m_nUserId==0); m_nUserId = nUserId; CDataBuffer<100> sbuf; sbuf.PutValue<unsigned int>(0); sbuf.PutValue<unsigned short>(0); sbuf.PutValue(m_nSeq); pLoop->PushMsg(SGCMDCODE_CONNECT, nUserId, sbuf.GetBuffer(), sbuf.GetLength()); }
void OnData(DWORD nSize, LPVOID pData) { if(!m_bActive) return; if(sizeof(m_DataBuf)-m_dwDataBufSize<nSize) { Disconnect(); return; } memcpy(m_DataBuf+m_dwDataBufSize, pData, nSize); m_dwDataBufSize += nSize; for(;;) { WORD len; if(m_dwDataBufSize<sizeof(len)) break; len = *((WORD*)m_DataBuf); if(m_dwDataBufSize<sizeof(len)+len) break; WORD code; if(sizeof(code)>len) { Disconnect(); return; } code = *((WORD*)(m_DataBuf+sizeof(len))); switch(code) { case SGCMDCODE_LOGIN: if(m_nUserId) { Disconnect(); return; } { unsigned int nUserId; const char* pUserName; const char* pPassword; CDataReader buf(m_DataBuf+sizeof(len)+sizeof(code), len-sizeof(code)); buf.GetString(pUserName); buf.GetString(pPassword); nUserId = (unsigned int)atoi(pUserName); int ret = -1; if(strcmp(pPassword, "password")==0) { ret = 0; SetAuthUser(nUserId); } CDataBuffer<100> sbuf; sbuf.PutValue<unsigned short>(SGCMDCODE_LOGIN_RETURN); sbuf.PutValue(ret); SendData(sbuf.GetBuffer(), sbuf.GetLength()); } break; default: if(!m_nUserId) { Disconnect(); return; } pLoop->PushMsg(SGCMDCODE_USERDATA, m_nUserId, m_DataBuf+sizeof(len), len); break; } memmove(m_DataBuf, &m_DataBuf[sizeof(len)+len], m_dwDataBufSize-sizeof(len)-len); m_dwDataBufSize -= sizeof(len) + len; } }