Example #1
0
int XSocket::iSendMsg(char * cData, uint32_t dwSize, char cKey) {
	uint16_t * wp;
	int iRet;

	//m_pSndBuffer = cData;
	if (dwSize > m_dwBufferSize) return DEF_XSOCKEVENT_MSGSIZETOOLARGE;

	if (m_cType != DEF_XSOCK_NORMALSOCK) return DEF_XSOCKEVENT_SOCKETMISMATCH;
	if (m_cType == 0) return DEF_XSOCKEVENT_NOTINITIALIZED;
	m_pSndBuffer[0] = cKey;

	wp = (uint16_t *) (m_pSndBuffer + 1);
	*wp = dwSize + 3;

	memcpy((char *) (m_pSndBuffer + 3), cData, dwSize);
	if (cKey != 0) {//Encryption
		for (uint16_t i = 0; i < dwSize; i++) {
			m_pSndBuffer[3 + i] += (i ^ cKey);
			m_pSndBuffer[3 + i] = m_pSndBuffer[3 + i] ^ (cKey ^ (dwSize - i));
		}
	}

	iRet = _iSend(m_pSndBuffer, dwSize + 3, true);

	if (iRet < 0) return iRet;
	else return (iRet - 3);
}
int XSocket::iSendMsg(char * Data, DWORD dwSize, char cKey)
{
 WORD * wp;
 int    i, iRet;

	if (dwSize > m_dwBufferSize) return XSOCKEVENT_MSGSIZETOOLARGE;

	if (m_cType != XSOCK_NORMALSOCK) return XSOCKEVENT_SOCKETMISMATCH;
	if (m_cType == NULL) return XSOCKEVENT_NOTINITIALIZED;

	m_pSndBuffer[0] = cKey;

	wp  = (WORD *)(m_pSndBuffer + 1);
	*wp = (WORD)(dwSize + 3);

	memcpy((char *)(m_pSndBuffer + 3), Data, dwSize);
	if (cKey != NULL) {
		for (i = 0; i < (int)(dwSize); i++) {
			m_pSndBuffer[3+i] += (i ^ cKey);
			m_pSndBuffer[3+i] = (char)( m_pSndBuffer[3+i] ^ (cKey ^ (dwSize - i)) );
		}
	}
	
	if (m_bIsWriteEnabled == FALSE) {
		iRet = _iRegisterUnsentData(m_pSndBuffer, dwSize +3);
	} 
	else iRet = _iSend(m_pSndBuffer, dwSize + 3, TRUE);

	if (iRet < 0) return iRet;
	else return (iRet - 3);
}
//=============================================================================
int XSocket::iSendMsg(char * cData, DWORD dwSize, char cKey, BOOL log)
{
 WORD * wp;
 int    iRet;
 DWORD  i;
 char   m_msgBuff[50000], dataBuff[50000];
 PutLogList("iSendMsg");
        if (dwSize > m_dwBufferSize) return XSOCKEVENT_MSGSIZETOOLARGE;
	if (m_cType != XSOCK_NORMALSOCK) return XSOCKEVENT_SOCKETMISMATCH;
	if (m_cType == NULL) return XSOCKEVENT_NOTINITIALIZED;

	m_pSndBuffer[0] = cKey;

	wp  = (WORD *)(m_pSndBuffer + 1);
	*wp = (WORD)(dwSize + 3);

	memcpy((char *)(m_pSndBuffer + 3), cData, dwSize);
	if(log){
        ZeroMemory(m_msgBuff, sizeof(m_msgBuff));
        ZeroMemory(dataBuff, sizeof(dataBuff));
        wsprintf(m_msgBuff,"Msg [%lu] was sent = ", dwSize);
        memcpy(dataBuff, cData, dwSize);

        for(i = 0; i < dwSize; i++) if(dataBuff[i] == NULL) dataBuff[i] = ' ';
        strcat(m_msgBuff, dataBuff);
        PutLogFileList(m_msgBuff, XSOCKET_LOGFILE);
	}
    if (cKey != NULL) {//Encryption
		for (i = 0; i < dwSize; i++) {
			m_pSndBuffer[3+i] += (i ^ cKey);
			m_pSndBuffer[3+i]  = (char)(m_pSndBuffer[3+i] ^ (cKey ^ (dwSize - i)));
		}
	}

	iRet = _iSend(m_pSndBuffer, dwSize + 3, TRUE);

	if (iRet < 0) return iRet;
	else return (iRet - 3);
}