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