cdrserver.cpp

来自「MySQL数据库开发源码 值得一看哦」· C++ 代码 · 共 1,492 行 · 第 1/5 页

CPP
1,492
字号
                          m2log(fi,"Waiting for connection");                       /* Note that addrlen is passed as a pointer      */                       /*  so that the accept call can return the       */                       /*  size of the returned address.                */                       addrlen = sizeof(struct sockaddr_in);                       /* This call will block until a new              */                       /*   connection arrives. Then, it will           */                       /*   return the address of the connecting        */                       /*   peer, and a new socket descriptor, s,       */                       /*   for that connection.                        */                        s = accept(ls,(struct sockaddr*) &peeraddr_in, &addrlen);		       #ifdef MYDEBUG			   puts("accepted");		       #endif			if ((checkchangelog(fi,temp))==0)                          m2log(fi,"Connection attempt from a client");			if ((checkchangelog(fi,temp))==0)                          m2log(fi,"Start communication server");                        if ( s == -1) exit(EXIT_FAILURE);                        switch (fork()) {                           case -1: /* Can't fork, just exit. */				if ((checkchangelog(fi,temp))==0)                                  m2log(fi,"Start communication server failed.");                                exit(EXIT_FAILURE);			   break;                           case 0: /* Child process comes here. */				/* Get clients adress and save it in the info area */				/* Keep track of how many times the client connects to the server */				printf("Connect attempt from client %u\n",peeraddr_in.sin_addr.s_addr);                                server(peeraddr_in.sin_addr.s_addr);                                exit(EXIT_FAILURE);			   break;                           default: /* Daemon process comes here. */                                /* The daemon needs to remember         */                                /*     to close the new accept socket   */                                /*     after forking the child. This    */                                /*     prevents the daemon from running */                                /*     out of file descriptor space. It */                                /*     also means that when the server  */                                /*     closes the socket, that it will  */                                /*     allow the socket to be destroyed */                                /*     since it will be the last close. */                                close(s);			   break;                        }               }           default: /* Parent process comes here. */               exit(EXIT_FAILURE);       }  return EXIT_SUCCESS;}/*----------------------------------------------------------------------    S E R V E R    * This is the actual server routine that the daemon forks to      handle each individual connection. Its purpose is to receive      the request packets from the remote client, process them,      and return the results to the client. It will also write some      logging information to stdout.  ----------------------------------------------------------------------*/server(long int servernum){	/******** NDB ***********/  	Ndb                   MyNdb( "TEST_DB" );  	int                   tTableId;	NdbConnection		*MyTransaction;	NdbOperation		*MyOperation;	int			check;	int			c1 = 0;	int			c2 = 0;	int			c3 = 0;	int			c4 = 0;	int			act_index = 0;	/************************/        register unsigned int   reqcnt;         /* keeps count of number of requests */        register unsigned int   i;          	/* Loop counters */	register int		x;        register short          done;           /* Loop variable */	short int		found;	/* The server index number */	int 			thisServer;	/* Variables used to keep track of some statistics */	time_t			ourtime;	time_t			tmptime;	int 			tmpvalue;	long int		tmptransfer;	long int		transfer;	int			ops = 0;	/* Variables used by the server */        char    		buf[400];       /* This example uses 10 byte messages. */        char    		*inet_ntoa();        char    		*hostname;      /* points to the remote host's name string */        int     		len;        int     		rcvbuf_size;	long			ctid;        unsigned char 		uc;	/* Variables used by the logging facilitiy */        char    		msg[600];        char    		crap[600];        char    		lognamn[600];        FILE    		*log;	/* scheduling parameter for pthread */	struct sched_param 	param1,param2,param3;        /* Header information */	/* cdrtype not used */        /*short           	cdrtype;   */   /* 1 CDR Typ                                            */        short           	cdrlen;         /* 2 CDR recored length in bytes excluding CDR type     */        short           	cdrsubtype;     /* 1 CDR subtype                                        */        unsigned int    	cdrid;          /* 8 CDR unique number of each call                     */        unsigned int    	cdrtime;        /* 4 CDR Time in seconds                                */        short           	cdrmillisec;    /* 2 CDR Milliseconds                                   */        short           	cdrstatus;      /* 1 CDR For future use                                 */        short           	cdrequipeid;    /* 1 CDR Equipment id                                   */        int             	cdrreserved1;   /* 4 CDR For future use                                 */        /* Defined or calculated for each record */        int             	cdrrestlen;     /*   Unprocessed data left in record in bytes   */        /* Gemensamma datatyper */        unsigned short  	parmtype_prev;  /* 1 Parameter type                                     */        unsigned short  	parmtype;       /* 1 Parameter type                                     */        unsigned short  	parmlen;        /* 1 Parameter type                                     */	int			rc;		/* return code for functions */		/* Attribute object used with threads */	pthread_attr_t 		attr1;	pthread_attr_t 		attr2;	pthread_attr_t 		attr3;	struct cdr_record	*tmpcdrptr,*ftest;	void			*dat;	int			error_from_client = 0;        /* Konstanter           */        const int       	headerlen = 24;         /*   Length of header record                    */        parmtype_prev = 99;	reqcnt = 0;        /* Close the listen socket inherited from the daemon. */        close(ls);	printf("Use the readinfo program to get information about server status\n\n");	if((checkchangelog(fi,temp))==0)          c2log(fi,"Communication server started");        /* Look up the host information for the remote host     */        /* that we have connected with. Its internet address    */        /* was returned by the accept call, in the main         */        /* daemon loop above.                                   */        hp=gethostbyaddr((char *) &peeraddr_in.sin_addr,sizeof(struct in_addr),peeraddr_in.sin_family);        if (hp == NULL) {                /* The information is unavailable for the remote        */                /* host. Just format its internet address to be         */                /* printed out in the logging information. The          */                /* address will be shown in "internet dot format".      */		/*                hostname = inet_ntoa(peeraddr_in.sin_addr);		*/		sprintf(hostname,"Test");                logname(lognamn,"Cdrserver","Child",hostname);        }        else {                hostname = hp->h_name; /* point to host's name */                logname(lognamn,"Cdrserver","Child",hostname);        }        log=fopen(lognamn,"w");	if (log == NULL)	{		perror(hostname);		exit(EXIT_FAILURE);	}        n2log(log,"Setup in progress");        /* Log a startup message. */        /* The port number must be converted first to host byte */        /* order before printing. On most hosts, this is not    */        /* necessary, but the ntohs() call is included here so  */        /* that this program could easily be ported to a host   */        /* that does require it.                                */        BaseString::snprintf(msg,sizeof(msg),"Startup from %s port %u",hostname,ntohs(peeraddr_in.sin_port));	if ((checkchangelog(fi,temp))==0)          c2log(fi,msg);        n2log(log,msg);        BaseString::snprintf(msg,sizeof(msg),"For further information, see log(%s)",lognamn);	if ((checkchangelog(fi,temp))==0)          c2log(fi,msg);        /* Set the socket for a lingering, graceful close.              */        /* This will cause a final close of this socket to wait until   */        /* all * data sent on it has been received by the remote host.  */        linger.l_onoff  =1;        linger.l_linger =0;        if (setsockopt(s, SOL_SOCKET, SO_LINGER,(const char*)&linger,sizeof(linger)) == -1) {                BaseString::snprintf(msg,sizeof(msg),"Setting SO_LINGER, l_onoff=%d, l_linger=%d",linger.l_onoff,linger.l_linger);		if ((checkchangelog(log,lognamn))==0)          		n2log(log,msg);                goto errout;        }        /* Set the socket for a lingering, graceful close.                              */        /* This will cause a final close of this socket to wait until all * data sent   */        /* on it has been received by the remote host.                                  */        rcvbuf_size=64*1024;        if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,(const char*) &rcvbuf_size,sizeof(rcvbuf_size)) == -1) {                BaseString::snprintf(msg,sizeof(msg),"Setting SO_RCVBUF = %d",rcvbuf_size);		if ((checkchangelog(log,lognamn))==0)          		n2log(log,msg);                goto errout;        }        /* Set nodelay on socket */        n2log(log,"Port setup complete");        /* Go into a loop, receiving requests from the remote           */        /* client. After the client has sent the last request,          */        /* it will do a shutdown for sending, which will cause          */        /* an end-of-file condition to appear on this end of the        */        /* connection. After all of the client's requests have          */        /* been received, the next recv call will return zero           */        /* bytes, signalling an end-of-file condition. This is          */        /* how the server will know that no more requests will          */        /* follow, and the loop will be exited.                         */        n2log(log,"Setup completed");	/* Fetch the process id for the server */	/* Inititate the variables used for counting transfer rates and rec/sec */	tmpvalue    = 0;	tmptime     = 0;	tmptransfer = 0;	transfer = 0;	printf("Client %s connected\nStarting to process the data\n\n",hostname);	tmpcdrptr = (struct cdr_record*)malloc(sizeof(struct cdr_record));	/***** NDB ******/	MyNdb.init();	if (MyNdb.waitUntilReady(30) != 0)	{		puts("Not ready");		exit(-1);	}	tTableId = MyNdb.getTable()->openTable(tableName);      	if (tTableId == -1) 	{		printf("%d: Creating table",getpid());        	create_table(&MyNdb);	}		else printf("%d: Table already created",getpid());      	/****************/        while (len = recv(s,buf,headerlen,MSG_WAITALL)) {                if (len == -1) {        		snprintf(msg,sizeof(msg),"Error from recv");			if ((checkchangelog(log,lognamn))==0)          			n2log(log,msg);                        goto errout; /* error from recv */                }                /* The reason this while loop exists is that there              */                /* is a remote possibility of the above recv returning          */                /* less than 10 bytes. This is because a recv returns           */                /* as soon as there is some data, and will not wait for         */                /* all of the requested data to arrive. Since 10 bytes          */                /* is relatively small compared to the allowed TCP              */                /* packet sizes, a partial receive is unlikely. If              */                /* this example had used 2048 bytes requests instead,           */                /* a partial receive would be far more likely.                  */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?