]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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 | #include <thrift/c_glib/thrift.h> | |
21 | #include <thrift/c_glib/transport/thrift_transport.h> | |
22 | ||
23 | /* define the GError domain string */ | |
24 | #define THRIFT_TRANSPORT_ERROR_DOMAIN "thrift-transport-error-quark" | |
25 | ||
26 | G_DEFINE_ABSTRACT_TYPE(ThriftTransport, thrift_transport, G_TYPE_OBJECT) | |
27 | ||
28 | gboolean | |
29 | thrift_transport_is_open (ThriftTransport *transport) | |
30 | { | |
31 | return THRIFT_TRANSPORT_GET_CLASS (transport)->is_open (transport); | |
32 | } | |
33 | ||
34 | gboolean | |
35 | thrift_transport_peek (ThriftTransport *transport, GError **error) | |
36 | { | |
37 | return THRIFT_TRANSPORT_GET_CLASS (transport)->peek (transport, error); | |
38 | } | |
39 | ||
40 | gboolean | |
41 | thrift_transport_open (ThriftTransport *transport, GError **error) | |
42 | { | |
43 | return THRIFT_TRANSPORT_GET_CLASS (transport)->open (transport, error); | |
44 | } | |
45 | ||
46 | gboolean | |
47 | thrift_transport_close (ThriftTransport *transport, GError **error) | |
48 | { | |
49 | return THRIFT_TRANSPORT_GET_CLASS (transport)->close (transport, error); | |
50 | } | |
51 | ||
52 | gint32 | |
53 | thrift_transport_read (ThriftTransport *transport, gpointer buf, | |
54 | guint32 len, GError **error) | |
55 | { | |
56 | return THRIFT_TRANSPORT_GET_CLASS (transport)->read (transport, buf, | |
57 | len, error); | |
58 | } | |
59 | ||
60 | gboolean | |
61 | thrift_transport_read_end (ThriftTransport *transport, GError **error) | |
62 | { | |
63 | return THRIFT_TRANSPORT_GET_CLASS (transport)->read_end (transport, | |
64 | error); | |
65 | } | |
66 | ||
67 | gboolean | |
68 | thrift_transport_write (ThriftTransport *transport, const gpointer buf, | |
69 | const guint32 len, GError **error) | |
70 | { | |
71 | return THRIFT_TRANSPORT_GET_CLASS (transport)->write (transport, buf, | |
72 | len, error); | |
73 | } | |
74 | ||
75 | gboolean | |
76 | thrift_transport_write_end (ThriftTransport *transport, GError **error) | |
77 | { | |
78 | return THRIFT_TRANSPORT_GET_CLASS (transport)->write_end (transport, | |
79 | error); | |
80 | } | |
81 | ||
82 | gboolean | |
83 | thrift_transport_flush (ThriftTransport *transport, GError **error) | |
84 | { | |
85 | return THRIFT_TRANSPORT_GET_CLASS (transport)->flush (transport, error); | |
86 | } | |
87 | ||
88 | gint32 | |
89 | thrift_transport_read_all (ThriftTransport *transport, gpointer buf, | |
90 | guint32 len, GError **error) | |
91 | { | |
92 | return THRIFT_TRANSPORT_GET_CLASS (transport)->read_all (transport, buf, | |
93 | len, error); | |
94 | } | |
95 | ||
96 | /* by default, peek returns true if and only if the transport is open */ | |
97 | static gboolean | |
98 | thrift_transport_real_peek (ThriftTransport *transport, GError **error) | |
99 | { | |
100 | THRIFT_UNUSED_VAR (error); | |
101 | ||
102 | return THRIFT_TRANSPORT_GET_CLASS (transport)->is_open (transport); | |
103 | } | |
104 | ||
105 | static gint32 | |
106 | thrift_transport_real_read_all (ThriftTransport *transport, gpointer buf, | |
107 | guint32 len, GError **error) | |
108 | { | |
109 | ThriftTransportClass *ttc; | |
110 | guint32 have; | |
111 | gint32 ret; | |
112 | gint8 *bytes; | |
113 | ||
114 | THRIFT_UNUSED_VAR (error); | |
115 | ||
116 | ttc = THRIFT_TRANSPORT_GET_CLASS (transport); | |
117 | have = 0; | |
118 | ret = 0; | |
119 | bytes = (gint8*) buf; | |
120 | ||
121 | while (have < len) { | |
122 | if ((ret = ttc->read (transport, (gpointer) (bytes + have), len - have, | |
123 | error)) < 0) { | |
124 | return ret; | |
125 | } | |
126 | have += ret; | |
127 | } | |
128 | ||
129 | return have; | |
130 | } | |
131 | ||
132 | /* define the GError domain for Thrift transports */ | |
133 | GQuark | |
134 | thrift_transport_error_quark (void) | |
135 | { | |
136 | return g_quark_from_static_string (THRIFT_TRANSPORT_ERROR_DOMAIN); | |
137 | } | |
138 | ||
139 | /* class initializer for ThriftTransport */ | |
140 | static void | |
141 | thrift_transport_class_init (ThriftTransportClass *cls) | |
142 | { | |
143 | /* set these as virtual methods to be implemented by a subclass */ | |
144 | cls->is_open = thrift_transport_is_open; | |
145 | cls->open = thrift_transport_open; | |
146 | cls->close = thrift_transport_close; | |
147 | cls->read = thrift_transport_read; | |
148 | cls->read_end = thrift_transport_read_end; | |
149 | cls->write = thrift_transport_write; | |
150 | cls->write_end = thrift_transport_write_end; | |
151 | cls->flush = thrift_transport_flush; | |
152 | ||
153 | /* provide a default implementation for the peek and read_all methods */ | |
154 | cls->peek = thrift_transport_real_peek; | |
155 | cls->read_all = thrift_transport_real_read_all; | |
156 | } | |
157 | ||
158 | static void | |
159 | thrift_transport_init (ThriftTransport *transport) | |
160 | { | |
161 | THRIFT_UNUSED_VAR (transport); | |
162 | } |