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,
27 /* OUT */ gras_socket_t *sock);
28 gras_error_t gras_trp_buf_socket_server(gras_trp_plugin_t *self,
30 /* OUT */ gras_socket_t *sock);
31 gras_error_t gras_trp_buf_socket_accept(gras_socket_t *sock,
34 void gras_trp_buf_socket_close(gras_socket_t *sd);
36 gras_error_t gras_trp_buf_chunk_send(gras_socket_t *sd,
40 gras_error_t gras_trp_buf_chunk_recv(gras_socket_t *sd,
43 gras_error_t gras_trp_buf_flush(gras_socket_t *sock);
47 *** Specific plugin part
51 gras_trp_plugin_t *super;
52 } gras_trp_buf_plug_data_t;
55 *** Specific socket part
61 int pos; /* for receive; not exchanged over the net */
64 struct gras_trp_bufdata_{
70 gras_error_t gras_trp_buf_init_sock(gras_socket_t *sock) {
71 gras_trp_bufdata_t *data=gras_new(gras_trp_bufdata_t,1);
77 // data->buffsize = 32 * 1024 - 4; /* default socket buffsize (32k) - headers */
78 data->buffsize = 100 * 1024 ; /* 100k */
80 if (!(data->in.data = (char*)gras_malloc(data->buffsize)))
82 data->in.pos = 0; /* useless, indeed, since size==pos */
84 if (!(data->out.data = (char*)gras_malloc(data->buffsize)))
96 gras_trp_buf_setup(gras_trp_plugin_t *plug) {
98 gras_trp_buf_plug_data_t *data =gras_new(gras_trp_buf_plug_data_t,1);
103 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
105 DEBUG1("Derivate a buffer plugin from %s",gras_if_RL() ? "tcp" : "sg");
107 plug->socket_client = gras_trp_buf_socket_client;
108 plug->socket_server = gras_trp_buf_socket_server;
109 plug->socket_accept = gras_trp_buf_socket_accept;
110 plug->socket_close = gras_trp_buf_socket_close;
112 plug->chunk_send = gras_trp_buf_chunk_send;
113 plug->chunk_recv = gras_trp_buf_chunk_recv;
115 plug->flush = gras_trp_buf_flush;
117 plug->data = (void*)data;
123 gras_error_t gras_trp_buf_socket_client(gras_trp_plugin_t *self,
126 /* OUT */ gras_socket_t *sock){
127 gras_error_t errcode;
128 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)self->data)->super;
131 TRY(super->socket_client(super,host,port,sock));
133 TRY(gras_trp_buf_init_sock(sock));
139 * gras_trp_buf_socket_server:
141 * Open a socket used to receive messages.
143 gras_error_t gras_trp_buf_socket_server(gras_trp_plugin_t *self,
145 /* OUT */ gras_socket_t *sock){
146 gras_error_t errcode;
147 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)self->data)->super;
150 TRY(super->socket_server(super,port,sock));
152 TRY(gras_trp_buf_init_sock(sock));
157 gras_trp_buf_socket_accept(gras_socket_t *sock,
158 gras_socket_t **dst) {
159 gras_error_t errcode;
160 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
163 TRY(super->socket_accept(sock,dst));
164 (*dst)->plugin = sock->plugin;
165 TRY(gras_trp_buf_init_sock(*dst));
169 void gras_trp_buf_socket_close(gras_socket_t *sock){
170 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
171 gras_trp_bufdata_t *data=sock->bufdata;
174 if (data->in.size || data->out.size)
175 gras_trp_buf_flush(sock);
179 free(data->out.data);
182 return super->socket_close(sock);
186 * gras_trp_buf_chunk_send:
188 * Send data on a TCP socket
191 gras_trp_buf_chunk_send(gras_socket_t *sock,
195 gras_error_t errcode;
196 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
200 /* Let underneath plugin check for direction, we work even in duplex */
201 gras_assert0(size >= 0, "Cannot send a negative amount of data");
203 while (chunk_pos < size) {
204 /* size of the chunck to receive in that shot */
205 long int thissize = min(size-chunk_pos,data->buffsize - data->out.size);
206 DEBUG5("Set the chars %d..%ld into the buffer (size=%ld, ctn='%.*s')",
208 ((int)data->out.size) + thissize -1,
209 size, chunk_pos, chunk);
211 memcpy(data->out.data + data->out.size, chunk + chunk_pos, thissize);
213 data->out.size += thissize;
214 chunk_pos += thissize;
215 DEBUG5("New pos = %d; Still to send = %ld of %ld; ctn sofar='%.*s'",
216 data->out.size,size-chunk_pos,size,(int)chunk_pos,chunk);
218 if (data->out.size == data->buffsize) /* out of space. Flush it */
219 TRY(gras_trp_buf_flush(sock));
227 * gras_trp_buf_chunk_recv:
229 * Receive data on a TCP socket.
232 gras_trp_buf_chunk_recv(gras_socket_t *sock,
236 gras_error_t errcode;
237 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
238 gras_trp_bufdata_t *data=sock->bufdata;
239 long int chunck_pos = 0;
241 /* Let underneath plugin check for direction, we work even in duplex */
242 gras_assert0(sock, "Cannot recv on an NULL socket");
243 gras_assert0(size >= 0, "Cannot receive a negative amount of data");
247 while (chunck_pos < size) {
248 /* size of the chunck to receive in that shot */
251 if (data->in.size == data->in.pos) { /* out of data. Get more */
253 DEBUG0("Recv the size");
254 TRY(super->chunk_recv(sock,(char*)&nextsize, 4));
255 data->in.size = ntohl(nextsize);
257 VERB1("Recv the chunk (size=%d)",data->in.size);
258 TRY(super->chunk_recv(sock, data->in.data, data->in.size));
262 thissize = min(size-chunck_pos , data->in.size - data->in.pos);
263 DEBUG2("Get the chars %d..%ld out of the buffer",
265 data->in.pos + thissize - 1);
266 memcpy(chunk+chunck_pos, data->in.data + data->in.pos, thissize);
268 data->in.pos += thissize;
269 chunck_pos += thissize;
270 DEBUG5("New pos = %d; Still to receive = %ld of %ld. Ctn so far='%.*s'",
271 data->in.pos,size - chunck_pos,size,(int)chunck_pos,chunk);
279 * gras_trp_buf_flush:
281 * Make sure the data is sent
284 gras_trp_buf_flush(gras_socket_t *sock) {
285 gras_error_t errcode;
287 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
288 gras_trp_bufdata_t *data=sock->bufdata;
291 size = htonl(data->out.size);
292 DEBUG1("Send the size (=%d)",data->out.size);
293 TRY(super->chunk_send(sock,(char*) &size, 4));
295 DEBUG1("Send the chunk (size=%d)",data->out.size);
296 TRY(super->chunk_send(sock, data->out.data, data->out.size));
297 VERB1("Chunk sent (size=%d)",data->out.size);