Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / ace / MEM_IO.h
blob9f9b4787c59e7e326fa11725635bdf94863a4665
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file MEM_IO.h
7 * @author Nanbor Wang <nanbor@cs.wustl.edu>
8 */
9 //=============================================================================
12 #ifndef ACE_MEM_IO_H
13 #define ACE_MEM_IO_H
14 #include /**/ "ace/pre.h"
16 #include /**/ "ace/ACE_export.h"
18 #if !defined (ACE_LACKS_PRAGMA_ONCE)
19 # pragma once
20 #endif /* ACE_LACKS_PRAGMA_ONCE */
22 #if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1)
24 #include "ace/SOCK.h"
25 #include "ace/MEM_SAP.h"
26 #include "ace/Message_Block.h"
27 #include "ace/Process_Semaphore.h"
28 #include "ace/Process_Mutex.h"
30 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
32 class ACE_Export ACE_Reactive_MEM_IO : public ACE_MEM_SAP
34 public:
35 ACE_Reactive_MEM_IO ();
37 virtual ~ACE_Reactive_MEM_IO ();
39 /**
40 * Initialize the MEM_SAP object.
42 * @a options is used to pass in the Malloc_Options to initialize
43 * underlying ACE_MMAP.
45 virtual int init (ACE_HANDLE handle,
46 const ACE_TCHAR *name,
47 MALLOC_OPTIONS *options);
49 /**
50 * Fetch location of next available data into <recv_buffer_>.
51 * As this operation read the address of the data off the socket
52 * using ACE::recv, @a timeout only applies to ACE::recv.
54 virtual ssize_t recv_buf (ACE_MEM_SAP_Node *&buf,
55 int flags,
56 const ACE_Time_Value *timeout);
58 /**
59 * Wait to to @a timeout amount of time to send @a buf. If <send>
60 * times out a -1 is returned with @c errno == ETIME. If it succeeds
61 * the number of bytes sent is returned. */
62 virtual ssize_t send_buf (ACE_MEM_SAP_Node *buf,
63 int flags,
64 const ACE_Time_Value *timeout);
66 /**
67 * Convert the buffer offset @a off to absolute address to @a buf.
68 * Return the size of valid information containing in the @a buf,
69 * -1 if <shm_malloc_> is not initialized.
71 ssize_t get_buf_len (const ACE_OFF_T off, ACE_MEM_SAP_Node *&buf);
74 #if defined (ACE_WIN32) || !defined (_ACE_USE_SV_SEM)
75 class ACE_Export ACE_MT_MEM_IO : public ACE_MEM_SAP
77 public:
78 /// Structure for a simple queue
79 typedef struct
81 ACE_MEM_SAP_Node::ACE_MEM_SAP_NODE_PTR head_;
82 ACE_MEM_SAP_Node::ACE_MEM_SAP_NODE_PTR tail_;
83 } MQ_Struct;
85 class Simple_Queue
87 public:
88 Simple_Queue ();
89 Simple_Queue (MQ_Struct *mq);
91 int init (MQ_Struct *mq, ACE_MEM_SAP::MALLOC_TYPE *malloc);
93 int write (ACE_MEM_SAP_Node *new_msg);
95 ACE_MEM_SAP_Node *read ();
96 private:
97 MQ_Struct *mq_;
98 ACE_MEM_SAP::MALLOC_TYPE *malloc_;
101 typedef struct
103 ACE_SYNCH_PROCESS_SEMAPHORE *sema_;
104 ACE_SYNCH_PROCESS_MUTEX *lock_;
105 Simple_Queue queue_;
106 } Channel;
108 ACE_MT_MEM_IO ();
110 virtual ~ACE_MT_MEM_IO ();
113 * Initialize the MEM_SAP object.
115 virtual int init (ACE_HANDLE handle,
116 const ACE_TCHAR *name,
117 MALLOC_OPTIONS *options);
119 int fini ();
122 * Fetch location of next available data into <recv_buffer_>.
123 * As this operation read the address of the data off the socket
124 * using ACE::recv, @a timeout only applies to ACE::recv.
126 virtual ssize_t recv_buf (ACE_MEM_SAP_Node *&buf,
127 int flags,
128 const ACE_Time_Value *timeout);
131 * Wait to to @a timeout amount of time to send @a buf. If <send>
132 * times out a -1 is returned with @c errno == ETIME. If it succeeds
133 * the number of bytes sent is returned. */
134 virtual ssize_t send_buf (ACE_MEM_SAP_Node *buf,
135 int flags,
136 const ACE_Time_Value *timeout);
138 private:
139 Channel recv_channel_;
140 Channel send_channel_;
142 #endif /* ACE_WIN32 || !_ACE_USE_SV_SEM */
145 * @class ACE_MEM_IO
147 * @brief Defines the methods for the ACE shared memory wrapper I/O
148 * routines (e.g., send/recv).
149 * The shared memory transport uses ACE_SOCK_* class to
150 * implement the signaling mechanism so we can easily use the
151 * new mechanism with the Reactor pattern (which uses select
152 * under the hood.)
153 * ACE_MEM_Acceptor and ACE_MEM_Connector are used to establish
154 * connections. When a connection is established,
155 * ACE_MEM_Acceptor creates the MMAP file for data exchange and
156 * sends the location of the file (complete path name) to
157 * ACE_MEM_Connector thru the socket. ACE_MEM_Connector then
158 * reads the location of the file off the socket and opens up
159 * the same MMAP file. ACE_MEM_Stream at each side then
160 * contains a reference to the ACE_Mallo object using the same
161 * MMAP file.
162 * When sending information using methods provided in this
163 * class, ACE_MEM_IO requests a chunk of memory from the
164 * MALLOC_TYPE object, copy the data into the shared memory and
165 * send the memory offset (from the start of the ACE_Malloc)
166 * across the socket. This action also servers as a signal to
167 * the other end. The receiving side then reverses the
168 * procedures and copies the information into user buffer.
170 class ACE_Export ACE_MEM_IO : public ACE_SOCK
172 public:
173 /// Constructor.
174 ACE_MEM_IO ();
176 /// Destructor.
177 ~ACE_MEM_IO ();
179 typedef enum
181 Reactive,
183 } Signal_Strategy;
186 * Initialize the MEM_SAP object.
188 int init (const ACE_TCHAR *name,
189 ACE_MEM_IO::Signal_Strategy type = ACE_MEM_IO::Reactive,
190 ACE_MEM_SAP::MALLOC_OPTIONS *options = 0);
193 * Finalizing the MEM_IO object. This method doesn't invoke
194 * the <remove> method.
196 int fini ();
198 /// Send an @a n byte buffer to the other process using shm_malloc_
199 /// connected thru the socket.
200 ssize_t send (const void *buf,
201 size_t n,
202 int flags) ;
204 /// Recv an @a n byte buffer from the shm_malloc_ thru connected socket.
205 ssize_t recv (void *buf,
206 size_t n,
207 int flags) ;
209 /// Send an @a n byte buffer to the other process using shm_malloc_
210 /// connected thru the socket.
211 ssize_t send (const void *buf,
212 size_t n) ;
214 /// Recv an @a n byte buffer from the shm_malloc_ thru connected socket.
215 ssize_t recv (void *buf,
216 size_t n) ;
219 * Wait to to @a timeout amount of time to send up to @a n bytes into
220 * @a buf from <handle> (uses the <send> call). If <send> times out
221 * a -1 is returned with @c errno == ETIME. If it succeeds the
222 * number of bytes sent is returned.
224 ssize_t send (const void *buf,
225 size_t n,
226 const ACE_Time_Value *timeout);
229 * Wait to to @a timeout amount of time to send up to @a n bytes into
230 * @a buf from <handle> (uses the <send> call). If <send> times out
231 * a -1 is returned with @c errno == ETIME. If it succeeds the
232 * number of bytes sent is returned.
234 ssize_t send (const void *buf,
235 size_t n,
236 int flags,
237 const ACE_Time_Value *timeout);
240 * Wait to to @a timeout amount of time to send the @a message_block.
241 * If <send> times out a -1 is returned with @c errno == ETIME. If
242 * it succeeds the number of bytes sent is returned.
244 ssize_t send (const ACE_Message_Block *message_block,
245 const ACE_Time_Value *timeout);
248 * Wait up to @a timeout amount of time to receive up to @a n bytes
249 * into @a buf from <handle> (uses the <recv> call). If <recv> times
250 * out a -1 is returned with @c errno == ETIME. If it succeeds the
251 * number of bytes received is returned.
253 ssize_t recv (void *buf,
254 size_t n,
255 const ACE_Time_Value *timeout);
258 * Wait up to @a timeout amount of time to receive up to @a n bytes
259 * into @a buf from <handle> (uses the <recv> call). If <recv> times
260 * out a -1 is returned with @c errno == ETIME. If it succeeds the
261 * number of bytes received is returned.
263 ssize_t recv (void *buf,
264 size_t n,
265 int flags,
266 const ACE_Time_Value *timeout);
269 /// Dump the state of an object.
270 void dump () const;
272 /// Declare the dynamic allocation hooks.
273 ACE_ALLOC_HOOK_DECLARE;
275 /// Return the local endpoint port number. Returns 0 if successful,
276 /// else -1.
277 /* int get_local_port (u_short &) const;
279 /// Return the port number of the remotely connected peer (if there
280 /// is one). Returns 0 if successful, else -1.
281 int get_remote_port (u_short &) const;
284 private:
285 ssize_t fetch_recv_buf (int flag, const ACE_Time_Value *timeout);
287 /// Actual deliverying mechanism.
288 ACE_MEM_SAP *deliver_strategy_;
290 /// Internal pointer for support recv/send.
291 ACE_MEM_SAP_Node *recv_buffer_;
293 /// Record the current total buffer size of <recv_buffer_>.
294 ssize_t buf_size_;
296 /// Record the current read pointer location in <recv_buffer_>.
297 ssize_t cur_offset_;
300 ACE_END_VERSIONED_NAMESPACE_DECL
302 #if defined (__ACE_INLINE__)
303 #include "ace/MEM_IO.inl"
304 #endif /* __ACE_INLINE__ */
306 #endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1 */
308 #include /**/ "ace/post.h"
309 #endif /* ACE_SOCK_IO_H */