123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- #include <stdafx.h>
- #include "CMessage.h"
- #include <memory.h>
- #include <string>
- namespace Chromium{
- std::string UtfToGbk(const char* utf8)
- {
- int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
- wchar_t* wstr = new wchar_t[len + 1];
- memset(wstr, 0, len + 1);
- MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
- len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
- char* str = new char[len + 1];
- memset(str, 0, len + 1);
- WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
- if (wstr) delete[] wstr;
- return str;
- }
- //GBKת»¯ÎªUTF8¸ñʽ
- std::string ConvertGBKToUtf8(std::string &strGBK)
- {
- int len = MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK.c_str(), -1, NULL, 0);
- wchar_t * wszUtf8 = new wchar_t[len];
- memset(wszUtf8, 0, len);
- MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK.c_str(), -1, wszUtf8, len);
- len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL);
- char *szUtf8 = new char[len + 1];
- memset(szUtf8, 0, len + 1);
- WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL, NULL);
- std::string ret = szUtf8;
- delete[] szUtf8;
- delete[] wszUtf8;
- return ret;
- }
- 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;
- }
- int err = memcpy_s(p, this->_buf_length*2, this->_data, this->_buf_length);
- if (0 != err)
- {
- delete[] p;
- return -1;
- }
- delete[] this->_data;
- this->_data = p;
- this->_buf_length = this->_buf_length*2;
- return (this->_buf_length);
- }
- int CMessage::getBufferLength(){
- return *(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;
- }
-
- }
- 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;
- }
- }
|