📄 ged125switcher.cpp
字号:
// Ged125Switcher.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Ged125Switcher.h"
#include "yes.h"
#include "SocketControl.h"
#include <process.h>
#define FAILURE_CONF 1
#define FAILURE_EVENT 2
#define OPEN_REQ 3
#define OPEN_CONF 4
#define HEARTBEAT_REQ 5
#define HEARTBEAT_CONF 6
#define CLOSE_REQ 7
#define INIT_DATA_REQ 9
#define INIT_DATA_CONF 10
#define INIT_TRKGRP_DATA_EVENT 11
#define INIT_SERVICE_DATA_EVENT 12
#define INIT_VRU_DATA_EVENT 13
#define INIT_DATA_END_EVENT 14
#define DELIVERED_EVENT 15
#define ORIGINATED_EVENT 16
#define CALL_CLEARED_EVENT 17
#define CONFERENCED_EVENT 18
#define DIVERTED_EVENT 19
#define NEW_TRANSACTION_EVENT 20
#define SET_CALL_VARIABLES_EVENT 21
#define VRU_STATUS_EVENT 22
#define TRKGRP_STATUS_EVENT 23
#define SERVICE_STATUS_EVENT 24
#define ROUTE_REQUEST_EVENT 41
#define ROUTE_SELECT 42
#define ROUTE_END_EVENT 43
#define ROUTE_END 44
#define TIME_SYNCH_REQ 45
#define TIME_SYNCH_CONF 46
#define SERVICE_CONTROL 47
#define REGISTER_VARIABLES 49
#define INIT_SERVICE_CTRL_REQ 1
#define INIT_SERVICE_CTRL_CONF 2
#define INIT_SERVICE_CTRL_DATA 3
#define INIT_SERVICE_CTRL_END 4
#define NEW_CALL 5
#define REQUEST_INSTRUCTION 6
#define RUN_SCRIPT_REQ 7
#define RUN_SCRIPT_RESULT 8
#define CONNECT 9
#define EVENT_REPORT 10
#define DIALOGUE_FAILURE_CONF 11
#define DIALOGUE_FAILURE_EVENT 12
#define INIT_SERVICE_CTRL_TRKGRP 13
#define INIT_SERVICE_CTRL_SERVICE 14
#define INIT_SERVICE_CTRL_VRU 15
#define TRKGRP_STATUS 16
#define SERVICE_STATUS 17
#define VRU_STATUS 18
#define CANCEL 19
#define RELEASE 20
#define NEW_DIALOGUE 21
#define CONNECT_TO_RESOURCE 22
#define ICM_RESOURCE_CONNECTED 23
#define MICROAPP_CONTEXT 24
#define MICROAPP_PLAY 25
#define MICROAPP_PLAY_CONTINUE 26
#define MICROAPP_COLLECT_DATA 27
#define MICROAPP_MENU 28
#define MICROAPP_RESULT 29
#define TEMPORARY_CONNECT 30
#define NULL_DATA_ELEMENT 0xFFFFFFFF
#define NULL_SERVICE_ID 0xFFFFFFFF
#define NULL_CALL_ID 0xFFFFFFFF
#define MSG_HEAD_LEN 8
#define OPEN_REQ_LEN 12
#define OPEN_CONF_LEN 24
#define INIT_SERVICE_CTRL_REQ_LEN 16
#define INIT_DATA_REQ_LEN 4
#define INIT_DATA_CON_LEN 4
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CCriticalSection m_showlock;
CCriticalSection m_idlock;
void GetSocketData(SOCKET client);
//CSocketControl m_control(GetSocketData);
bool SendInitServiceCtrlReq();
bool SendInitDataReq();
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
char m_callserver[16];
short m_port=5000;
SOCKET m_client=NULL;
int m_invokeid=0;
CWinApp theApp;
using namespace std;
bool m_run=false;
bool Login();
void ListenThread(LPVOID lp);
int GenInvokeId()
{
m_idlock.Lock();
if(m_invokeid>=0X07FFFF)
m_invokeid=0;
m_invokeid++;
m_idlock.Unlock();
return m_invokeid;
}
void ShowMsg(CString msg)
{
m_showlock.Lock();
CString strmsg;
CTime t=CTime::GetCurrentTime();
strmsg.Format("[%s] %s",t.Format("%Y-%m-%d %H:%M:%S"),msg);
cout<<(char*)(LPCTSTR)strmsg<<endl;
m_showlock.Unlock();
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
InitSocket();
strcpy(m_callserver,"192.168.80.50");
m_run=true;
_beginthread(ListenThread,0,NULL);
while(1)
{
char command[100];
cin>>command;
CString strCommand(command);
if(strCommand.CompareNoCase("SHUTDOWN")==0)
{
m_run=false;
if(m_client!=NULL)
{
closesocket(m_client);
m_client=NULL;
}
Sleep(2000);
break;
}else if(strCommand.CompareNoCase("ISCR")==0)
{
SendInitServiceCtrlReq();
}else if(strCommand.CompareNoCase("IDR")==0)
{
SendInitDataReq();
}
}
}
return nRetCode;
}
bool Login()
{
if(m_client==NULL)
m_client=ConnectSocket(m_callserver,m_port);
char loginmsg[MSG_HEAD_LEN+OPEN_REQ_LEN];
memset(loginmsg,0,MSG_HEAD_LEN+OPEN_REQ_LEN);
long tmplong;
int offset=0;
tmplong=htonl(OPEN_REQ_LEN);
memcpy(loginmsg+offset,&tmplong,4);
offset+=4;
tmplong=htonl(OPEN_REQ);
memcpy(loginmsg+offset,&tmplong,4);
offset+=4;
tmplong=htonl(GenInvokeId());
memcpy(loginmsg+offset,&tmplong,4);
offset+=4;
tmplong=htonl(5);
memcpy(loginmsg+offset,&tmplong,4);
offset+=4;
tmplong=htonl(300000);
memcpy(loginmsg+offset,&tmplong,4);
offset+=4;
int nret=SendSocket(m_client,loginmsg,offset,0);
if(nret!=offset)
{
CloseSocket(m_client);
m_client=NULL;
return false;
}
char confmsg[MSG_HEAD_LEN];
memset(confmsg,0,MSG_HEAD_LEN);
nret=RecieveSocket(m_client,MSG_HEAD_LEN,confmsg,30000);
if(nret!=MSG_HEAD_LEN)
{
CloseSocket(m_client);
m_client=NULL;
return false;
}
offset=0;
memcpy(&tmplong,confmsg+offset,4);
long plen=ntohl(tmplong);
offset+=4;
memcpy(&tmplong,confmsg+offset,4);
tmplong=ntohl(tmplong);
offset+=4;
char confct[OPEN_CONF_LEN];
memset(confct,0,OPEN_CONF_LEN);
nret=RecieveSocket(m_client,plen,confct,30000);
if(nret!=plen)
{
CloseSocket(m_client);
m_client=NULL;
return false;
}
offset=0;
if(tmplong==OPEN_CONF)
{
char msg[200];
memset(msg,0,200);
strcpy(msg,"OPEN OK SERVER setting is ");
memcpy(&tmplong,confct+offset,4);
tmplong=ntohl(tmplong);
offset+=4;
memcpy(&tmplong,confct+offset,4);
tmplong=ntohl(tmplong);
offset+=4;
strcat(msg,"UseEventFeed:");
strcat(msg,((tmplong==0)?"FALSE":"TRUE"));
memcpy(&tmplong,confct+offset,4);
tmplong=ntohl(tmplong);
offset+=4;
strcat(msg," UsePolledFeed:");
strcat(msg,((tmplong==0)?"FALSE":"TRUE"));
memcpy(&tmplong,confct+offset,4);
tmplong=ntohl(tmplong);
offset+=4;
strcat(msg," UseCallRouting:");
strcat(msg,((tmplong==0)?"FALSE":"TRUE"));
memcpy(&tmplong,confct+offset,4);
tmplong=ntohl(tmplong);
offset+=4;
strcat(msg," UseTimeSynch:");
strcat(msg,((tmplong==0)?"FALSE":"TRUE"));
memcpy(&tmplong,confct+offset,4);
tmplong=ntohl(tmplong);
offset+=4;
strcat(msg," UseServiceControl:");
strcat(msg,((tmplong==0)?"FALSE":"TRUE"));
ShowMsg(msg);
}else
{
memcpy(&tmplong,confct+offset,4);
tmplong=ntohl(tmplong);
offset+=4;
memcpy(&tmplong,confct+offset,4);
tmplong=ntohl(tmplong);
offset+=4;
char msg[100];
sprintf(msg,"****OPEN FAILED ,ERROR CODE:%ld",tmplong);
ShowMsg(msg);
CloseSocket(m_client);
m_client=NULL;
return false;
}
return true;
}
void GetSocketData(SOCKET client)
{
CString strmsg;
sockaddr stemp;
int i = sizeof(sockaddr);
getpeername(client, &stemp, &i);
sockaddr_in *temp1 = (sockaddr_in *)&stemp;
CString strIp = inet_ntoa(temp1->sin_addr);
strmsg.Format("正在从IP:%s的连接上接收数据",strIp);
ShowMsg(strmsg);
}
void ListenThread(LPVOID lp)
{
while(m_run)
{
if(m_client==NULL)
{
if (!Login())
{
Sleep(10);
continue;
}
}
char head[MSG_HEAD_LEN];
memset(head,0,MSG_HEAD_LEN);
int nret=RecieveSocket(m_client,MSG_HEAD_LEN,head,0);
if(nret!=MSG_HEAD_LEN)
{
CloseSocket(m_client);
m_client=NULL;
continue;
}
long offset=0;
long plen=0;
long tmplong=0;
long cmd=0;
offset=0;
memcpy(&tmplong,head+offset,4);
plen=ntohl(tmplong);
offset+=4;
memcpy(&tmplong,head+offset,4);
cmd=ntohl(tmplong);
offset+=4;
char* content=(char*)malloc(plen);
memset(content,0,plen);
nret=RecieveSocket(m_client,plen,content,30000);
if(nret!=plen)
{
CloseSocket(m_client);
m_client=NULL;
continue;
}
char m_msg[100];
memset(m_msg,0,100);
sprintf(m_msg,"#####################Receive Msg command:%ld",cmd);
ShowMsg(m_msg);
switch(cmd)
{
case SERVICE_CONTROL:
{
char msg[150];
memset(msg,0,150);
offset=0;
long MessageSubType=0;
memcpy(&MessageSubType,content+offset,4);
MessageSubType=ntohl(MessageSubType);
offset+=4;
long dialogid=0;
long sno=0;
memcpy(&dialogid,content+offset,4);
dialogid=ntohl(dialogid);
offset+=4;
memcpy(&sno,content+offset,4);
sno=ntohl(sno);
offset+=4;
offset+=4;
sprintf(msg,"**************Receive Service Msg command:%ld",MessageSubType);
ShowMsg(msg);
switch(MessageSubType)
{
case INIT_SERVICE_CTRL_CONF:
{
ShowMsg("---Get CMD INIT_SERVICE_CTRL_CONF");
sprintf(msg,"DialogueID:%ld,SendSeqNo:%ld",dialogid,sno);
ShowMsg(msg);
}
break;
case INIT_SERVICE_CTRL_DATA:
{
ShowMsg("---Get CMD INIT_SERVICE_CTRL_DATA");
long ServiceFeatures=0;
memcpy(&ServiceFeatures,content+offset,4);
ServiceFeatures=ntohl(ServiceFeatures);
offset+=4;
sprintf(msg,"DialogueID:%ld,SendSeqNo:%ld,ServiceFeatures:%ld",dialogid,sno,ServiceFeatures);
ShowMsg(msg);
}
break;
case INIT_SERVICE_CTRL_TRKGRP:
{
ShowMsg("---Get CMD INIT_SERVICE_CTRL_TRKGRP");
long TrunkGroupID=0;
memcpy(&TrunkGroupID,content+offset,4);
TrunkGroupID=ntohl(TrunkGroupID);
offset+=4;
long TrunkCount=0;
memcpy(&TrunkCount,content+offset,4);
TrunkCount=ntohl(TrunkCount);
offset+=4;
sprintf(msg,"DialogueID:%ld,SendSeqNo:%ld,TrunkGroupID:%ld,TrunkCount:%ld",dialogid,sno,TrunkGroupID,TrunkCount);
ShowMsg(msg);
for(int i=0;i<TrunkCount;i++)
{
short TrunkNumber=0;
memcpy(&TrunkNumber,content+offset,2);
TrunkNumber=ntohs(TrunkNumber);
offset+=2;
short TrunkStatus=0;
memcpy(&TrunkStatus,content+offset,2);
TrunkStatus=ntohs(TrunkStatus);
offset+=2;
sprintf(msg,"TrunkNumber:%d,TrunkStatus:%d",TrunkNumber,TrunkStatus);
ShowMsg(msg);
}
}
break;
case INIT_SERVICE_CTRL_VRU:
{
ShowMsg("---Get CMD INIT_SERVICE_CTRL_VRU");
long CurrentTime=0;
memcpy(&CurrentTime,content+offset,4);
CurrentTime=ntohl(CurrentTime);
offset+=4;
long TimeZoneDelta=0;
memcpy(&TimeZoneDelta,content+offset,4);
TimeZoneDelta=ntohl(TimeZoneDelta);
offset+=4;
long OperationalStatus=0;
memcpy(&OperationalStatus,content+offset,4);
OperationalStatus=ntohl(OperationalStatus);
offset+=4;
CTime t(CurrentTime);
CString yy=t.Format("%Y-%m-%d %H:%M:%S");
sprintf(msg,"DialogueID:%ld,SendSeqNo:%ld,CurrentTime:%s,TimeZoneDelta:%ld,OperationalStatus:%ld",dialogid,sno,yy,TimeZoneDelta,OperationalStatus);
ShowMsg(msg);
while(offset<plen)
{
long StatusVariable=0;
memcpy(&StatusVariable,content+offset,4);
StatusVariable=ntohl(StatusVariable);
offset+=4;
sprintf(msg,"StatusVariable:%ld",StatusVariable);
ShowMsg(msg);
}
}
break;
case INIT_SERVICE_CTRL_SERVICE:
{
ShowMsg("---Get CMD INIT_SERVICE_CTRL_SERVICE");
long ServiceID=0;
long AvailableNow=0;
long CallsInNow=0;
long CallsOutNow=0;
memcpy(&ServiceID,content+offset,4);
ServiceID=ntohl(ServiceID);
offset+=4;
memcpy(&AvailableNow,content+offset,4);
AvailableNow=ntohl(AvailableNow);
offset+=4;
memcpy(&CallsInNow,content+offset,4);
CallsInNow=ntohl(CallsInNow);
offset+=4;
memcpy(&CallsOutNow,content+offset,4);
CallsOutNow=ntohl(CallsOutNow);
offset+=4;
sprintf(msg,"DialogueID:%ld,SendSeqNo:%ld,ServiceID:%ld,AvailableNow:%ld,CallsInNow:%ld,CallsOutNow:%ld",dialogid,sno,ServiceID,AvailableNow,CallsInNow,CallsOutNow);
ShowMsg(msg);
}
break;
}
}
break;
case REGISTER_VARIABLES:
{
char msg[100];
ShowMsg("---Get CMD REGISTER_VARIABLES");
offset=0;
short vmask=0;
memcpy(&vmask,content+offset,2);
vmask=ntohs(vmask);
offset+=2;
while(offset<plen)
{
BYTE eletype=*(content+offset);
offset+=1;
BYTE elelen=*(content+offset);
offset+=1;
sprintf(msg,"eletype:%ld,elelen:%ld,plen:%ld",eletype,elelen,plen);
ShowMsg(msg);
if(eletype==42)
{
long ecctag;
memcpy(&ecctag,content+offset,4);
offset+=4;
ecctag=ntohl(ecctag);
memset(msg,0,100);
sprintf(msg,"ecctag:%ld,eccname:",ecctag);
strncat(msg,content+offset,elelen-4);
ShowMsg(msg);
offset+=elelen-4;
}else
offset+=elelen;
}
}
break;
case INIT_DATA_CONF:
{
ShowMsg("---Get CMD INIT_DATA_CONF");
}
break;
case INIT_TRKGRP_DATA_EVENT:
{
ShowMsg("---Get CMD INIT_TRKGRP_DATA_EVENT");
}
break;
case INIT_SERVICE_DATA_EVENT:
{
ShowMsg("---Get CMD INIT_SERVICE_DATA_EVENT");
}
break;
}
free(content);
content=NULL;
}
}
bool SendInitDataReq()
{
char msg[MSG_HEAD_LEN+INIT_DATA_REQ_LEN];
memset(msg,0,MSG_HEAD_LEN+INIT_DATA_REQ_LEN);
long offset=0;
long tmplong;
tmplong =htonl(INIT_DATA_REQ_LEN);
memcpy(msg+offset,&tmplong,4);
offset+=4;
tmplong =htonl(INIT_DATA_REQ);
memcpy(msg+offset,&tmplong,4);
offset+=4;
tmplong=htonl(GenInvokeId());
memcpy(msg+offset,&tmplong,4);
offset+=4;
int nret=SendSocket(m_client,msg,offset,0);
if(nret!=offset)
{
CloseSocket(m_client);
m_client=NULL;
return false;
}
return true;
}
bool SendInitServiceCtrlReq()
{
char msg[MSG_HEAD_LEN+INIT_SERVICE_CTRL_REQ_LEN];
memset(msg,0,MSG_HEAD_LEN+INIT_SERVICE_CTRL_REQ_LEN);
long offset=0;
long tmplong;
tmplong =htonl(INIT_SERVICE_CTRL_REQ_LEN);
memcpy(msg+offset,&tmplong,4);
offset+=4;
tmplong =htonl(SERVICE_CONTROL);
memcpy(msg+offset,&tmplong,4);
offset+=4;
tmplong =htonl(INIT_SERVICE_CTRL_REQ);
memcpy(msg+offset,&tmplong,4);
offset+=4;
tmplong =htonl(NULL_DATA_ELEMENT);
memcpy(msg+offset,&tmplong,4);
offset+=4;
tmplong =htonl(NULL_DATA_ELEMENT);
memcpy(msg+offset,&tmplong,4);
offset+=4;
tmplong=htonl(GenInvokeId());
memcpy(msg+offset,&tmplong,4);
offset+=4;
int nret=SendSocket(m_client,msg,offset,0);
if(nret!=offset)
{
CloseSocket(m_client);
m_client=NULL;
return false;
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -