123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- #if (defined _WIN32 || defined _WIN64)
- #include "stdafx.h"
- #endif
- #include "CMessage.h"
- #include <memory.h>
- #include <string>
- #include <locale>
- #if !(defined _WIN32 || defined _WIN64)
- int memcpy_s(void* det, size_t detSize, const void* src, size_t srcSize)
- {
- uint8_t errorcode = 0;
- if (srcSize > detSize || src == NULL || det == NULL)
- {
- if (srcSize > detSize)
- errorcode = 1;
- else if (src == NULL)
- errorcode = 2;
- else if (det == NULL)
- errorcode = 3;
- fflush(stdout);
- return -1;
- }
- else
- memcpy(det, src, srcSize);
- return 1;
- }
- #endif
- namespace Chromium {
- CMessage::CMessage() :
- _buf_length(SUGGEST_BUFFER_LENGTH) {
- this->_data = new char[SUGGEST_BUFFER_LENGTH];
- memset(this->_data, 0, SUGGEST_BUFFER_LENGTH);
- }
- CMessage::CMessage(unsigned int length) :
- _buf_length(length) {
- this->_data = new char[length];
- memset(this->_data, 0, length);
- }
- CMessage::CMessage(CMessage* p) {
- if (NULL == p)
- {
- _buf_length = SUGGEST_BUFFER_LENGTH;
- this->_data = new char[SUGGEST_BUFFER_LENGTH];
- memset(this->_data, 0, SUGGEST_BUFFER_LENGTH);
- return;
- }
- this->_buf_length = p->getCMessageLength();
- this->_data = new char[this->_buf_length];
- memcpy_s(this->_data, _buf_length, p->getWriteableData(), _buf_length);
- }
- CMessage::~CMessage() {
- delete[] this->_data;
- }
- char* CMessage::getWriteableData() {
- return this->_data;
- }
- const char* CMessage::getPayload() const {
- return this->_data;
- }
- const unsigned int CMessage::getLength() {
- return (*(int*)_data) + 8;
- }
- int CMessage::memoryDynamicGrowth() {
- // memory dynamic growth
- char* p = new char[this->_buf_length * 2];
- if (p == nullptr) {
- return -1;
- }
- if (nullptr == memcpy(p, this->_data, this->_buf_length))
- {
- delete[] p;
- return -1;
- }
- delete[] this->_data;
- this->_data = p;
- this->_buf_length = this->_buf_length * 2;
- return (this->_buf_length);
- }
- unsigned int CMessage::getBufferLength() {
- return *(unsigned int*)_data;
- // return _buf_length;
- }
- int CMessage::getMessageType() {
- return *(int*)(_data + 4);
- }
- int CMessage::getSessionID() {
- return *(int*)(_data + 12);
- }
- void CMessage::setSessionID(int sessionid, bool hasTransId) {
- int pos = hasTransId ? 12 : 8;
- *(int*)(_data + pos) = sessionid;
- }
- int CMessage::getTransID() {
- return *(int*)(_data + 8);
- }
- void CMessage::exchangeSessionIdAndTransId() {
- for (int i = 0; i < 4; i++)
- {
- char tmp = *(_data + 8 + i);
- *(_data + 8 + i) = *(_data + 12 + i);
- *(_data + 12 + i) = tmp;
- }
- }
- std::string CMessage::printfHEX(int preNum)
- {
- const char* strbuf = getPayload();
- int length = (preNum == 0 ? getLength() : preNum);
- char tmpBuf[10] = "";
- std::string hexstring;
- for (int i = 0; i < length; i++)
- {
- sprintf(tmpBuf, "%02X", (unsigned char)strbuf[i]);
- hexstring.append(tmpBuf);
- }
- return hexstring;
- }
- void CMessage::hexToFile()
- {
- FILE* fp = NULL;
- fp = fopen("chromiumTmp", "a+");
- if (fp == NULL)
- return;
- auto logtxt = printfHEX();
- int res = fprintf(fp, "%s\n", logtxt.c_str());
- fclose(fp);
- }
- void CMessage::setTransID(int transid) {
- *(int*)(_data + 8) = transid;
- }
- bool CMessage::getIsEnd() {
- return *(int*)(_data + 16);
- }
- int CMessage::getEventID() {
- return *(int*)(_data + 16);
- }
- int CMessage::getSignatureID() {
- return *(int*)(_data + 20);
- }
- void CMessage::clear() {
- memset(this->_data, 0, _buf_length);
- }
- unsigned int CMessage::getCMessageLength() {
- return this->_buf_length;
- }
- }
|