Test engineering needs to include the header file
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <sys/stat.h>
#include "mynet.h"
#include <time.h>
#include <assert.h>
#include <iostream>
#include <string>
#include "server.h"
using namespace std;
TestMain used to define global variables
#define LOG_NOTICE printf
#define LOG_ERROR printf
#define LOG_WARN printf
#define LOG_INFO printf
#define MAX_PATH 1024
#define _stricmp strcasecmp
pthread_mutex_t mutexSpeexDec;
pthread_mutex_t mutexaccept;
char opt_SysConfig[1024] = "./conf/server.conf";
int opt_SessionNum = 0;
int opt_ListenPort = 0;
char opt_SaveVoice[32] = {0};
char opt_ListenIP[32] ={0};
char hostIpTail[30] = "";
int opt_ListenQueue;
asr_param_t *tParam = NULL;
int *socketCli = NULL;
static int GetConfig(FILE *fp_config, char *para_name, char *para_value);
void setParam(FILE* fpini);
bool AddStr(char * a, char * b, int limit);
void handle_pipe(int sig)
{
printf( "[%s:%d] enter into handle_pipe.", __FILE__, __LINE__);
}
static void *testProcThread(void *parameter);
Add main validation logic, reads ini configuration file, for example:
username=testmain
savepath = / opt / stt / file /
SessionNumber=1
After reading the configuration, the user name and save SessionNumber read out the file path, the code used in the business logic
int main(int argc, char * argv[])
{
FILE* fpini = fopen(opt_SysConfig, "rt");
if(fpini == NULL)
{
LOG_ERROR("can not open config file %s", opt_SysConfig);
exit(-1);
}
setParam(fpini);
//added by Hou
pthread_mutex_init(&mutexSpeexDec, NULL);
int i = 1;
for (i=1; i<argc; i++)
{
if (_stricmp(argv[i], "-s") == 0)
opt_SessionNum = atoi(argv[++i])+1;
else if(_stricmp(argv[i], "-p") == 0)
opt_ListenPort = atoi(argv[++i]);
else if(_stricmp(argv[i], "-i") == 0)
strcpy(opt_ListenIP, argv[++i]);
else
;
}
//Get the tail of the ip address for reclog.
//GetIpTail(hostIpTail, opt_ListenIP);
//checkParam();//对配置参数进行异常保护
int ret = 0;
//打印参数设置-begin
LOG_NOTICE("System Parameters:\n");
LOG_NOTICE("LIsten ip=%s\n", opt_ListenIP);
LOG_NOTICE("LIsten port=%d\n", opt_ListenPort);
LOG_NOTICE("SessionNumber=%d\n", opt_SessionNum);
//打印参数设置-end
tParam = new asr_param_t[opt_SessionNum];
socketCli = new int[opt_SessionNum];
//*********************************************************************************//
//***在Linux下写socket的程序的时候,如果尝试send到一个disconnected socket上,就会让底层抛出一个SIGPIPE信号***//
//**这个信号的缺省处理方法是退出进程,大多数时候这都不是我们期望的。因此我们需要重载这个信号的处理方法。**//
LOG_NOTICE("after Search_Init \n");
struct sigaction action;
action.sa_handler = handle_pipe;
sigemptyset(&action.sa_mask); //初始化信号集合set,将set 设置为空
action.sa_flags = 0;
sigaction(SIGPIPE, &action, NULL);
int socksvr = 0;
socksvr = MyTcpListen(opt_ListenIP, opt_ListenPort, opt_ListenQueue);
printf("after MyTcpListen \n");
if(socksvr > 0)
printf( "create server socket ok.\n");
else
{
printf(" create server socket fail.\n");
exit(-1);
}
pthread_t* threadHandleArray = new pthread_t[opt_SessionNum];
pthread_attr_t threadAttr;
// initialize the thread attribute
pthread_attr_init(&threadAttr);
//Set the stack size of the thread
pthread_attr_setstacksize(&threadAttr, 1024*1024); // 120*1024
// Set thread to detached state. No need for pthread_join
ret=pthread_mutex_init(&mutexaccept, NULL);
for(int i=0; i<opt_SessionNum; i++)
{
tParam[i].sessId = i;
tParam[i].sockHandle = socksvr;
memcpy(tParam[i].hostIp , "127.0.0.1",10);
tParam[i].port = opt_ListenPort;
pthread_create(&(threadHandleArray[i]), &threadAttr, testProcThread, (void *) &tParam[i]);
printf( "After thread: i:%d,socketCli[i]:,socksvr:\n", i);
}
pthread_attr_destroy(&threadAttr);
for(i=0; i<opt_SessionNum; i++)
pthread_join(threadHandleArray[i], NULL);
free(threadHandleArray);
pthread_mutex_destroy(&mutexaccept);
pthread_mutex_destroy(&mutexSpeexDec);
if(socketCli != NULL) delete []socketCli;
if(tParam != NULL) delete []tParam;
LOG_NOTICE("programe end.\n");
return 0;
}
Reads the configuration file
#define NAME_LENGTH 2048
static int GetConfig(FILE *fp_config, char *para_name, char *para_value)
{
char line[NAME_LENGTH], *token, line_para[NAME_LENGTH];
rewind(fp_config);
while(fgets(line, NAME_LENGTH, fp_config))
{
line[strlen(line)-1] = 0;
if(line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0';
if(line[strlen(line)-1] == '\r') line[strlen(line)-1] = '\0';
token=strchr(line, ' '); //去掉所有的空格
while(token != NULL)
{
strcpy(token, token+1);
token = strchr(line, ' ');
}//while(token)
line_para[0] = 0;
token=strrchr(line, '=');
if(token != NULL)
{
*token = 0;
token++;
strcpy(line_para, token);
}//if(token != NULL)
if(strncmp(line, para_name, NAME_LENGTH) == 0)
{
strcpy(para_value, line_para);
return 0;
}//if(strcmp(line, para_name)==0)
}//while
return -1;
}//GetConfig()
/*变量设值*/
void setParam(FILE* fpini)
{
char tmpConfig[1024] = {0};
GetConfig(fpini, "ListenIP", opt_ListenIP);
if(strncmp(opt_ListenIP, "",MAX_PATH) == 0)
{
strcpy(opt_ListenIP, "0.0.0.0");
}
GetConfig(fpini, "ListenPort", tmpConfig);
opt_ListenPort = atoi(tmpConfig);
if(opt_ListenPort < 10000)
opt_ListenPort = 18780;
GetConfig(fpini, "SessionNumber", tmpConfig);
opt_SessionNum = atoi(tmpConfig);
}
bool AddStr(char * a, char * b, int limit){
int n = strlen(a), m = strlen(b);
if(n + m < limit){
strcat(a, b);
return true;
}else
return false;
}
Measured as Sample