1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef _THRIFT_TRANSPORT_H
21 #define _THRIFT_TRANSPORT_H
22 
23 #include <glib-object.h>
24 
25 #include <thrift/c_glib/thrift_configuration.h>
26 
27 G_BEGIN_DECLS
28 
29 /*! \file thrift_transport.h
30  *  \brief Abstract class for Thrift transports.
31  *
32  * An abstract class is used instead of an interface because:
33  *  - interfaces can't seem to be used as properties.  ThriftProtocol has
34  *    a ThriftTransport as an object property.
35  *  - if a method needs to be added that all subclasses can use, a class
36  *    is necessary.
37  */
38 
39 /* type macros */
40 #define THRIFT_TYPE_TRANSPORT (thrift_transport_get_type ())
41 #define THRIFT_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), THRIFT_TYPE_TRANSPORT, ThriftTransport))
42 #define THRIFT_IS_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), THRIFT_TYPE_TRANSPORT))
43 #define THRIFT_TRANSPORT_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), THRIFT_TYPE_TRANSPORT, ThriftTransportClass))
44 #define THRIFT_IS_TRANSPORT_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), THRIFT_TYPE_TRANSPORT))
45 #define THRIFT_TRANSPORT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), THRIFT_TYPE_TRANSPORT, ThriftTransportClass))
46 
47 typedef struct _ThriftTransport ThriftTransport;
48 
49 /*!
50  * Thrift Protocol object
51  */
52 struct _ThriftTransport
53 {
54   GObject parent;
55 
56   /* protected */
57   ThriftConfiguration *configuration;
58   glong remainingMessageSize_;
59   glong knowMessageSize_;
60 };
61 
62 typedef struct _ThriftTransportClass ThriftTransportClass;
63 
64 /*!
65  * Thrift Transport class
66  */
67 struct _ThriftTransportClass
68 {
69   GObjectClass parent;
70 
71   /* vtable */
72   gboolean (*is_open) (ThriftTransport *transport);
73   gboolean (*peek) (ThriftTransport *transport, GError **error);
74   gboolean (*open) (ThriftTransport *transport, GError **error);
75   gboolean (*close) (ThriftTransport *transport, GError **error);
76   gint32 (*read) (ThriftTransport *transport, gpointer buf,
77                   guint32 len, GError **error);
78   gboolean (*read_end) (ThriftTransport *transport, GError **error);
79   gboolean (*write) (ThriftTransport *transport, const gpointer buf,
80                    const guint32 len, GError **error);
81   gboolean (*write_end) (ThriftTransport *transport, GError **error);
82   gboolean (*flush) (ThriftTransport *transport, GError **error);
83   gint32 (*read_all) (ThriftTransport *transport, gpointer buf,
84                       guint32 len, GError **error);
85   gboolean (*updateKnownMessageSize) (ThriftTransport *transport, glong size, GError **error);
86   gboolean (*checkReadBytesAvailable) (ThriftTransport *transport, glong numBytes, GError **error);
87   gboolean (*resetConsumedMessageSize) (ThriftTransport *transport, glong newSize, GError **error);
88   gboolean (*countConsumedMessageBytes) (ThriftTransport *transport, glong numBytes, GError **error);
89 };
90 
91 /* used by THRIFT_TYPE_TRANSPORT */
92 GType thrift_transport_get_type (void);
93 
94 /* virtual public methods */
95 
96 /*!
97  * Checks if this transport is opened.
98  * \public \memberof ThriftTransportInterface
99  */
100 gboolean thrift_transport_is_open (ThriftTransport *transport);
101 
102 /*!
103  * Open the transport for reading and writing.
104  * \public \memberof ThriftTransportInterface
105  */
106 gboolean thrift_transport_open (ThriftTransport *transport, GError **error);
107 
108 /*!
109  * Tests whether there is more data to read or if the remote side is still
110  * open. By default this is true whenever the transport is open, but
111  * implementations should add logic to test for this condition where possible
112  * (i.e. on a socket).
113  *
114  * This is used by a server to check if it should listen for another request.
115  * \public \memberof ThriftTransportInterface
116  */
117 gboolean thrift_transport_peek (ThriftTransport *transport, GError **error);
118 
119 /*!
120  * Close the transport.
121  * \public \memberof ThriftTransportInterface
122  */
123 gboolean thrift_transport_close (ThriftTransport *transport, GError **error);
124 
125 /*!
126  * Read some data into the buffer buf.
127  * \public \memberof ThriftTransportInterface
128  */
129 gint32 thrift_transport_read (ThriftTransport *transport, gpointer buf,
130                               guint32 len, GError **error);
131 
132 /*!
133  * Called when read is completed.
134  * \public \memberof ThriftTransportInterface
135  */
136 gboolean thrift_transport_read_end (ThriftTransport *transport, GError **error);
137 
138 /*!
139  * Writes data from a buffer to the transport.
140  * \public \memberof ThriftTransportInterface
141  */
142 gboolean thrift_transport_write (ThriftTransport *transport, const gpointer buf,
143                                  const guint32 len, GError **error);
144 
145 /*!
146  * Called when write is completed.
147  * \public \memberof ThriftTransportInterface
148  */
149 gboolean thrift_transport_write_end (ThriftTransport *transport,
150                                      GError **error);
151 
152 /*!
153  * Flushes any pending data to be written.  Typically used with buffered
154  * transport mechanisms.
155  * \public \memberof ThriftTransportInterface
156  */
157 gboolean thrift_transport_flush (ThriftTransport *transport, GError **error);
158 
159 /*!
160  * Read len bytes of data into the buffer buf.
161  * \public \memberof ThriftTransportInterface
162  */
163 gint32 thrift_transport_read_all (ThriftTransport *transport, gpointer buf,
164                                   guint32 len, GError **error);
165 
166 /* define error/exception types */
167 typedef enum
168 {
169   THRIFT_TRANSPORT_ERROR_UNKNOWN,
170   THRIFT_TRANSPORT_ERROR_HOST,
171   THRIFT_TRANSPORT_ERROR_SOCKET,
172   THRIFT_TRANSPORT_ERROR_CONNECT,
173   THRIFT_TRANSPORT_ERROR_SEND,
174   THRIFT_TRANSPORT_ERROR_RECEIVE,
175   THRIFT_TRANSPORT_ERROR_CLOSE,
176   THRIFT_TRANSPORT_ERROR_MAX_MESSAGE_SIZE_REACHED
177 } ThriftTransportError;
178 
179 /* define an error domain for GError to use */
180 GQuark thrift_transport_error_quark (void);
181 #define THRIFT_TRANSPORT_ERROR (thrift_transport_error_quark ())
182 
183 /* define macro for invalid socket */
184 #define THRIFT_INVALID_SOCKET (-1)
185 
186 G_END_DECLS
187 
188 #endif /* _THRIFT_TRANSPORT_H */
189