📄 gatemain.c
字号:
{
/* Set gateChannels to minimum available channels */
if(gateChannels > IPTChannels) {
gateChannels = IPTChannels;
printf("\nSupports %d channels.\n",gateChannels);
}
if(gateChannels == 0) {
printf("No available channels in the system (D/xx or DM3 channels).\n");
gateExit(OPEN_ERROR, ALL_CHANNELS);
}
} /* Function getGateChannels */
/*****FUNCTION***************************************************
* NAME : enableKBInput
* DESCRIPTION : Create thread - waitForKey,
* enable detection of CTRL_CLOSE_EVENT
* INPUT : None
* OUTPUT : None
* RETURNS : void
* CAUTIONS : None
****************************************************************/
void enableKBInput()
{
DWORD threadRet;
/* Enable detect of CTRL_CLOSE_EVENT (closing the window).
If such event detected call X_routine function */
if(SetConsoleCtrlHandler((PHANDLER_ROUTINE)&(X_routine), /* Routine to handle the event */
TRUE) == 0) {
printf("error SetConsoleCtrlHandler 0x%x.\n",GetLastError());
}
/* Create thread, waiting for key to
exit application, send User Input Indication or send Non Standard Command */
if(CreateThread(NULL,
1024,
(LPTHREAD_START_ROUTINE)waitForKey, /* Thread start routine */
NULL,
0,
(LPDWORD)(&threadRet)) == NULL) {
printf("Unable to create thread.\n");
}
else {
printf("%s %s %s\n",
"Waiting for key:\n\t'Q' - to quit\n\t",
"'N' - for Non standard command (Active only in connected state)\n\t",
"'U' - for User Input Indication (Active only in connected state)\n");
}
}
/*****FUNCTION***************************************************
* NAME : printConfiguration
* DESCRIPTION : Print information from confoguration file
* INPUT : None
* OUTPUT : None
* RETURNS : void
* CAUTIONS : None
****************************************************************/
void printConfiguration()
{
USHORT index;
USHORT coderCount;
/* Print out the configuration information */
for(index=1;index<=gateChannels;index++) {
if(strlen(Session[index].ConfigFileParm.UII.Val) > 99) {
gateFATAL(index,(Session[index].LogFile,"UII string is too long, can be max 99 bytes."));
strncpy(Session[index].ConfigFileParm.UII.Val,Session[index].ConfigFileParm.UII.Val,99);
}
if(strlen(Session[index].ConfigFileParm.NonStdCmd.CmdVal) > 99) {
gateFATAL(index,(Session[index].LogFile,"Non Std Cmd string is too long, can be max 99 bytes."));
strncpy(Session[index].ConfigFileParm.NonStdCmd.CmdVal,Session[index].ConfigFileParm.NonStdCmd.CmdVal,99);
}
printf("Session %d: Destination %s\n\t PhoneList %s\n\t LocalPhoneNumber %s\n\t",
index,
Session[index].ConfigFileParm.destAddr,
Session[index].ConfigFileParm.phoneList,
Session[index].ConfigFileParm.localPhoneNumber);
for(coderCount = 0; coderCount < Session[index].ConfigFileParm.maxTxCoders; coderCount++) {
printf(" Coder[%d]\n\t\t Type %d\n\t\t FramesPerPacket %d\n\t\t FrameSize %d\n\t\t Rate %d\n\t\t VAD %d \n\t",
coderCount,
Session[index].ConfigFileParm.TxCoder[coderCount].CoderType,
Session[index].ConfigFileParm.TxCoder[coderCount].FramesPerPkt,
Session[index].ConfigFileParm.TxCoder[coderCount].FrameSize,
Session[index].ConfigFileParm.TxCoder[coderCount].Rate,
Session[index].ConfigFileParm.TxCoder[coderCount].VadEnable);
}
printf(" Display %s\n\t UUI %s\n\t UII %s\n\t NonStdCmd %s\n\t",
Session[index].ConfigFileParm.display,
Session[index].ConfigFileParm.IPT_UUI,
Session[index].ConfigFileParm.UII.Val,
Session[index].ConfigFileParm.NonStdCmd.CmdVal);
}
}
/*****FUNCTION***************************************************
* NAME : gateInit
* DESCRIPTION : Initializes the Camel Gateway demo:
* gets argument from command line,
* opens NetTSC instances,
* gets information from configuration file,
* opens VOX channels.
* INPUT : int argc
* char *argv[]
* OUTPUT : None
* RETURNS : void
* CAUTIONS : None
****************************************************************/
void gateInit(int argc,
char *argv[])
{
int index;
USHORT VOXChannels=0;
/* Checks list of arguments from command line */
checkArg(argc,argv);
/* IO completion stuff */
completionPorts();
#if 0
/* Get the number of DM3 boards in the system */
if(IPTGetNumBoards(&NumOfBoards) == DM3FAIL) {
exit(0);
}
#else
NumOfBoards = 1;
#endif
/* Check number of avialable vox channels */
VOXChannels = pstnGetVOXChannels();
for(index=0;((index<= GATE_MAX_SESSIONS) && (index <= VOXChannels));index++) {
IPTResetSession(index,TRUE);
}
if(gateChannels == 0) {
gateChannels = VOXChannels;
}
/* Init NetTSC cluster fields */
ClusterInit(&IPTChannels,VOXChannels);
/* Get gateChannels number, which is the minimum
of VOXChannels and NetTSC availble clusters */
getGateChannels();
/* Init NetTSC component instances fields,
attach DM3 key to I/O Completeion port
and detect events for each instance */
if(NetTSCCompInit() == DM3FAIL) {
gateExit(OPEN_ERROR, ALL_CHANNELS);
}
/* Configure the demo */
if(gateConfiguration(cfgFile) != DM3SUCCESS) {
printf("\nError in configuration file\n");
gateExit(CFG_ERROR,ALL_CHANNELS);
}
printConfiguration();
/* Initialize the front end */
if( pstnOpenFrontEnd(gateChannels) == DM3FAIL) {
printf("Open pstn fail.\n");
gateExit(CFG_ERROR, ALL_CHANNELS);
}
//使用同步停止通道!没用!!!
//for(index=1;index<= VOXChannels;index++) {
// if (dx_stopch(index, EV_SYNC)==-1) {
// printf("Error in stop channel %d !\n",index);
// }
//}
/* Create thread - waitForKey,
enable detection of CTRL_CLOSE_EVENT */
enableKBInput();
} /* Function gateInit */
/*****FUNCTION***************************************************
* NAME : X_routine
* DESCRIPTION : Routine to handle CTRL_CLOSE_EVENT (closing window)
* INPUT : DWORD crtlType - Event type
* OUTPUT : None
* RETURNS : TRUE - success, FALSE - fail
* CAUTIONS : None
****************************************************************/
BOOL X_routine(DWORD crtlType)
{
OVERLAPPED Overlapped;
BOOL rc;
if(crtlType == CTRL_CLOSE_EVENT) {
printf("got close evt.\n");
quitFlag = TRUE;
/* Post END_KEY to completion port, to end application */
rc = PostQueuedCompletionStatus(hIOCP,
1,
END_KEY,
&Overlapped);
if (rc == FALSE) {
printf("Last error in X_routine 0x%x.\n", GetLastError());
exit(0);
}
}
return(DM3SUCCESS);
}
/*****FUNCTION***************************************************
* NAME : gateGetEvent
* DESCRIPTION : Gets Next Event from IO completion port
* INPUT : DWORD timeout - Timeout to wait for event
* OUTPUT : None
* RETURNS : Success or fail
* CAUTIONS : None
****************************************************************/
USHORT gateGetEvent(DWORD timeout)
{
DWORD dwBytesTransferred;
DWORD dwCompletionKey;
DWORD dwStatus;
DWORD errorCode = NO_ERROR; /* Always set the LastError to NO_ERROR */
LPOVERLAPPED pOverlapped;
BOOL rBool;
/* Wait forever for event from IO completion port */
dwStatus = GetQueuedCompletionStatus(hIOCP,
&dwBytesTransferred,
&dwCompletionKey,
&pOverlapped,
timeout);
/* An error occured */
if(dwStatus == FALSE) {
errorCode = GetLastError();
/* The I/O operation was aborted due to
either thread exit or application request */
if(errorCode == ERROR_OPERATION_ABORTED) {
/* perform cleanup */
return(ERROR_OPERATION_ABORTED);
}
/* Overlapped erros */
if(pOverlapped == NULL) {
/* Error: Failed to dequeue an I/O completion packet */
printf("Failed to dequeue in IO completion port\n");
return(DM3FAIL);
}
/* The semaphore timeout period (for MNTI) has expired
Send another empty message to recieve DM3 events */
if(errorCode == ERROR_SEM_TIMEOUT) {
Dm3CompProcIoCompletion(pOverlapped, errorCode);
return(ERROR_SEM_TIMEOUT);
}
return(DM3FAIL);
}
/* Success: we have a valid overlapped + completion key */
switch(dwCompletionKey)
{
/* Terminated by the User (by crtl+c, 'Q' or by closing the application window ) */
case END_KEY:
printf("%s","\tExiting application\n");
exit(0);
break;
/* The SRL key for SRL events */
case SRL_KEY:
/* Get the SRL event */
rBool = pstnGetEvent();
if(rBool==DM3FAIL) {
printf("\tReturn error getting telephone line event\n");
}
break;
/* The DM3_IPT_KEY key for DM3 events */
case DM3_IPT_KEY:
Dm3CompProcIoCompletion(pOverlapped, errorCode);
break;
default:
printf("\tUndefined IOCP key:[%d]\n",dwCompletionKey);
return(DM3FAIL);
} /* end switch on type of event (SRL or DM3IPT or END) */
return(DM3SUCCESS);
} /* Function gateGetEvent */
/****FUNCTION***************************************************
* NAME : main
* DESCRIPTION : Our main function
* The main loop waiting for events
* INPUT : int argc - Number of arguments from command line
* char *argv[] - Array of arguments from command line
* OUTPUT : None
* RETURNS : void
* CAUTIONS : None
****************************************************************/
void main(int argc,
char *argv[])
{
USHORT rc;
/* Print banner titles */
banner();
//数据库连接初始化
database_init();
/* General initialization */
gateInit(argc,argv);
/* spin waiting for Events */
while(1) {
/* Poll for any Gateway Event */
rc = gateGetEvent(INFINITE);
/* Check return code */
if(rc != DM3SUCCESS) {
/* If the return code isn't semaphore timeout -
end the application */
if(rc != ERROR_SEM_TIMEOUT) {
exit(0);
}
}
} /* while 1 */
} /* Function main */
/*** End of File ***/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -