64,671
社区成员
发帖
与我相关
我的任务
分享
UINT ReceiveThreadFunc(LPVOID lparam)
{
parmStruct* cs = (parmStruct*)lparam;
SOCKET soc = (cs->cons);
CCITICServerDlg * dialog = cs->ps->mdlg;
char rcvBuf[1024] = {'\0'};
int i=0;
APIController *myAPI = new APIController(soc, cs->ps);
CString broke = cs->ps->getController()->getBrokerID();
myAPI->Initial(broke.GetBuffer());
broke.ReleaseBuffer();
map<CString,CString> mycontainer;
BOOL isLogon = FALSE;
while(0 <= recv(soc,rcvBuf, sizeof(rcvBuf), 0))
{
//if (rcvBuf[0]!=LogonReq&&!myAPI->getIfConnectToCTPServer())
//{
// NotInWorkingTimeMessage * ntm = new NotInWorkingTimeMessage();
// myAPI->sendMessage(ntm->getString(),ntm->getLength());
// delete ntm;
// ntm = NULL;
// continue;
//}
if(rcvBuf[0]!=LogonReq)
{
if (!myAPI->getIfConnectToCTPServer())
{
NotInWorkingTimeMessage * ntm = new NotInWorkingTimeMessage();
myAPI->sendMessage(ntm->getString(),ntm->getLength());
if(ntm!=NULL)
{
delete ntm;
ntm = NULL;
}
continue;
}
}
if (!myAPI->getIfConnectToCTPServer())
{
NotInWorkingTimeMessage * ntm = new NotInWorkingTimeMessage();
myAPI->sendMessage(ntm->getString(),ntm->getLength());
if(ntm!=NULL)
{
delete ntm;
ntm = NULL;
}
continue;
}
CString tmp = rcvBuf;
switch(rcvBuf[0])
{
case MoneyReq:
break;
case TradeReq:
break;
case HistoryTradeReq:
{
myAPI->getController()->getDlg()->WriteLog("收到历史成交信息请求");
if (isLogon==FALSE)
{
}
char Account[16]={0};
char Password[41]={0};
char date[9] = {0};
char date1[9] = {0};
int i =2 ;
for (int j =0;j<sizeof(Account);j++,i++)
{
Account[j]= rcvBuf[i];
}
for (int j= 0;j<sizeof(Password);j++,i++)
{
Password[j]=rcvBuf[i];
}
for (int j=0;j<sizeof(date);j++,i++)
{
date[j] = rcvBuf[i];
}
for (int j=0;j<sizeof(date);j++,i++)
{
date1[j] = rcvBuf[i];
}
myAPI->sendHisTrade(date,date1);
}
break;
case PositionReq:
break;
case MarketReq:
break;
case SettlementReq:
{
char Account[16]={0};
char Password[41]={0};
char date[9] = {0};
int i =2 ;
for (int j =0;j<sizeof(Account);j++,i++)
{
Account[j]= rcvBuf[i];
}
for (int j= 0;j<sizeof(Password);j++,i++)
{
Password[j]=rcvBuf[i];
}
for (int j=0;j<sizeof(date);j++,i++)
{
date[j] = rcvBuf[i];
}
CString tmp = "";
tmp = date;
myAPI->getSettlementMessage(tmp);
}
break;
case gtRealTimeReq:
myAPI->getController()->getDlg()->WriteLog("收到实时信息请求");
AfxBeginThread(SendMarketDataThreadFunc,myAPI);
AfxBeginThread(SendTradeDataThreadFunc,myAPI);
AfxBeginThread(SendPositionDataThreadFunc,myAPI);
AfxBeginThread(SendMoneyDataThreadFunc,myAPI);
break;
case LogonReq:
{
myAPI->getController()->getDlg()->WriteLog("收到登录请求");
char Account[16]={0};
char Password[41]={0};
int i=2;
for (int j =0;j<sizeof(Account);j++,i++)
{
Account[j]= rcvBuf[i];
}
for (int j= 0;j<sizeof(Password);j++,i++)
{
Password[j]=rcvBuf[i];
}
myAPI->Login(Account,Password);
}
break;
case LogoutReq:
break;
case errorInfor:
break;
default:
break;
}
memset(rcvBuf,0x00,1024);
}
closesocket(soc);
myAPI->getController()->RemoveSocket(soc);
if(lparam!=NULL)
{
delete lparam;
lparam = NULL;
}
if (myAPI!=NULL)
{
myAPI->Release();
delete myAPI;
myAPI = NULL;
}
return 0;
}
NotInWorkingTimeMessage::NotInWorkingTimeMessage()
{
this->length =2; // 这边是2
}
char * NotInWorkingTimeMessage::getString()
{
// 这边实际上你用了3个byte,但是你只new了2个byte,可能是因为这个导致你在delete的时候出错
charP = new char[length];
char * tmp = charP;
*tmp = messageType;
tmp++;
*tmp = ifEnd;
tmp++;
*tmp = '/0';
return charP;
}
.h
#pragma once
#ifndef _MESSAGE_H
#define _MESSAGE_H
#include "Global.h"
class CMessage
{
public:
CMessage();
~CMessage();
virtual char* getString()=0;
int getLength();
void makeMessage(char* tmp,char* source,int ssize);
protected:
char messageType;
char ifEnd;
int length;
char* charP;
};
#endif
.cpp
#include "StdAfx.h"
#include "Message.h"
CMessage::CMessage()
{
this->ifEnd=1;
this->messageType='\29';
this->length=0;
this->charP=NULL;
}
/*------------------------------------------------------------------------------
Name:
getLength
Parm:
Return:
int
Description
To get the length of the current message
------------------------------------------------------------------------------*/
int CMessage::getLength()
{
return length;
}
/*------------------------------------------------------------------------------
Name:
makeMessage
Parm:
char* tmp
char* source
int ssize
Return:
Description
To copy the value of the source into the tmp
------------------------------------------------------------------------------*/
void CMessage::makeMessage(char* tmp,char* source,int ssize)
{
for (int j=0;j<ssize;j++)
{
*tmp=source[j];
tmp++;
}
}
CMessage::~CMessage()
{
if (charP!=NULL)
{
delete charP;
charP=NULL;
}
}
.h
#ifndef _NOTINWORKINGTIMEMESSAGE_H
#define _NOTINWORKINGTIMEMESSAGE_H
#include "Message.h"
class NotInWorkingTimeMessage:public CMessage
{
public:
NotInWorkingTimeMessage();
~NotInWorkingTimeMessage();
virtual char* getString();
private:
char flag;
};
#endif
.cpp
#include "stdafx.h"
#include "Global.h"
#include "NotInWorkingTimeMessage.h"
NotInWorkingTimeMessage::NotInWorkingTimeMessage()
{
this->ifEnd = messageBegin;
this->messageType = NotInWorkingTime;
this->length =2;
}
NotInWorkingTimeMessage::~NotInWorkingTimeMessage()
{
}
/*------------------------------------------------------------------------------
Name:
getString
Parm:
Return:
char*
Description
To get the message as char*
------------------------------------------------------------------------------*/
char * NotInWorkingTimeMessage::getString()
{
charP = new char[length];
char * tmp = charP;
*tmp = messageType;
tmp++;
*tmp = ifEnd;
tmp++;
*tmp = '/0';
return charP;
}