3 /* buf trp (transport) - buffered transport using the TCP one */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2004 Martin Quinson. */
8 /* This program is free software; you can redistribute it and/or modify it
9 under the terms of the license (GNU LGPL) which comes with this package. */
11 #include <netinet/in.h> /* htonl/ntohl */
13 #include <string.h> /* memset */
15 #include "gras_private.h"
16 #include "transport_private.h"
18 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(trp_buf,transport,
19 "Generic buffered transport (works on top of TCP or SG)");
24 gras_error_t gras_trp_buf_socket_client(gras_trp_plugin_t *self,
25 /* OUT */ gras_socket_t *sock);
26 gras_error_t gras_trp_buf_socket_server(gras_trp_plugin_t *self,
27 /* OUT */ gras_socket_t *sock);
28 gras_error_t gras_trp_buf_socket_accept(gras_socket_t *sock,
31 void gras_trp_buf_socket_close(gras_socket_t *sd);
33 gras_error_t gras_trp_buf_chunk_send(gras_socket_t *sd,
37 gras_error_t gras_trp_buf_chunk_recv(gras_socket_t *sd,
40 gras_error_t gras_trp_buf_flush(gras_socket_t *sock);
44 *** Specific plugin part
48 gras_trp_plugin_t *super;
49 } gras_trp_buf_plug_data_t;
52 *** Specific socket part
58 int pos; /* for receive; not exchanged over the net */
61 struct gras_trp_bufdata_{
67 gras_error_t gras_trp_buf_init_sock(gras_socket_t *sock) {
68 gras_trp_bufdata_t *data=gras_new(gras_trp_bufdata_t,1);
74 data->buffsize = 100 * 1024 ; /* 100k */
76 if (!(data->in.data = (char*)gras_malloc(data->buffsize)))
78 data->in.pos = 0; /* useless, indeed, since size==pos */
80 if (!(data->out.data = (char*)gras_malloc(data->buffsize)))
92 gras_trp_buf_setup(gras_trp_plugin_t *plug) {
94 gras_trp_buf_plug_data_t *data =gras_new(gras_trp_buf_plug_data_t,1);
99 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
101 DEBUG1("Derivate a buffer plugin from %s",gras_if_RL() ? "tcp" : "sg");
103 plug->socket_client = gras_trp_buf_socket_client;
104 plug->socket_server = gras_trp_buf_socket_server;
105 plug->socket_accept = gras_trp_buf_socket_accept;
106 plug->socket_close = gras_trp_buf_socket_close;
108 plug->chunk_send = gras_trp_buf_chunk_send;
109 plug->chunk_recv = gras_trp_buf_chunk_recv;
111 plug->flush = gras_trp_buf_flush;
113 plug->data = (void*)data;
119 gras_error_t gras_trp_buf_socket_client(gras_trp_plugin_t *self,
120 /* OUT */ gras_socket_t *sock){
121 gras_error_t errcode;
122 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)self->data)->super;
125 TRY(super->socket_client(super,sock));
127 TRY(gras_trp_buf_init_sock(sock));
133 * gras_trp_buf_socket_server:
135 * Open a socket used to receive messages.
137 gras_error_t gras_trp_buf_socket_server(gras_trp_plugin_t *self,
138 /* OUT */ gras_socket_t *sock){
139 gras_error_t errcode;
140 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)self->data)->super;
143 TRY(super->socket_server(super,sock));
145 TRY(gras_trp_buf_init_sock(sock));
150 gras_trp_buf_socket_accept(gras_socket_t *sock,
151 gras_socket_t **dst) {
152 gras_error_t errcode;
153 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
156 TRY(super->socket_accept(sock,dst));
157 (*dst)->plugin = sock->plugin;
158 TRY(gras_trp_buf_init_sock(*dst));
162 void gras_trp_buf_socket_close(gras_socket_t *sock){
163 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
164 gras_trp_bufdata_t *data=sock->bufdata;
167 if (data->in.size || data->out.size)
168 gras_trp_buf_flush(sock);
172 free(data->out.data);
175 super->socket_close(sock);
179 * gras_trp_buf_chunk_send:
181 * Send data on a TCP socket
184 gras_trp_buf_chunk_send(gras_socket_t *sock,
188 gras_error_t errcode;
189 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
193 /* Let underneath plugin check for direction, we work even in duplex */
194 gras_assert0(size >= 0, "Cannot send a negative amount of data");
196 while (chunk_pos < size) {
197 /* size of the chunck to receive in that shot */
198 long int thissize = min(size-chunk_pos,data->buffsize - data->out.size);
199 DEBUG5("Set the chars %d..%ld into the buffer (size=%ld, ctn='%.*s')",
201 ((int)data->out.size) + thissize -1,
202 size, chunk_pos, chunk);
204 memcpy(data->out.data + data->out.size, chunk + chunk_pos, thissize);
206 data->out.size += thissize;
207 chunk_pos += thissize;
208 DEBUG5("New pos = %d; Still to send = %ld of %ld; ctn sofar='%.*s'",
209 data->out.size,size-chunk_pos,size,(int)chunk_pos,chunk);
211 if (data->out.size == data->buffsize) /* out of space. Flush it */
212 TRY(gras_trp_buf_flush(sock));
220 * gras_trp_buf_chunk_recv:
222 * Receive data on a TCP socket.
225 gras_trp_buf_chunk_recv(gras_socket_t *sock,
229 gras_error_t errcode;
230 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
231 gras_trp_bufdata_t *data=sock->bufdata;
232 long int chunck_pos = 0;
234 /* Let underneath plugin check for direction, we work even in duplex */
235 gras_assert0(sock, "Cannot recv on an NULL socket");
236 gras_assert0(size >= 0, "Cannot receive a negative amount of data");
240 while (chunck_pos < size) {
241 /* size of the chunck to receive in that shot */
244 if (data->in.size == data->in.pos) { /* out of data. Get more */
246 DEBUG0("Recv the size");
247 TRY(super->chunk_recv(sock,(char*)&nextsize, 4));
248 data->in.size = ntohl(nextsize);
250 VERB1("Recv the chunk (size=%d)",data->in.size);
251 TRY(super->chunk_recv(sock, data->in.data, data->in.size));
255 thissize = min(size-chunck_pos , data->in.size - data->in.pos);
256 DEBUG2("Get the chars %d..%ld out of the buffer",
258 data->in.pos + thissize - 1);
259 memcpy(chunk+chunck_pos, data->in.data + data->in.pos, thissize);
261 data->in.pos += thissize;
262 chunck_pos += thissize;
263 DEBUG5("New pos = %d; Still to receive = %ld of %ld. Ctn so far='%.*s'",
264 data->in.pos,size - chunck_pos,size,(int)chunck_pos,chunk);
272 * gras_trp_buf_flush:
274 * Make sure the data is sent
277 gras_trp_buf_flush(gras_socket_t *sock) {
278 gras_error_t errcode;
280 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
281 gras_trp_bufdata_t *data=sock->bufdata;
284 size = htonl(data->out.size);
285 DEBUG1("Send the size (=%d)",data->out.size);
286 TRY(super->chunk_send(sock,(char*) &size, 4));
288 DEBUG1("Send the chunk (size=%d)",data->out.size);
289 TRY(super->chunk_send(sock, data->out.data, data->out.size));
290 VERB1("Chunk sent (size=%d)",data->out.size);