📄 _emsp.cpp
字号:
#include "stdafx.h"
#include "net_protocal.h"
#include "_MSG_Head.h"
#include "_MBLOGIN.h"
#include "_MB_LOGOFF.h"
#include "_MBLOGIN_RESP.h"
#include "time.h"
#include ".\_emsp.h"
#include"_MBNOOP.h"
#include "EMSP_ERROR.h"
#include "_MBNOOP_RESP.h"
#include ".\clientsocket.h"
#include <typeinfo.h>
#include <windows.h>//Use system resorce lock
using namespace std;
using namespace EMSP2_0;
EMSP2_0::C_EMSP::C_EMSP(void)
: theSock(NULL)
, m_SeqNum(0)
, timeout(0)
{
time_t t=time(NULL);
m_SeqNum=t;
m_UserNo=0;
m_logined=false;
}
EMSP2_0::C_EMSP::~C_EMSP(void)
{
}
ClientSocket* EMSP2_0::C_EMSP::getSock(void)
{
return theSock;
}
void EMSP2_0::C_EMSP::SetSock(ClientSocket* sock)
{
theSock=sock;
}
char EMSP2_0::C_EMSP::getConnectionType(void)
{
return m_ConnType;
}
void EMSP2_0::C_EMSP::SetConnectionType(char cType)
{
m_ConnType=cType;
}
int EMSP2_0::C_EMSP::GetCorpNO(void)
{
return m_CorpNo;
}
void EMSP2_0::C_EMSP::SetCorpNo(int aCorpNo)
{m_CorpNo=aCorpNo;
}
char EMSP2_0::C_EMSP::GetCrypt(void)
{
return m_Crypt;
}
void EMSP2_0::C_EMSP::SetCrypt(char cType)
{
m_Crypt=cType;
}
char EMSP2_0::C_EMSP::GetLoginType(void)
{
return m_loginType;
}
void EMSP2_0::C_EMSP::SetLoginType(char lType)
{
m_loginType=lType;
}
string EMSP2_0::C_EMSP::getPassword(void)
{
return m_pass;
}
void EMSP2_0::C_EMSP::SetPassWord(string psw)
{
m_pass=psw;
}
int EMSP2_0::C_EMSP::GetCommandSeqNum(void)
{
m_SeqNum++;
return m_SeqNum;
}
string EMSP2_0::C_EMSP::GetSessionID(void)
{
return m_SessionID;
}
string EMSP2_0::C_EMSP::GetSessionKey(void)
{
return m_SessionKey;
}
string EMSP2_0::C_EMSP::GetUserID(void)
{
return m_UserID;
}
void EMSP2_0::C_EMSP::SetUserID(string user)
{
m_UserID=user;
}
int EMSP2_0::C_EMSP::GetUserNO(void)
{
return m_UserNo;
}
bool EMSP2_0::C_EMSP::Login(void)
{
MsgHead msgh;
msgh.Command_ID=C_LOGIN;
msgh.Crypt=m_Crypt;
msgh.Seqnum=GetCommandSeqNum();
msgh.UserNo=m_UserNo;
MB_LOGIN mbl;
mbl.ConnectType=m_ConnType;
mbl.CorpNo=m_CorpNo;
mbl.LoginType=m_loginType;
mbl.UserID=m_UserID;
mbl.Password=m_pass;
C_MBLOGIN mblgobj(mbl);
char* bbuff=mblgobj.getBytes();
int bsize=mblgobj.getSize();
msgh.Total_length=bsize+13;
C_MSG_Head msgobj(msgh);
char *result=msgobj.getBytes();
if(!this->SendBuff((void*)result,13) )
{
printf("Net Error ID%d\n,can't Send Package",theSock->Handle);
}
SendBuff((void*)bbuff,bsize);
lastActive=time(NULL);
//Get the Serer Response
char hbuff[13];
if(ReceiveBuf((void*)hbuff,13)>0)
{
msgobj.TransFromchars(hbuff,13);
if(msgobj.value()->Command_ID==C_LOGIN_RESP)
{
short size=msgobj.value()->Total_length-13;
char* blr=new char[size];
ReceiveBuf((void*)blr,size);
C_MBLOGIN_RESP mbrobj;
mbrobj.TransFromchars(blr,size);
if(mbrobj.value()->Result==0)//login ok
{
m_UserNo=mbrobj.value()->UserNO;
m_SessionID=mbrobj.value()->SessionID;
m_SessionKey=mbrobj.value()->SessionKey;
m_logined=true;
time_t tl=time(NULL);
string s=typeid(*this).name();
s=s+ ctime(&tl);
s=s+":"+"Login OK ";
writelog(s.c_str());
return true;
}
else
{
EMSP2_0::ShowError(mbrobj.value()->Result);
}
delete[] blr;
}
}
time_t tl=time(NULL);
string s=typeid(*this).name();
s=s+ ctime(&tl);
s=s+":"+"Login failed ";
writelog(s.c_str());
return false;
}
int EMSP2_0::C_EMSP::GetTimeOut(void)
{
return timeout;
}
void EMSP2_0::C_EMSP::SetTimeOut(int value)
{
timeout=value;
}
bool EMSP2_0::C_EMSP::logout(void)
{ MsgHead mb;
FillHead(&mb,C_LOGOFF);
MB_LOGOFF mblf;
mblf.SessionID=m_SessionID;
mb.Total_length=13+m_SessionID.size();
C_MSG_Head mgobj(mb);
C_MB_LOGOFF mblobj(mblf);
char* hb=mgobj.getBytes();
char* bb=mblobj.getBytes();
if(SendBuff(hb,13)>0)
{
if( SendBuff(bb,mblobj.getSize())>0 )
{
return true;
}
}
return false;
}
void EMSP2_0::C_EMSP::FillHead(MsgHead* msgh, unsigned short CMDID)
{
msgh->Command_ID=CMDID;
msgh->Crypt=m_Crypt;
msgh->Seqnum=GetCommandSeqNum();
msgh->UserNo=m_UserNo;
}
bool EMSP2_0::C_EMSP::isLogined()
{
return m_logined;
}
void EMSP2_0::C_EMSP::KeepConnection(void)
{
RTL_CRITICAL_SECTION rt;
InitializeCriticalSection(&rt) ;
EnterCriticalSection(&rt);
time_t t =time(NULL);
if ((t-lastActive)>3)
{
MsgHead mh;
FillHead(&mh,C_NOOP);
MB_NOOP mb;
mb.SessionID=m_SessionID;
C_MBNOOP mbobj(mb);
int size=mb.SessionID.size()+1;
mh.Total_length=size+13;
C_MSG_Head mhObj(mh);
char* mhBuf=mhObj.getBytes();
char* mbBuf=mbobj.getByttes();
SendBuff((void*)mhBuf,13);
SendBuff((void*)mbBuf,size);
char buff[13];
if(ReceiveBuf(buff,13))
{
mhObj.TransFromchars(buff,13);
int rsize=mhObj.value()->Total_length-13;
char * rbuff=new char[rsize];
if(ReceiveBuf((void*)rbuff,rsize))
{ C_MBNOOP_RESP mrb;
mrb.TransFromchars(rbuff,rsize);
if(mrb.value()->Result==0) //Keep Connection OK
{
m_SessionID=mrb.value()->SessionID;
lastActive=time(NULL);
string s=typeid(*this).name();
s=s+ ctime(&lastActive);
s=string("Keep Connection OK ")+string(":")+s;
writelog(s.c_str());
}
else
{
theSock->ReConnect();
Login();
EMSP2_0::ShowError(mrb.value()->Result);
}
}
else
{
theSock->ReConnect();
Login();
}
delete[] rbuff;
} else
{
theSock->ReConnect();
Login();
}
}
LeaveCriticalSection(&rt) ;
DeleteCriticalSection(&rt) ;
}
int EMSP2_0::C_EMSP::SendBuff(void* buff, int size)
{
if (theSock->isConnected())
{
return theSock->SendBuf(buff,size);
}else
{
if(theSock->ReConnect())
{
if(Login())
{
return theSock->SendBuf(buff,size);
}
}
else
{
throw "Can't Connect to Server";
}
}
}
int EMSP2_0::C_EMSP::ReceiveBuf(void* buff, int size)
{
if (theSock->isConnected())
{
return theSock->ReceiveBuf(buff,size);
}else
{
if(theSock->ReConnect())
{
if(Login())
{
return theSock->ReceiveBuf(buff,size);
}
}
else
{
throw "Can't Connect to Server";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -