00001 /* -*- C++ -*- */ 00002 00003 //============================================================================= 00004 /** 00005 * @file MEM_IO.h 00006 * 00007 * $Id: MEM_IO.h,v 1.1.1.4 2003/02/21 18:36:32 chad Exp $ 00008 * 00009 * @author Nanbor Wang <nanbor@cs.wustl.edu> 00010 */ 00011 //============================================================================= 00012 00013 00014 #ifndef ACE_MEM_IO_H 00015 #define ACE_MEM_IO_H 00016 #include "ace/pre.h" 00017 00018 #include "ace/SOCK.h" 00019 #include "ace/MEM_SAP.h" 00020 #include "ace/Memory_Pool.h" 00021 #include "ace/Message_Block.h" 00022 #include "ace/Process_Semaphore.h" 00023 #include "ace/Process_Mutex.h" 00024 00025 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00026 # pragma once 00027 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00028 00029 #if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1) 00030 00031 class ACE_Export ACE_Reactive_MEM_IO : public ACE_MEM_SAP 00032 { 00033 public: 00034 ACE_Reactive_MEM_IO (void); 00035 00036 virtual ~ACE_Reactive_MEM_IO (void); 00037 00038 /** 00039 * Initialize the MEM_SAP object. 00040 */ 00041 virtual int init (ACE_HANDLE handle, 00042 const ACE_TCHAR *name, 00043 MALLOC_OPTIONS *options); 00044 00045 /** 00046 * Fetch location of next available data into <recv_buffer_>. 00047 * As this operation read the address of the data off the socket 00048 * using ACE::recv, <timeout> only applies to ACE::recv. 00049 */ 00050 virtual ssize_t recv_buf (ACE_MEM_SAP_Node *&buf, 00051 int flags, 00052 const ACE_Time_Value *timeout); 00053 00054 /** 00055 * Wait to to <timeout> amount of time to send <buf>. If <send> 00056 * times out a -1 is returned with <errno == ETIME>. If it succeeds 00057 * the number of bytes sent is returned. */ 00058 virtual ssize_t send_buf (ACE_MEM_SAP_Node *buf, 00059 int flags, 00060 const ACE_Time_Value *timeout); 00061 00062 /** 00063 * Convert the buffer offset <off> to absolute address to <buf>. 00064 * Return the size of valid information containing in the <buf>, 00065 * -1 if <shm_malloc_> is not initialized. 00066 */ 00067 ssize_t get_buf_len (const off_t off, ACE_MEM_SAP_Node *&buf); 00068 }; 00069 00070 #if defined (ACE_WIN32) || !defined (_ACE_USE_SV_SEM) 00071 class ACE_Export ACE_MT_MEM_IO : public ACE_MEM_SAP 00072 { 00073 public: 00074 typedef struct 00075 { 00076 ACE_MEM_SAP_Node::ACE_MEM_SAP_NODE_PTR head_; 00077 ACE_MEM_SAP_Node::ACE_MEM_SAP_NODE_PTR tail_; 00078 } MQ_Struct; // Structure for a simple queue 00079 00080 class Simple_Queue 00081 { 00082 public: 00083 ACE_INLINE_FOR_GNUC Simple_Queue (void); 00084 Simple_Queue (MQ_Struct *mq); 00085 00086 int init (MQ_Struct *mq, ACE_MEM_SAP::MALLOC_TYPE *malloc); 00087 00088 int write (ACE_MEM_SAP_Node *new_msg); 00089 00090 ACE_MEM_SAP_Node *read (void); 00091 private: 00092 MQ_Struct *mq_; 00093 ACE_MEM_SAP::MALLOC_TYPE *malloc_; 00094 }; 00095 00096 typedef struct 00097 { 00098 ACE_SYNCH_PROCESS_SEMAPHORE *sema_; 00099 ACE_SYNCH_PROCESS_MUTEX *lock_; 00100 Simple_Queue queue_; 00101 } Channel; 00102 00103 ACE_MT_MEM_IO (void); 00104 00105 virtual ~ACE_MT_MEM_IO (void); 00106 00107 /** 00108 * Initialize the MEM_SAP object. 00109 */ 00110 virtual int init (ACE_HANDLE handle, 00111 const ACE_TCHAR *name, 00112 MALLOC_OPTIONS *options); 00113 00114 /** 00115 * Fetch location of next available data into <recv_buffer_>. 00116 * As this operation read the address of the data off the socket 00117 * using ACE::recv, <timeout> only applies to ACE::recv. 00118 */ 00119 virtual ssize_t recv_buf (ACE_MEM_SAP_Node *&buf, 00120 int flags, 00121 const ACE_Time_Value *timeout); 00122 00123 /** 00124 * Wait to to <timeout> amount of time to send <buf>. If <send> 00125 * times out a -1 is returned with <errno == ETIME>. If it succeeds 00126 * the number of bytes sent is returned. */ 00127 virtual ssize_t send_buf (ACE_MEM_SAP_Node *buf, 00128 int flags, 00129 const ACE_Time_Value *timeout); 00130 00131 private: 00132 Channel recv_channel_; 00133 Channel send_channel_; 00134 }; 00135 #endif /* ACE_WIN32 || !_ACE_USE_SV_SEM */ 00136 00137 /** 00138 * @class ACE_MEM_IO 00139 * 00140 * @brief Defines the methods for the ACE shared memeory wrapper I/O 00141 * routines (e.g., send/recv). 00142 * The shared memory transport uses ACE_SOCK_* class to 00143 * implement the signaling mechanism so we can easily use the 00144 * new mechanism with the Reactor pattern (which uses select 00145 * under the hood.) 00146 * ACE_MEM_Acceptor and ACE_MEM_Connector are used to establish 00147 * connections. When a connection is established, 00148 * ACE_MEM_Acceptor creates the MMAP file for data exchange and 00149 * sends the location of the file (complete path name) to 00150 * ACE_MEM_Connector thru the socket. ACE_MEM_Connector then 00151 * reads the location of the file off the socket and opens up 00152 * the same MMAP file. ACE_MEM_Stream at each side then 00153 * contains a reference to the ACE_Mallo object using the same 00154 * MMAP file. 00155 * When sending information using methods provided in this 00156 * class, ACE_MEM_IO requests a chunk of memory from the 00157 * MALLOC_TYPE object, copy the data into the shared memory and 00158 * send the memory offset (from the start of the ACE_Malloc) 00159 * across the socket. This action also servers as a signal to 00160 * the other end. The receiving side then reverses the 00161 * procedures and copies the information into user buffer. 00162 */ 00163 class ACE_Export ACE_MEM_IO : public ACE_SOCK 00164 { 00165 public: 00166 // = Initialization and termination methods. 00167 /// Constructor. 00168 ACE_MEM_IO (void); 00169 00170 /// Destructor. 00171 ~ACE_MEM_IO (void); 00172 00173 typedef enum 00174 { 00175 Reactive, 00176 MT 00177 } Signal_Strategy; 00178 00179 /** 00180 * Initialize the MEM_SAP object. 00181 */ 00182 int init (const ACE_TCHAR *name, 00183 ACE_MEM_IO::Signal_Strategy type = ACE_MEM_IO::Reactive, 00184 ACE_MEM_SAP::MALLOC_OPTIONS *options = 0); 00185 00186 /** 00187 * Finalizing the MEM_IO object. This method doesn't invoke 00188 * the <remove> method. 00189 */ 00190 int fini (void); 00191 00192 /// Send an <n> byte buffer to the other process using shm_malloc_ 00193 /// connected thru the socket. 00194 ssize_t send (const void *buf, 00195 size_t n, 00196 int flags) ; 00197 00198 /// Recv an <n> byte buffer from the shm_malloc_ thru connected socket. 00199 ssize_t recv (void *buf, 00200 size_t n, 00201 int flags) ; 00202 00203 /// Send an <n> byte buffer to the other process using shm_malloc_ 00204 /// connected thru the socket. 00205 ssize_t send (const void *buf, 00206 size_t n) ; 00207 00208 /// Recv an <n> byte buffer from the shm_malloc_ thru connected socket. 00209 ssize_t recv (void *buf, 00210 size_t n) ; 00211 00212 /** 00213 * Wait to to <timeout> amount of time to send up to <n> bytes into 00214 * <buf> from <handle> (uses the <send> call). If <send> times out 00215 * a -1 is returned with <errno == ETIME>. If it succeeds the 00216 * number of bytes sent is returned. 00217 */ 00218 ssize_t send (const void *buf, 00219 size_t n, 00220 const ACE_Time_Value *timeout); 00221 00222 /** 00223 * Wait to to <timeout> amount of time to send up to <n> bytes into 00224 * <buf> from <handle> (uses the <send> call). If <send> times out 00225 * a -1 is returned with <errno == ETIME>. If it succeeds the 00226 * number of bytes sent is returned. 00227 */ 00228 ssize_t send (const void *buf, 00229 size_t n, 00230 int flags, 00231 const ACE_Time_Value *timeout); 00232 00233 /** 00234 * Wait to to <timeout> amount of time to send the <message_block>. 00235 * If <send> times out a -1 is returned with <errno == ETIME>. If 00236 * it succeeds the number of bytes sent is returned. 00237 */ 00238 ssize_t send (const ACE_Message_Block *message_block, 00239 const ACE_Time_Value *timeout); 00240 00241 /** 00242 * Wait up to <timeout> amount of time to receive up to <n> bytes 00243 * into <buf> from <handle> (uses the <recv> call). If <recv> times 00244 * out a -1 is returned with <errno == ETIME>. If it succeeds the 00245 * number of bytes received is returned. 00246 */ 00247 ssize_t recv (void *buf, 00248 size_t n, 00249 const ACE_Time_Value *timeout); 00250 00251 /** 00252 * Wait up to <timeout> amount of time to receive up to <n> bytes 00253 * into <buf> from <handle> (uses the <recv> call). If <recv> times 00254 * out a -1 is returned with <errno == ETIME>. If it succeeds the 00255 * number of bytes received is returned. 00256 */ 00257 ssize_t recv (void *buf, 00258 size_t n, 00259 int flags, 00260 const ACE_Time_Value *timeout); 00261 00262 00263 /// Dump the state of an object. 00264 void dump (void) const; 00265 00266 /// Declare the dynamic allocation hooks. 00267 ACE_ALLOC_HOOK_DECLARE; 00268 00269 /// Return the local endpoint port number. Returns 0 if successful, 00270 /// else -1. 00271 /* int get_local_port (u_short &) const; 00272 00273 /// Return the port number of the remotely connected peer (if there 00274 /// is one). Returns 0 if successful, else -1. 00275 int get_remote_port (u_short &) const; 00276 */ 00277 00278 private: 00279 ssize_t fetch_recv_buf (int flag, const ACE_Time_Value *timeout); 00280 00281 /// Actual deliverying mechanism. 00282 ACE_MEM_SAP *deliver_strategy_; 00283 00284 /// Internal pointer for support recv/send. 00285 ACE_MEM_SAP_Node *recv_buffer_; 00286 00287 /// Record the current total buffer size of <recv_buffer_>. 00288 ssize_t buf_size_; 00289 00290 /// Record the current read pointer location in <recv_buffer_>. 00291 ssize_t cur_offset_; 00292 }; 00293 00294 #if !defined (ACE_LACKS_INLINE_FUNCTIONS) 00295 #include "ace/MEM_IO.i" 00296 #endif /* ACE_LACKS_INLINE_FUNCTIONS */ 00297 00298 #endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1 */ 00299 00300 #include "ace/post.h" 00301 #endif /* ACE_SOCK_IO_H */
1.2.14 written by Dimitri van Heesch,
© 1997-2002