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