CDataBuffer::CDataBuffer(const CDataBuffer& Buffer) : m_BufferSize(Buffer.GetDataSize()), m_AllocPercentage(Buffer.m_AllocPercentage), m_DataEnd(0) { m_Buffer = (BYTE*)malloc(m_BufferSize); Append(Buffer.GetData(), Buffer.GetDataSize()); }
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 OnConnect(unsigned int nIP, unsigned short nPort) { m_bActive = TRUE; m_nIP = nIP; m_nPort = nPort; CDataBuffer<100> buf; unsigned char aSalt[10]; buf.PutValue<unsigned short>(SGCMDCODE_LOGIN_SEED); memset(aSalt, 0xf3, sizeof(aSalt)); buf.PutArray(aSalt, sizeof(aSalt)); SendData(buf.GetBuffer(), buf.GetLength()); }
ConnectionResult CGenericServer::FirstPhaseConnection(const CString& key,const char* local_ip, char* buff, int buf_len,int& reply_length) { CString s_address; int s_port; CDataBuffer request; //request line CHttpRequest http_request(GET_M,DIFFIE_HELLMAN_CLIENT_HELLO,HTTP_1_0,EMPTY_STRING); //headers http_request.AddHeader(NETWORK_NAME_HEADER,_network_name); http_request.AddHeader(DATA_PORT_HEADER,_data_sock.GetLocalPort()); http_request.AddHeader(KEEPALIVE_PORT_HEADER,_thread->GetHeartBeatPort()); http_request.AddHeader(ADDRESS_HEADER,local_ip); //end http_request.Finalize(request); CDataBuffer::Encrypt(request.GetBuffer(),request.GetLength(),&key); START_TRY GetLog()->SetConsoleColor(YELLOW); GetLog()->Log(LOG_LEVEL_INFO,"[%s] [Send] Client Hello [%s:%d --> %s:%d]",GetUserName().GetBuffer(), local_ip,_data_sock.GetLocalPort(),_eib_address.GetBuffer(),_eib_port); _data_sock.SendTo(request.GetBuffer(),request.GetLength(),_eib_address,_eib_port); reply_length = _data_sock.RecvFrom(buff,buf_len,s_address,s_port,5000); END_TRY_START_CATCH(ex) GetLog()->SetConsoleColor(RED); GetLog()->Log(LOG_LEVEL_ERROR,"[%s] Cannot connect to eib server: %s",GetUserName().GetBuffer(),ex.what()); return STATUS_INRERNAL_ERR; END_TRY_START_CATCH_SOCKET(e) GetLog()->SetConsoleColor(RED); GetLog()->Log(LOG_LEVEL_ERROR,"[%s] Socket Error: Reason: %s",GetUserName().GetBuffer(),e.what()); return STATUS_INRERNAL_ERR; END_TRY_START_CATCH_ANY GetLog()->SetConsoleColor(RED); GetLog()->Log(LOG_LEVEL_ERROR,"[%s] Cannot connect to eib server... Unknown Exception.",GetUserName().GetBuffer()); return STATUS_INRERNAL_ERR; END_CATCH if (reply_length > 0){ return STATUS_CONN_OK; } //no reply from EIB... GetLog()->SetConsoleColor(RED); GetLog()->Log(LOG_LEVEL_ERROR,"No reply from EIB Server. maybe EIBServer is down?"); return STATUS_NO_REPLY; }
/////////////////////////////////////////////////////////////////////////////// /// Reads the copy operation state from a buffer /// /// \param [in] const BYTEVECTOR& lambdaBuffer - the buffer to read from /// \param [in,out] PULONG pLambdaBufPos - the position in the buffer to read from /// \return std::unique_ptr<CCopyOperation> - the deserialised copy operation, /// or nullptr if lambdaBuffer did not contain a CCopyOperation at /// position *pLambdaBufPos /////////////////////////////////////////////////////////////////////////////// std::unique_ptr<CCopyOperation> CCopyOperation::TryDeserialise(const CDataBuffer& copyFromBuffer, IDataReader& lambdaReader) { // check the type of the operation if (lambdaReader.PeekByte() != COPY_OPERATION_TYPE) { // not a copy op return nullptr; } lambdaReader.ReadByte(); // get length ULONG nNumBytesToCopy = lambdaReader.ReadULONG(); // get dictionary buffer offset to copy from ULONG nOffset = lambdaReader.ReadULONG(); std::unique_ptr<CCopyOperation> pCopyOp( new CCopyOperation( copyFromBuffer, CDataBuffer(copyFromBuffer.Buffer() + nOffset, nNumBytesToCopy) ) ); #ifdef DEBUG_OUTPUT std::cout << "<CopyOp><Offset>" << nOffset << "</Offset><Length>" << nNumBytesToCopy << "</Length></CopyOp>" << std::endl; #endif return pCopyOp; }
std::vector<TickData> GtaStockQuoter::querySnap(const std::vector<Contract> &contracts) { std::string codes = contracts2string(contracts); std::vector<TickData> vec; CDataBuffer<CQuotationDataPtr> lst; RetCode code = api_->QuerySnap(Category_Quotation, const_cast<char*>(codes.c_str()), lst); int cnt = lst.Size(); for (int i = 0; i< cnt; i++) { IQuotationData& quo_data = *lst[i]; TickData tick; fromGtaStockTick(quo_data, &tick); vec.push_back(tick); } return vec; }
std::vector<Contract> GtaStockQuoter::getStockList(const char* market) { char* market_tag = const_cast<char*>(market); std::vector<Contract> contracts; CDataBuffer<StockSymbol> list; api_->GetStockList(market_tag,list); int cnt = list.Size(); for (int i = 0; i < cnt; i++) { std::vector<std::string> c = Util::split(list[i].Symbol, '.'); Contract contract(map2QDExchType(c[1].c_str()), c[0]); contract.volume_multiple = 100; contracts.push_back(contract); } return contracts; }
void CXmlDocument::ToString(CDataBuffer& buffer) { TiXmlPrinter printer; printer.SetLineBreak("\r\n"); printer.SetIndent("\t"); _doc->Accept(&printer); buffer.Add(printer.CStr()); }
void TestDataBuffer() { CDataBuffer DataBuffer; char* String = "SimpleString"; int StringLength = 12; int NumberOfRepetitions = 1000; for(int i = 0; i < NumberOfRepetitions; ++i) DataBuffer.Append((BYTE*)String, StringLength); Assert(DataBuffer.GetDataSize() == StringLength * NumberOfRepetitions); const BYTE* Data = DataBuffer.GetData(); for(int j = 0; j < NumberOfRepetitions; ++j) { bool Equal = (0 == memcmp(String, Data, StringLength)); Assert(Equal); Data += StringLength; } CDataBuffer AnotherBuffer(DataBuffer); char* UpdateString = "UpdateString"; int UpdateStringLen = (int)strlen(UpdateString); int UpdatePosition = 10; Assert(AnotherBuffer.UpdateData(UpdatePosition, (BYTE*)UpdateString, UpdateStringLen) == true); Data = AnotherBuffer.GetData(); Assert(memcmp(Data + UpdatePosition, UpdateString, UpdateStringLen) == 0); TestStruct TestType; AnotherBuffer.AppendType(TestType); CDataBuffer AssignedBuffer = AnotherBuffer; Assert(AssignedBuffer.GetDataSize() == AnotherBuffer.GetDataSize()); Assert(memcmp(AssignedBuffer.GetData(), AnotherBuffer.GetData(), AnotherBuffer.GetDataSize()) == 0); }
void CHttpRequest::Finalize(CDataBuffer& raw_request) { raw_request.Clear(); if(!IsHeaderExist(CONTENT_LENGTH_HEADER)){ CHttpHeader content_length; content_length.SetName(CONTENT_LENGTH_HEADER); content_length.SetValue(CString(_content.GetLength())); AddHeader(content_length); } if (_method == GET_M){ raw_request += HTTP_METHOD_GET_STR; } else if (_method == POST_M){ raw_request += HTTP_METHOD_POST_STR; } else{ throw CEIBException(HttpProtocolError,"Http Method not defined."); } raw_request += SPACE; raw_request += _request_uri; //parameters section if(_params.size() > 0){ raw_request += QUERY_STRING_SEPERATOR; } map<CString,CHttpParameter>::iterator params_it; for(params_it = _params.begin(); params_it != _params.end(); ++params_it) { raw_request += params_it->second.GetName(); raw_request += PARAMETER_SEPERATOR; raw_request += params_it->second.GetValue(); } raw_request += SPACE; if (_version == HTTP_1_0){ raw_request += HTTP_1_0_STR; } else if (_version == HTTP_1_1){ raw_request += HTTP_1_0_STR; } else{ throw CEIBException(HttpProtocolError,"Http version not defined."); } raw_request += CRLF; map<CString,CHttpHeader>::iterator it; for(it = _headers.begin(); it != _headers.end(); ++it) { raw_request += it->second.GetName(); raw_request += HEADER_SEPERATOR; raw_request += SPACE; raw_request += it->second.GetValue(); raw_request += CRLF; } if (_content.GetLength() > 0){ raw_request += CRLF; raw_request.Add(_content.GetBuffer(),_content.GetLength()); } raw_request += CRLF; raw_request += CRLF; }
void CDataBuffer::Set(const CDataBuffer& data) { Clear(); Add((char*)data.GetBuffer(),data.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; } }