📄 sgip.cpp
字号:
#include "Sgip.h"
int ICP_BeginListen(int * Accept_ID,
unsigned short My_Port)
{
int ServeSocket ;
struct sockaddr_in ServeAddr;
int flag = 1;
int len = 4;
ServeSocket=socket(AF_INET,SOCK_STREAM,0);
if( ServeSocket == INVALID_SOCKET )
return CREAT_SOCKET_FAILED;
ServeAddr.sin_family=AF_INET;
ServeAddr.sin_port=htons(My_Port);
ServeAddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
setsockopt(ServeSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, len);
if(bind(ServeSocket,(struct sockaddr*)&ServeAddr,sizeof(ServeAddr))<0)
return BIND_SOCKET_FAILED;
if( listen(ServeSocket,5)!=0 )
return LISTEN_SOCKET_FAILED;
*Accept_ID = ServeSocket;
return 0;
}
int ICP_EndListen(int Accept_ID)
{
if( Accept_ID != INVALID_SOCKET )
{
#ifdef WIN32
if(closesocket(Accept_ID)!=0)
#else
if(close(Accept_ID) != 0)
#endif
return CLOSE_SOCKET_FAILED;
}
else
return SOCKET_INEXISTENCE;
return 0;
}
// UCS2解码
// pSrc: 源编码串指针
// pDst: 目标字符串指针
// nSrcLength: 源编码串长度
// 返回: 目标字符串长度
int GsmDecodeUcs2(const unsigned char* pSrc, char* pDst, int nSrcLength)
{
int nDstLength; // UNICODE宽字符数目
WCHAR wchar[128]; // UNICODE串缓冲区
// 高低字节对调,拼成UNICODE
for(int i=0; i<nSrcLength/2; i++)
{
wchar[i] = *pSrc++ << 8; // 先高位字节
wchar[i] |= *pSrc++; // 后低位字节
}
nDstLength = ::WideCharToMultiByte(CP_ACP, 0, wchar, nSrcLength/2, pDst, 160, NULL, NULL);
// 输出字符串加个结束符
pDst[nDstLength] = '\0';
// 返回目标字符串长度
return nDstLength;
}
/////////////////////////////////////////////////////////////////////////////////////////
//Impletation of Class CSgipRecv
////////////////////////////////////////////////////////////////////////////////////////
const char DEFAULT_PROCFILE[] = "..\\config\\cmicp.ini" ;
const char DEFAULT_PROCNAME[] = "SgipInit" ;
CSgipRecv::CSgipRecv()
{
SetProcPara( DEFAULT_PROCFILE,DEFAULT_PROCNAME ) ;
}
void CSgipRecv::SetProcPara( const char *sProcfileName,const char *sProcName )
{
m_pcProfile = new Profile(sProcfileName) ;
strcpy( m_sProcName,sProcName ) ;
ReadInitFile( ) ;
delete m_pcProfile ;
}
void CSgipRecv::ReadInitFile( )
{
char sValue[ 20 ] ;
//get SrcTermID
GetProcValue( "SpCode" , m_sSpCode ) ;
//get ICP ID
GetProcValue( "IcpID" , m_sIcpId ) ;
//get WaitTime
GetProcValue( "WaitTime" , sValue ) ;
m_nWaitTime = atoi( sValue ) ;
//get ServerPort
GetProcValue( "ServerPort" , sValue ) ;
m_nServerPort = atoi( sValue ) ;
}
void CSgipRecv::GetProcValue( char *sEntry,char *sValue )
{
if ( 0 != m_pcProfile->Read( m_sProcName, sEntry, sValue ) )
{
g_clLog.Panic( 0, "Unable to get %s from profile !",sEntry ) ;
}
}
int CSgipRecv::BeginListen()
{
int nRet = -1 ;
while ( ( nRet = ICP_BeginListen( (int *)&m_nServerSocket,m_nServerPort ) ) != 0 )
{
g_clLog.Error( 0 ,"Begin listen failed : %d !", nRet );
Sleep( 1000 ) ;
}
return 0 ;
}
int CSgipRecv::EndListen()
{
int nRet = ICP_EndListen(m_nServerSocket);
if ( nRet != 0 )
{
g_clLog.Error( 0 ,"End listen failed : %d ", nRet);
}
return 0 ;
}
//return : >0 client socket
// 0 no request
// -1 error
int CSgipRecv::Accept( )
{
struct sockaddr_in client;
size_t size = sizeof(struct sockaddr_in);
struct timeval stWaitTime;
fd_set readfds,writefds,exceptfds;
FD_ZERO( &readfds ) ;
FD_ZERO( &writefds ) ;
FD_ZERO( &exceptfds ) ;
stWaitTime.tv_sec=m_nWaitTime ;
stWaitTime.tv_usec=0;
FD_SET( m_nServerSocket, &readfds );
FD_SET( m_nServerSocket, &writefds);
FD_SET( m_nServerSocket, &exceptfds);
int rc ,socket = 0 ;
rc = select( 0, &readfds, 0, &exceptfds , &stWaitTime );
if ( rc > 0 )
{
if ( FD_ISSET( m_nServerSocket,&readfds ) )
{
socket = accept(m_nServerSocket, (struct sockaddr*)&client, (int*)&size);
}
else
socket = -1 ;
}
if ( rc == SOCKET_ERROR )
{
socket = -1 ;
}
return socket ;
}
int CSgipRecv::GetDeliverMsg( int mo_socket,StruDeliver &stDeliver,int &command_id )
{
SGIP_MsgHead head;
int ret = recv_msghead(mo_socket, &head) ;
if ( ret != 0 )
{
g_clLog.DeBug( 0 ,"Receive message head failed : %d !", ret );
return ret ;
}
SGIP_MsgBody body;
initialize_msgbody(&body, SGIP_DELIVER);
ret = recv_msgbody(mo_socket, head.Total_Length - sizeof(head), &body, head.Command_ID) ;
if ( ret != 0 )
{
g_clLog.Error( 0 ,"Receive message body failed : %d !", ret);
return ret ;
}
command_id = head.Command_ID ;
switch (head.Command_ID)
{
case SGIP_BIND:
{//send bind response
g_clLog.DeBug( 0 ,"Receive Bind Success !" );
SGIP_MsgBody resp_body;
initialize_msgbody(&resp_body, head.Command_ID);
//memset( &resp_body.SGIP_Bind_Resp,0,sizeof(SGIP_Bind_Resp_Body) ) ;
resp_body.SGIP_Bind_Resp.Result = 0;
ret = sendmsg_withseq(mo_socket, &resp_body, head.Command_ID|0x80000000, head.Sequence_ID);
if ( ret < 0 )
g_clLog.Error( 0 ,"Send Bind response failed : %d !", ret);
}
break;
case SGIP_REPORT:
{//send reprot response
SGIP_MsgBody resport_resp;
initialize_msgbody(&resport_resp, head.Command_ID);
resport_resp.SGIP_Report_Resp.Result = 0;
ret = sendmsg_withseq(mo_socket, &resport_resp, head.Command_ID|0x80000000, head.Sequence_ID);
if ( ret < 0 )
g_clLog.Error( 0 ,"Send report response failed : %d !", ret);
}
break;
case SGIP_DELIVER :
{// Send Deliver Response
SGIP_MsgBody deliver_resp;
initialize_msgbody(&deliver_resp, head.Command_ID);
deliver_resp.SGIP_Deliver_Resp.Result = 0;
ret = sendmsg_withseq(mo_socket, &deliver_resp, head.Command_ID|0x80000000, head.Sequence_ID);
if ( ret < 0 )
g_clLog.Error( 0 ,"Send deliver response failed : %d !", ret);
}
break;
default:
g_clLog.Error( 0 , "Unknow MO command !");
break;
}
if( ret == 0 )
{
switch( command_id )
{
case SGIP_REPORT:
stDeliver.bReport = true ;
sprintf( stDeliver.stReport.message_id,"%u%u%u",
body.SGIP_Report.SubmitSequenceNumber[0],
body.SGIP_Report.SubmitSequenceNumber[1],
body.SGIP_Report.SubmitSequenceNumber[2] ) ;
if ( 0 == strncmp( body.SGIP_Report.UserNumber,"86",2) )//Ltrim "86"
strcpy( stDeliver.stReport.dest_nbr,body.SGIP_Report.UserNumber+2 ) ;
else
strcpy( stDeliver.stReport.dest_nbr,body.SGIP_Report.UserNumber ) ;
sprintf( stDeliver.stReport.state,"%u", body.SGIP_Report.State ) ;
stDeliver.stReport.ErrorCode = body.SGIP_Report.ErrorCode ;
break ;
case SGIP_DELIVER:
stDeliver.bReport = false ;
if ( 0 == strncmp( body.SGIP_Deliver.UserNumber,"86",2) )//Ltrim "86"
strcpy( stDeliver.stRequest.acct_nbr,body.SGIP_Deliver.UserNumber+2 ) ;
else
strcpy( stDeliver.stRequest.acct_nbr,body.SGIP_Deliver.UserNumber ) ;
strcpy( stDeliver.stRequest.sp_code,m_sSpCode ) ;
strcpy( stDeliver.stRequest.inf_type_id,body.SGIP_Deliver.SPNumber + strlen(m_sSpCode) ) ;
if ( body.SGIP_Deliver.MessageCoding == MSG_UCS_CODE )
{//decode
stDeliver.stRequest.msg_length = GsmDecodeUcs2( (unsigned char*)body.SGIP_Deliver.Msg_Content ,
stDeliver.stRequest.msg_content,
body.SGIP_Deliver.Msg_Length ) ;
}
else
{
strcpy( stDeliver.stRequest.msg_content,body.SGIP_Deliver.Msg_Content ) ;
stDeliver.stRequest.msg_length = body.SGIP_Deliver.Msg_Length ;
}
break;
default:
break ;
}
}
return ret ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -