1 /** @file
2 
3   Public VConnection declaration and associated declarations
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22 
23  */
24 
25 #if !defined(_I_VConnection_h_)
26 #define _I_VConnection_h_
27 
28 #include "libts.h"
29 #include "I_EventSystem.h"
30 #if !defined(I_VIO_h)
31 #error "include I_VIO.h"
32 -- -
33   include I_VIO.h
34 #endif
35 
36 //
37 // Data Types
38 //
39 #define VCONNECTION_CACHE_DATA_BASE 0
40 #define VCONNECTION_NET_DATA_BASE 100
41 #define VCONNECTION_API_DATA_BASE 200
42 
43 //
44 // Event signals
45 //
46 
47 #define VC_EVENT_NONE EVENT_NONE
48 
49 /** When a Continuation is first scheduled on a processor. */
50 #define VC_EVENT_IMMEDIATE EVENT_IMMEDIATE
51 
52 #define VC_EVENT_READ_READY VC_EVENT_EVENTS_START
53 
54 /**
55   Any data in the accociated buffer *will be written* when the
56   Continuation returns.
57 
58 */
59 #define VC_EVENT_WRITE_READY (VC_EVENT_EVENTS_START + 1)
60 
61 #define VC_EVENT_READ_COMPLETE (VC_EVENT_EVENTS_START + 2)
62 #define VC_EVENT_WRITE_COMPLETE (VC_EVENT_EVENTS_START + 3)
63 
64 /**
65   No more data (end of stream). It should be interpreted by a
66   protocol engine as either a COMPLETE or ERROR.
67 
68 */
69 #define VC_EVENT_EOS (VC_EVENT_EVENTS_START + 4)
70 
71 #define VC_EVENT_ERROR EVENT_ERROR
72 
73 /**
74   VC_EVENT_INACTIVITY_TIMEOUT indiates that the operation (read or write) has:
75     -# been enabled for more than the inactivity timeout period
76        (for a read, there has been space in the buffer)
77        (for a write, there has been data in the buffer)
78     -# no progress has been made
79        (for a read, no data has been read from the connection)
80        (for a write, no data has been written to the connection)
81 
82 */
83 #define VC_EVENT_INACTIVITY_TIMEOUT (VC_EVENT_EVENTS_START + 5)
84 
85 /**
86   Total time for some operation has been exeeded, regardless of any
87   intermediate progress.
88 
89 */
90 #define VC_EVENT_ACTIVE_TIMEOUT (VC_EVENT_EVENTS_START + 6)
91 
92 #define VC_EVENT_OOB_COMPLETE (VC_EVENT_EVENTS_START + 7)
93 
94 //
95 // Event names
96 //
97 
98 //
99 // VC_EVENT_READ_READ occurs when data *has been written* into
100 // the associated buffer.
101 //
102 // VC_EVENT_ERROR indicates that some error has occured.  The
103 // "data" will be either 0 if the errno is unavailable or errno.
104 //
105 // VC_EVENT_INTERVAL indidates that an interval timer has expired.
106 //
107 
108 //
109 // Event return codes
110 //
111 #define VC_EVENT_DONE CONTINUATION_DONE
112 #define VC_EVENT_CONT CONTINUATION_CONT
113 
114   //////////////////////////////////////////////////////////////////////////////
115   //
116   //      Support Data Structures
117   //
118   //////////////////////////////////////////////////////////////////////////////
119 
120   /** Used in VConnection::shutdown(). */
121   enum ShutdownHowTo_t {
122     IO_SHUTDOWN_READ = 0,
123     IO_SHUTDOWN_WRITE,
124     IO_SHUTDOWN_READWRITE
125   };
126 
127 /** Used in VConnection::get_data(). */
128 enum TSApiDataType {
129   TS_API_DATA_READ_VIO = VCONNECTION_API_DATA_BASE,
130   TS_API_DATA_WRITE_VIO,
131   TS_API_DATA_OUTPUT_VC,
132   TS_API_DATA_CLOSED
133 };
134 
135 extern "C" {
136 typedef struct tsapi_vio *TSVIO;
137 }
138 
139 /**
140   Base class for the connection classes that provide IO capabilities.
141 
142   The VConnection class is an abstract representation of a uni or
143   bi-directional data conduit returned by a Processor. In a sense,
144   they serve a similar purpose to file descriptors. A VConnection
145   is a pure base class that defines methods to perform stream IO.
146   It is also a Continuation that is called back from processors.
147 
148 */
149 class VConnection : public Continuation
150 {
151 public:
152   virtual ~VConnection();
153 
154   /**
155     Read data from the VConnection.
156 
157     Called by a state machine to read data from the VConnection.
158     Processors implementing read functionality take out lock, put
159     new bytes on the buffer and call the continuation back before
160     releasing the lock in order to enable the state machine to
161     handle transfer schemes where the end of a given transaction
162     is marked by a special character (ie: NNTP).
163 
164     <b>Possible Event Codes</b>
165 
166     On the callback to the continuation, the VConnection may use
167     on of the following values for the event code:
168 
169     <table border="1">
170       <tr>
171         <td align="center"><b>Event code</b></td>
172         <td align="center"><b>Meaning</b></td>
173       </tr>
174       <tr>
175         <td>VC_EVENT_READ_READY</td>
176         <td>Data has been added to the buffer or the buffer is full</td>
177       </tr>
178       <tr>
179         <td>VC_EVENT_READ_COMPLETE</td>
180         <td>The amount of data indicated by 'nbytes' has been read into the
181             buffer</td>
182       </tr>
183       <tr>
184         <td>VC_EVENT_EOS</td>
185         <td>The stream being read from has been shutdown</td>
186       </tr>
187       <tr>
188         <td>VC_EVENT_ERROR</td>
189         <td>An error occurred during the read</td>
190       </tr>
191     </table>
192 
193     @param c Continuation to be called back with events.
194     @param nbytes Number of bytes to read. If unknown, nbytes must
195       be set to INT64_MAX.
196     @param buf buffer to read into.
197     @return VIO representing the scheduled IO operation.
198 
199   */
200   virtual VIO *do_io_read(Continuation *c = NULL, int64_t nbytes = INT64_MAX, MIOBuffer *buf = 0) = 0;
201 
202   /**
203     Write data to the VConnection.
204 
205     This method is called by a state machine to write data to the
206     VConnection.
207 
208     <b>Possible Event Codes</b>
209 
210     On the callback to the continuation, the VConnection may use
211     on of the following event codes:
212 
213     <table border="1">
214       <tr>
215         <td align="center"><b>Event code</b></td>
216         <td align="center"><b>Meaning</b></td>
217       </tr>
218       <tr>
219         <td>VC_EVENT_WRITE_READY</td>
220         <td>Data was written from the reader or there are no bytes available
221         for the reader to write.</td>
222       </tr>
223       <tr>
224         <td>VC_EVENT_WRITE_COMPLETE</td>
225         <td>The amount of data indicated by 'nbytes' has been written to the
226             VConnection</td>
227       </tr>
228       <tr>
229         <td>VC_EVENT_INACTIVITY_TIMEOUT</td>
230         <td>No activity was performed for a certain period.</td>
231       </tr>
232       <tr>
233         <td>VC_EVENT_ACTIVE_TIMEOUT</td>
234         <td>Write operation continued beyond a time limit.</td>
235       </tr>
236       <tr>
237         <td>VC_EVENT_ERROR</td>
238         <td>An error occurred during the write</td>
239       </tr>
240     </table>
241 
242     @param c Continuation to be called back with events.
243     @param nbytes Number of bytes to write. If unknown, nbytes must
244       be set to INT64_MAX.
245     @param buf Reader whose data is to be read from.
246     @param owner
247     @return VIO representing the scheduled IO operation.
248 
249   */
250   virtual VIO *do_io_write(Continuation *c = NULL, int64_t nbytes = INT64_MAX, IOBufferReader *buf = 0, bool owner = false) = 0;
251 
252   /**
253     Indicate that the VConnection is no longer needed.
254 
255     Once the state machine has finished using this VConnection, it
256     must call this function to indicate that the VConnection can
257     be deallocated.  After a close has been called, the VConnection
258     and underlying processor must not send any more events related
259     to this VConnection to the state machine. Likeswise, the state
260     machine must not access the VConnection or any VIOs obtained
261     from it after calling this method.
262 
263     @param lerrno indicates where a close is a normal close or an
264       abort. The difference between a normal close and an abort
265       depends on the underlying type of the VConnection.
266 
267   */
268   virtual void do_io_close(int lerrno = -1) = 0;
269 
270   /**
271     Terminate one or both directions of the VConnection.
272 
273     Indicates that one or both sides of the VConnection should be
274     terminated. After this call is issued, no further I/O can be
275     done on the specified direction of the connection. The processor
276     must not send any further events (including timeout events) to
277     the state machine, and the state machine must not use any VIOs
278     from a shutdown direction of the connection. Even if both sides
279     of a connection are shutdown, the state machine must still call
280     do_io_close() when it wishes the VConnection to be deallocated.
281 
282     <b>Possible howto values</b>
283 
284     <table border="1">
285       <tr>
286         <td align="center"><b>Value</b></td>
287         <td align="center"><b>Meaning</b></td>
288       </tr>
289       <tr>
290         <td>IO_SHUTDOWN_READ</td>
291         <td>Indicates that this VConnection should not generate any more
292         read events</td>
293       </tr>
294       <tr>
295         <td>IO_SHUTDOWN_WRITE</td>
296         <td>Indicates that this VConnection should not generate any more
297         write events</td>
298       </tr>
299       <tr>
300         <td>IO_SHUTDOWN_READWRITE</td>
301         <td>Indicates that this VConnection should not generate any more
302         read nor write events</td>
303       </tr>
304     </table>
305 
306     @param howto Specifies which direction of the VConnection to
307       shutdown.
308 
309   */
310   virtual void do_io_shutdown(ShutdownHowTo_t howto) = 0;
311 
312   VConnection(ProxyMutex *aMutex);
313 
314   /** @deprecated */
315   VIO *do_io(int op, Continuation *c = NULL, int64_t nbytes = INT64_MAX, MIOBuffer *buf = 0, int data = 0);
316 
317   // Private
318   // Set continuation on a given vio. The public interface
319   // is through VIO::set_continuation()
320   virtual void set_continuation(VIO *vio, Continuation *cont);
321 
322   // Reenable a given vio.  The public interface is through VIO::reenable
323   virtual void reenable(VIO *vio);
324   virtual void reenable_re(VIO *vio);
325 
326   /**
327     Convenience function to retrieve information from VConnection.
328 
329     This function is provided as a convenience for state machines
330     to transmit information from/to a VConnection without breaking
331     the VConnection abstraction. Its behavior varies depending on
332     the type of VConnection being used.
333 
334     @param id Identifier associated to interpret the data field
335     @param data Value or pointer with state machine or VConnection data.
336     @return True if the oparation is successful.
337 
338   */
339   virtual bool
340   get_data(int id, void *data)
341   {
342     (void)id;
343     (void)data;
344     return false;
345   }
346 
347   /**
348     Convenience function to set information into the VConnection.
349 
350     This function is provided as a convenience for state machines
351     to transmit information from/to a VConnection without breaking
352     the VConnection abstraction. Its behavior varies depending on
353     the type of VConnection being used.
354 
355     @param id Identifier associated to interpret the data field.
356     @param data Value or pointer with state machine or VConnection data.
357     @return True if the oparation is successful.
358 
359   */
360   virtual bool
361   set_data(int id, void *data)
362   {
363     (void)id;
364     (void)data;
365     return false;
366   }
367 
368 public:
369   /**
370     The error code from the last error.
371 
372     Indicates the last error on the VConnection. They are either
373     system error codes or from the InkErrno.h file.
374 
375   */
376   int lerrno;
377 };
378 
379 struct DummyVConnection : public VConnection {
380   virtual VIO *
381   do_io_write(Continuation * /* c ATS_UNUSED */, int64_t /* nbytes ATS_UNUSED */, IOBufferReader * /* buf ATS_UNUSED */,
382               bool /* owner ATS_UNUSED */)
383   {
384     ink_assert(!"VConnection::do_io_write -- "
385                 "cannot use default implementation");
386     return NULL;
387   }
388   virtual VIO *
389   do_io_read(Continuation * /* c ATS_UNUSED */, int64_t /* nbytes ATS_UNUSED */, MIOBuffer * /* buf ATS_UNUSED */)
390   {
391     ink_assert(!"VConnection::do_io_read -- "
392                 "cannot use default implementation");
393     return NULL;
394   }
395   virtual void
396   do_io_close(int /* alerrno ATS_UNUSED */)
397   {
398     ink_assert(!"VConnection::do_io_close -- "
399                 "cannot use default implementation");
400   }
401   virtual void do_io_shutdown(ShutdownHowTo_t /* howto ATS_UNUSED */)
402   {
403     ink_assert(!"VConnection::do_io_shutdown -- "
404                 "cannot use default implementation");
405   }
406   DummyVConnection(ProxyMutex *m) : VConnection(m) {}
407 };
408 
409 #endif /*_I_VConnection_h_*/
410