📄 ipc.h
字号:
/* $Id: ipc.h,v 1.38 2005/02/09 01:45:05 gshi Exp $ *//* * ipc.h IPC abstraction data structures. * * author Xiaoxiang Liu <xiliu@ncsa.uiuc.edu>, * Alan Robertson <alanr@unix.sh> * * * Copyright (c) 2002 International Business Machines * Copyright (c) 2002 Xiaoxiang Liu <xiliu@ncsa.uiuc.edu> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */#ifndef _IPC_H_#define _IPC_H_#include <glib.h>#undef MIN#undef MAX#include <sys/types.h>#include <sys/poll.h>#ifdef HAVE_STRING_H#include <string.h>#endif#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif/* constants */#define DEFAULT_MAX_QLEN 40#define MAX_MESSAGE_SIZE 4096#define MAX_MSGPAD 128/* channel and connection status */#define IPC_CONNECT 1 /* Connected: can read, write */#define IPC_WAIT 2 /* Waiting for connection */#define IPC_DISCONNECT 3 /* Disconnected, can't read or write*/#define IPC_DISC_PENDING 4 /* Disconnected, can't write but */ /* may be more data to read */#define IPC_ISRCONN(ch) ((ch)->ch_status == IPC_CONNECT \ || (ch)->ch_status == IPC_DISC_PENDING)#define IPC_ISWCONN(ch) ((ch)->ch_status == IPC_CONNECT)/* general return values */#define IPC_OK 0#define IPC_FAIL 1#define IPC_BROKEN 2#define IPC_INTR 3/* * IPC: Sockets-like Interprocess Communication Abstraction * * We have two fundamental abstractions which we maintain. * Everything else is in support of these two abstractions. * * These two main abstractions are: * * IPC_WaitConnection: * A server-side abstraction for waiting for someone to connect. * * IPC_Channel: * An abstraction for an active communications channel. * * All the operations on these two abstractions are carried out * via function tables (channel->ops). Below we refer to the * function pointers in these tables as member functions. * * On the server side, everything starts up with a call to * ipc_wait_conn_constructor(), which returns an IPC_WaitConnection. * * Once the server has the IPC_WaitConnection object in hand, * it can give the result of the get_select_fd() member function * to poll or select to inform you when someone tries to connect. * * Once select tells you someone is trying to connect, you then * use the accept_connection() member function to accept * the connection. accept_connection() returns an IPC_Channel. * * With that, the server can talk to the client, and away they * go ;-) * * On the client side, everything starts up with a call to * ipc_channel_constructor() which we use to talk to the server. * The client is much easier ;-) */typedef struct IPC_WAIT_CONNECTION IPC_WaitConnection;typedef struct IPC_CHANNEL IPC_Channel;typedef struct IPC_MESSAGE IPC_Message;typedef struct IPC_QUEUE IPC_Queue;typedef struct IPC_AUTH IPC_Auth;typedef struct IPC_OPS IPC_Ops;typedef struct IPC_WAIT_OPS IPC_WaitOps;/* wait connection structure. */struct IPC_WAIT_CONNECTION{ int ch_status; /* wait conn. status.*/ void * ch_private; /* wait conn. private data. */ IPC_WaitOps *ops; /* wait conn. function table .*/};typedef void(*flow_callback_t)(IPC_Channel*, void*);/* channel structure.*/struct IPC_CHANNEL{ int ch_status; /* identify the status of channel.*/ pid_t farside_pid; /* far side pid */ void* ch_private; /* channel private data. */ /* (may contain conn. info.) */ IPC_Ops* ops; /* IPC_Channel function table.*/ /* number of bytes needed * at the begginging of <ipcmessage>->msg_body * it's for msg head needed to tranmit in wire */ unsigned int msgpad; /* the number of bytes remainng to send for the first message in send queue 0 means nothing has been sent thus all bytes needs to be send n != 0 means there are still n bytes needs to be sent */ unsigned int bytes_remaining; /* is the send blocking or nonblocking*/ gboolean should_send_blocking; /* There are two queues in channel. One is for sending and the other * is for receiving. * Those two queues are channel's internal queues. They should not be * accessed directly. */ /* private: */ IPC_Queue* send_queue; IPC_Queue* recv_queue; /* buffer pool for receive in this channel*/ struct ipc_bufpool* pool; /* the follwing is for send flow control*/ int high_flow_mark; int low_flow_mark; void* high_flow_userdata; void* low_flow_userdata; flow_callback_t high_flow_callback; flow_callback_t low_flow_callback; };struct IPC_QUEUE{ int current_qlen; /* Current qlen */ int max_qlen; /* Max allowed qlen */ GList* queue; /* List of messages */};/* authentication information : set of gids and uids */struct IPC_AUTH { GHashTable * uid; /* hash table for user id */ GHashTable * gid; /* hash table for group id */};/* Message structure. */struct IPC_MESSAGE{ size_t msg_len; void* msg_buf; void* msg_body;/* * IPC_MESSAGE::msg_done * the callback function pointer which can be called after this * message is sent, received or otherwise processed. * * Parameter: * msg: the back pointer to the message which contains this * function pointer. * */ void (* msg_done)(IPC_Message * msg); void* msg_private; /* the message private data. */ /* Belongs to message creator */ /* May be used by callback function. */ IPC_Channel * msg_ch; /* Channel the */ /* message is from/in */};struct IPC_WAIT_OPS{/* * IPC_WAIT_OPS::destroy * destroy the wait connection and free the memory space used by * this wait connection. * * Parameters: * wait_conn (IN): the pointer to the wait connection. * */ void (* destroy)(IPC_WaitConnection *wait_conn);/* * IPC_WAIT_OPS::get_select_fd * provide a fd which user can listen on for a new coming connection. * * Parameters: * wait_conn (IN) : the pointer to the wait connection which * we're supposed to return the file descriptor for * (the file descriptor can be used with poll too ;-)) * * Return values: * integer >= 0 : the select_fd. * -1 : can't get the select fd. * */ int (* get_select_fd)(IPC_WaitConnection *wait_conn);/* * IPC_WAIT_OPS::accept_connection * accept and create a new connection and verify the authentication. * * Parameters: * wait_conn (IN) : the waiting connection which will accept * create the new connection. * auth_info (IN) : the authentication information which will be * verified for the new connection. * * Return values: * the pointer to the new IPC channel; NULL if the creation or * authentication fails. * */ IPC_Channel * (* accept_connection) (IPC_WaitConnection * wait_conn, IPC_Auth *auth_info);};/* Standard IPC channel operations */struct IPC_OPS{/* * IPC_OPS::destroy * brief destroy the channel object. * * Parameters: * ch (IN) : the pointer to the channel which will be destroyed. * */ void (*destroy) (IPC_Channel * ch);/* * IPC_OPS::initiate_connection * used by service user side to set up a connection. * * Parameters: * ch (IN) : the pointer to channel used to initiate the connection. * * Return values: * IPC_OK : the channel set up the connection successfully. * IPC_FAIL : the connection initiation fails. * */ int (* initiate_connection) (IPC_Channel * ch);/* * IPC_OPS::verify_auth * used by either side to verify the identity of peer on connection. * * Parameters * ch (IN) : the pointer to the channel. * * Return values: * IPC_OK : the peer is trust. * IPC_FAIL : verifying authentication fails. */ int (* verify_auth) (IPC_Channel * ch, IPC_Auth* info);/* * IPC_OPS::assert_auth * service user asserts to be certain qualified service user. * * Parameters: * ch (IN): the active channel. * auth (IN): the hash table which contains the asserting information. * * Return values: * IPC_OK : assert the authentication successfully. * IPC_FAIL : assertion fails. * * NOTE: This operation is a bit obscure. It isn't needed with * UNIX domain sockets at all. The intent is that some kinds * of IPC (like FIFOs), do not have an intrinsic method to * authenticate themselves except through file permissions. * The idea is that you must tell it how to chown/grp your * FIFO so that the other side and see that if you can write * this, you can ONLY be the user/group they expect you to be. * But, I think the parameters may be wrong for this ;-) */ int (* assert_auth) (IPC_Channel * ch, GHashTable * auth);/* * IPC_OPS::send * send the message through the sending connection. * * Parameters: * ch (IN) : the channel which contains the connection. * msg (IN) : pointer to the sending message. User must * allocate the message space. * * Return values: * IPC_OK : the message was either sent out successfully or * appended to the send_queue. * IPC_FAIL : the send operation failed. * IPC_BROKEN : the channel is broken. **/ int (* send) (IPC_Channel * ch, IPC_Message* msg);/* * IPC_OPS::recv * receive the message through receving queue. * * Parameters: * ch (IN) : the channel which contains the connection. * msg (OUT): the IPC_MESSAGE** pointer which contains the pointer * to the received message or NULL if there is no * message available. * * Return values: * IPC_OK : receive operation is completed successfully. * IPC_FAIL : operation failed. * IPC_BROKEN : the channel is broken (disconnected) * * Note: * return value IPC_OK doesn't mean the message is already
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -