IP Phone Log 3

xiaoxiao2021-03-06  105

Today, modify the queue code into an operable code (this code is impending photos, Linux program design >> modified) ************************************ ************************** COMMMAIN.C

#include #include #include #include #include #include # Include #include

#include "msg_queue.h"

INT SAVE_ERRNO; Static Int Server_Running = 1;

Static void process_command (const ssismsg message_command);

Void catch_signals () {server_running = 0;

INT command (int Argc, char * argv []) {ssipmsg mess_command;

if exit (EXIT_FAILURE); while (server_running) {if (read_request_from_client (& mess_command)) {process_command (mess_command); (server_starting ()!)} else {if (server_running) fprintf (stderr, "Server ended - can not / read pipe / N "); server_running = 0;}} / * while * / server_ending (); exit_success);

/ * Any Client Messages Are Fed to The Process_Command Function, WHERE The Fed Into a Case Statement That Makes The Appropriate Calls To CD_DBM.c. * /

Static void process_command (const ssipmsg comm) {ssipmsg resp; int first_time = 1;

Resp = Comm; / * Copy Command Back, Then Change Res request * /

Save_errno = 0;

switch (resp.request) {case sip_Invite: break; case sip_Ack: break; case sip_Cancel: break; case sip_Options: break; case sip_Bye: break; case sip_Register: break; default: break;} / * switch * / if (! Send_resp_to_client (resp) {fprintf (stderr, "server warning: - / failed to respond to% d / n", resp.ModuleId);

Return;}

*********************** "MSG_Queue.h" #ifndef _base_define_h # define _base_define_h

TypedEf Enum {sip_invite = 0, SIP_ACK, SIP_CANCEL, SIP_OPTIONS, SIP_BYE, SIP_REGISTER,} Client_Request_e;

Typedef strunt {// pid_t client_pid; long moduleid; // module id client_request_e request; char message [100];} ssipmsg;

TypeDefstruct {long msg_key; // type of message ssipmsg sipmsg; // message text * /} smsgbuf;

INT server_starting (void); void server_ending (void); int in_request_from_client (SSIPMSG * REC_PTR); int success_resp_to_client (const ssipmsg message_to_send);

#ndif // _base_define_h ****************************************************** ************************************************************************************************************************TION #include #include

#include "msg_queue.h"

#define server_mqueue 1234 # Define Client_Mqueue 4321

/ * Two variables with file scope hold the two queue Identifier Returned from the msgget function. * /

Static int serv_qid = -1; static int CLI_QID = -1;

/ * WE Make The Server Responsible for Creating Both Message Queues. * /

INT server_starting () {#if debug_trace printf ("% d: - server_starting () / n", getPid ()); #ENDIFSERV_QID = MSGGET ((key_t) server_mqueue, 0666 | ipc_creat); if (serv_qid == -1) Return (0);

CLI_QID = msgget (key_t) Client_Mqueue, 0666 | IPC_CREAT; if (CLI_QID == -1) Return (0);

Return (1);

/ * The server is also responsible for tidying up if it ever exits. When the server ends, we set our file scope variables to illegal values. This will catch any bugs if the server attempts to send messages after it has called server_ending. * /

Void server_ending () {#if debug_trace printf ("% d: - server_ending () / n", getpid ()); #ENDIF

(void) MSGCTL (Serv_qID, IPC_rmid, 0); (void) MSGCTL (CLI_QID, IPC_RMID, 0);

Serv_qid = -1; CLI_QID = -1;}

/ * The Server Read Function Reads a Message of Any Type (That IS, from any client) from the Queue, And Returns The Data Part (Ignoring The Type) of the message. * /

INT read_request_from_client (SSIPMSG * REC_PTR) {struct smsgbuf my_msg; #if debug_trace printf ("% d: - read_request_from_client () / n", get pid ()); #ENDIF

IF (MSGRCV (SERV_QID, (VOID *) & MY_MSG, SIZEOF (* REC_PTR), 0, 0) == -1) {RETURN (0);} * REC_PTR = My_Msg.SIPMSG; Return (1);}

/ * Sending a response uses the client process id thing was stored in the request to address the message. * /

INT sent_resp_to_client (const SSIPMSG Mess_to_send) {struct smsgbuf my_msg; #if debug_trace printf ("% d: - send_resp_to_client () / n", get pid ()); #ENDIF

MY_MSG.SIPMSG = Mess_to_send; my_msg.msg_key = message_to_send.moduleId

转载请注明原文地址:https://www.9cbs.com/read-96265.html

New Post(0)