软件工程

本类阅读TOP10

·PHP4 + MYSQL + APACHE 在 WIN 系统下的安装、配置
·Linux 入门常用命令(1)
·Linux 入门常用命令(2)
·使用 DCPROMO/FORCEREMOVAL 命令强制将 Active Directory 域控制器降级
·DirectShow学习(八): CBaseRender类及相应Pin类的源代码分析
·基于ICE方式SIP信令穿透Symmetric NAT技术研究
·Windows 2003网络负载均衡的实现
·一网打尽Win十四种系统故障解决方法
·数百种 Windows 软件的免费替代品列表
·收藏---行百里半九十

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
IP phone日志3

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

今天把队列代码修改成可操作代码(这个代码是仿照<<linux程序设计>>修改的)

*********************************************CommMain.c

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "Msg_queue.h"

int save_errno;
static int server_running = 1;

static void process_command(const sSIPMsg mess_command);

void catch_signals()
{
    server_running = 0;
}

 

int CommMain(int argc, char *argv[]) {
    
    sSIPMsg mess_command;

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

/*
  Any client messages are fed to the process_command function, where they are 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 resp as required */

    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 struct {
 //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 read_request_from_client(sSIPMsg *rec_ptr);
int send_resp_to_client(const sSIPMsg mess_to_send);

#endif // _BASE_DEFINE_H
*******************************************************

Msg_queue.c
***********************************

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#include "msg_queue.h"

#define SERVER_MQUEUE 1234
#define CLIENT_MQUEUE 4321

 

/* Two variables with file scope hold the two queue identifiers 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());
    #endif

    serv_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",  getpid());
    #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 that was stored in the request to address
 the message. */

int send_resp_to_client(const sSIPMsg mess_to_send)
{
    struct sMsgBuf my_msg;
    #if DEBUG_TRACE
        printf("%d :- send_resp_to_client()\n",  getpid());
    #endif

    my_msg.SipMsg = mess_to_send;
    my_msg.msg_key = mess_to_send.ModuleID

    if (msgsnd(cli_qid, (void *)&my_msg, sizeof(mess_to_send), 0) == -1) {
        return(0);
    }
    return(1);
}
*****************************************
这样就完成了一个简单的消息泵.




相关文章

相关软件