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 */
16 #include "transport_private.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_buf,transport,
19 "Generic buffered transport (works on top of TCP or SG)");
24 xbt_error_t gras_trp_buf_socket_client(gras_trp_plugin_t *self,
26 xbt_error_t gras_trp_buf_socket_server(gras_trp_plugin_t *self,
28 xbt_error_t gras_trp_buf_socket_accept(gras_socket_t sock,
31 void gras_trp_buf_socket_close(gras_socket_t sd);
33 xbt_error_t gras_trp_buf_chunk_send(gras_socket_t sd,
37 xbt_error_t gras_trp_buf_chunk_recv(gras_socket_t sd,
40 xbt_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 void gras_trp_buf_init_sock(gras_socket_t sock) {
68 gras_trp_bufdata_t *data=xbt_new(gras_trp_bufdata_t,1);
71 data->buffsize = 100 * 1024 ; /* 100k */
74 data->in.data = xbt_malloc(data->buffsize);
75 data->in.pos = 0; /* useless, indeed, since size==pos */
78 data->out.data = xbt_malloc(data->buffsize);
89 gras_trp_buf_setup(gras_trp_plugin_t *plug) {
91 gras_trp_buf_plug_data_t *data =xbt_new(gras_trp_buf_plug_data_t,1);
94 TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
96 DEBUG1("Derivate a buffer plugin from %s",gras_if_RL() ? "tcp" : "sg");
98 plug->socket_client = gras_trp_buf_socket_client;
99 plug->socket_server = gras_trp_buf_socket_server;
100 plug->socket_accept = gras_trp_buf_socket_accept;
101 plug->socket_close = gras_trp_buf_socket_close;
103 plug->chunk_send = gras_trp_buf_chunk_send;
104 plug->chunk_recv = gras_trp_buf_chunk_recv;
106 plug->flush = gras_trp_buf_flush;
108 plug->data = (void*)data;
114 xbt_error_t gras_trp_buf_socket_client(gras_trp_plugin_t *self,
115 /* OUT */ gras_socket_t sock){
117 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)self->data)->super;
120 TRY(super->socket_client(super,sock));
122 gras_trp_buf_init_sock(sock);
128 * gras_trp_buf_socket_server:
130 * Open a socket used to receive messages.
132 xbt_error_t gras_trp_buf_socket_server(gras_trp_plugin_t *self,
133 /* OUT */ gras_socket_t sock){
135 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)self->data)->super;
138 TRY(super->socket_server(super,sock));
140 gras_trp_buf_init_sock(sock);
145 gras_trp_buf_socket_accept(gras_socket_t sock,
146 gras_socket_t *dst) {
148 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
151 TRY(super->socket_accept(sock,dst));
152 (*dst)->plugin = sock->plugin;
153 gras_trp_buf_init_sock(*dst);
157 void gras_trp_buf_socket_close(gras_socket_t sock){
158 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
159 gras_trp_bufdata_t *data=sock->bufdata;
162 if (data->in.size || data->out.size)
163 gras_trp_buf_flush(sock);
165 xbt_free(data->in.data);
167 xbt_free(data->out.data);
170 super->socket_close(sock);
174 * gras_trp_buf_chunk_send:
176 * Send data on a TCP socket
179 gras_trp_buf_chunk_send(gras_socket_t sock,
184 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
188 /* Let underneath plugin check for direction, we work even in duplex */
189 xbt_assert0(size >= 0, "Cannot send a negative amount of data");
191 while (chunk_pos < size) {
192 /* size of the chunck to receive in that shot */
193 long int thissize = min(size-chunk_pos,data->buffsize - data->out.size);
194 DEBUG5("Set the chars %d..%ld into the buffer (size=%ld, ctn='%.*s')",
196 ((int)data->out.size) + thissize -1,
197 size, chunk_pos, chunk);
199 memcpy(data->out.data + data->out.size, chunk + chunk_pos, thissize);
201 data->out.size += thissize;
202 chunk_pos += thissize;
203 DEBUG5("New pos = %d; Still to send = %ld of %ld; ctn sofar='%.*s'",
204 data->out.size,size-chunk_pos,size,(int)chunk_pos,chunk);
206 if (data->out.size == data->buffsize) /* out of space. Flush it */
207 TRY(gras_trp_buf_flush(sock));
215 * gras_trp_buf_chunk_recv:
217 * Receive data on a TCP socket.
220 gras_trp_buf_chunk_recv(gras_socket_t sock,
225 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
226 gras_trp_bufdata_t *data=sock->bufdata;
227 long int chunck_pos = 0;
229 /* Let underneath plugin check for direction, we work even in duplex */
230 xbt_assert0(sock, "Cannot recv on an NULL socket");
231 xbt_assert0(size >= 0, "Cannot receive a negative amount of data");
235 while (chunck_pos < size) {
236 /* size of the chunck to receive in that shot */
239 if (data->in.size == data->in.pos) { /* out of data. Get more */
241 DEBUG0("Recv the size");
242 TRY(super->chunk_recv(sock,(char*)&nextsize, 4));
243 data->in.size = ntohl(nextsize);
245 VERB1("Recv the chunk (size=%d)",data->in.size);
246 TRY(super->chunk_recv(sock, data->in.data, data->in.size));
250 thissize = min(size-chunck_pos , data->in.size - data->in.pos);
251 DEBUG2("Get the chars %d..%ld out of the buffer",
253 data->in.pos + thissize - 1);
254 memcpy(chunk+chunck_pos, data->in.data + data->in.pos, thissize);
256 data->in.pos += thissize;
257 chunck_pos += thissize;
258 DEBUG5("New pos = %d; Still to receive = %ld of %ld. Ctn so far='%.*s'",
259 data->in.pos,size - chunck_pos,size,(int)chunck_pos,chunk);
267 * gras_trp_buf_flush:
269 * Make sure the data is sent
272 gras_trp_buf_flush(gras_socket_t sock) {
275 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
276 gras_trp_bufdata_t *data=sock->bufdata;
279 size = htonl(data->out.size);
280 DEBUG1("Send the size (=%d)",data->out.size);
281 TRY(super->chunk_send(sock,(char*) &size, 4));
283 DEBUG1("Send the chunk (size=%d)",data->out.size);
284 TRY(super->chunk_send(sock, data->out.data, data->out.size));
285 VERB1("Chunk sent (size=%d)",data->out.size);