3 /* buf trp (transport) - buffered transport using the TCP one */
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include <string.h> /* memset */
15 #include "xbt/sysdep.h"
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 hexa_print(const char*name, unsigned char *data, int size); /* in gras.c */
26 xbt_error_t gras_trp_buf_socket_client(gras_trp_plugin_t *self,
28 xbt_error_t gras_trp_buf_socket_server(gras_trp_plugin_t *self,
30 xbt_error_t gras_trp_buf_socket_accept(gras_socket_t sock,
33 void gras_trp_buf_socket_close(gras_socket_t sd);
35 xbt_error_t gras_trp_buf_chunk_send(gras_socket_t sd,
39 xbt_error_t gras_trp_buf_chunk_recv(gras_socket_t sd,
42 xbt_error_t gras_trp_buf_flush(gras_socket_t sock);
46 *** Specific plugin part
50 gras_trp_plugin_t *super;
51 } gras_trp_buf_plug_data_t;
54 *** Specific socket part
60 int pos; /* for receive; not exchanged over the net */
63 struct gras_trp_bufdata_{
69 void gras_trp_buf_init_sock(gras_socket_t sock) {
70 gras_trp_bufdata_t *data=xbt_new(gras_trp_bufdata_t,1);
73 data->buffsize = 100 * 1024 ; /* 100k */
76 data->in.data = xbt_malloc(data->buffsize);
77 data->in.pos = 0; /* useless, indeed, since size==pos */
79 /* In SG, the 4 first bytes are for the chunk size as htonl'ed, so that we can send it in one shoot.
80 * This is mandatory in SG because all emissions go to the same channel, so if we split them,
81 * they can get mixed. */
82 data->out.size = gras_if_RL()?0:4;
83 data->out.data = xbt_malloc(data->buffsize);
84 data->out.pos = data->out.size;
94 gras_trp_buf_setup(gras_trp_plugin_t *plug) {
96 gras_trp_buf_plug_data_t *data =xbt_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 xbt_error_t gras_trp_buf_socket_client(gras_trp_plugin_t *self,
120 /* OUT */ gras_socket_t sock){
122 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)self->data)->super;
125 TRY(super->socket_client(super,sock));
127 gras_trp_buf_init_sock(sock);
133 * gras_trp_buf_socket_server:
135 * Open a socket used to receive messages.
137 xbt_error_t gras_trp_buf_socket_server(gras_trp_plugin_t *self,
138 /* OUT */ gras_socket_t sock){
140 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)self->data)->super;
143 TRY(super->socket_server(super,sock));
145 gras_trp_buf_init_sock(sock);
150 gras_trp_buf_socket_accept(gras_socket_t sock,
151 gras_socket_t *dst) {
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 gras_trp_buf_init_sock(*dst);
163 void gras_trp_buf_socket_close(gras_socket_t sock){
164 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
165 gras_trp_bufdata_t *data=sock->bufdata;
168 if (data->in.size!=data->in.pos) {
169 WARN1("Socket closed, but %d bytes were unread",data->in.size - data->in.pos);
172 if (data->out.size!=data->out.pos) {
173 DEBUG2("Flush the socket before closing (in=%d,out=%d)",data->in.size, data->out.size);
174 gras_trp_buf_flush(sock);
180 free(data->out.data);
183 super->socket_close(sock);
187 * gras_trp_buf_chunk_send:
189 * Send data on a TCP socket
192 gras_trp_buf_chunk_send(gras_socket_t sock,
197 gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
201 /* Let underneath plugin check for direction, we work even in duplex */
202 xbt_assert0(size >= 0, "Cannot send a negative amount of data");
204 while (chunk_pos < size) {
205 /* size of the chunck to receive in that shot */
206 long int thissize = min(size-chunk_pos,data->buffsize - data->out.size);
207 DEBUG5("Set the chars %d..%ld into the buffer (size=%ld, ctn='%.*s')",
209 ((int)data->out.size) + thissize -1,
210 size, chunk_pos, chunk);
212 memcpy(data->out.data + data->out.size, chunk + chunk_pos, thissize);
214 data->out.size += thissize;
215 chunk_pos += thissize;
216 DEBUG5("New pos = %d; Still to send = %ld of %ld; ctn sofar='%.*s'",
217 data->out.size,size-chunk_pos,size,(int)chunk_pos,chunk);
219 if (data->out.size == data->buffsize) /* out of space. Flush it */
220 TRY(gras_trp_buf_flush(sock));
228 * gras_trp_buf_chunk_recv:
230 * Receive data on a TCP socket.
233 gras_trp_buf_chunk_recv(gras_socket_t sock,
238 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
239 gras_trp_bufdata_t *data=sock->bufdata;
240 long int chunck_pos = 0;
242 /* Let underneath plugin check for direction, we work even in duplex */
243 xbt_assert0(sock, "Cannot recv on an NULL socket");
244 xbt_assert0(size >= 0, "Cannot receive a negative amount of data");
248 while (chunck_pos < size) {
249 /* size of the chunck to receive in that shot */
252 if (data->in.size == data->in.pos) { /* out of data. Get more */
255 DEBUG0("Recv the size");
256 TRY(super->chunk_recv(sock,(char*)&nextsize, 4));
257 data->in.size = (int)ntohl(nextsize);
258 VERB1("Recv the chunk (size=%d)",data->in.size);
263 TRY(super->chunk_recv(sock, data->in.data, data->in.size));
268 memcpy((char*)&nextsize,data->in.data,4);
269 data->in.size = (int)ntohl(nextsize)+4;
271 VERB3("Got the chunk (size=%d+4 for the size ifself)='%.*s'",data->in.size-4,
272 data->in.size,data->in.data);
273 if (XBT_LOG_ISENABLED(trp_buf,xbt_log_priority_debug))
274 hexa_print("chunck received",data->in.data,data->in.size);
279 thissize = min(size-chunck_pos , data->in.size - data->in.pos);
280 DEBUG2("Get the chars %d..%ld out of the buffer",
282 data->in.pos + thissize - 1);
283 memcpy(chunk+chunck_pos, data->in.data + data->in.pos, thissize);
285 data->in.pos += thissize;
286 chunck_pos += thissize;
287 DEBUG5("New pos = %d; Still to receive = %ld of %ld. Ctn so far='%.*s'",
288 data->in.pos,size - chunck_pos,size,(int)chunck_pos,chunk);
296 * gras_trp_buf_flush:
298 * Make sure the data is sent
301 gras_trp_buf_flush(gras_socket_t sock) {
304 gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
305 gras_trp_bufdata_t *data=sock->bufdata;
309 if (XBT_LOG_ISENABLED(trp_buf,xbt_log_priority_debug))
310 hexa_print("chunck to send",data->out.data,data->out.size);
311 if ((data->out.size - data->out.pos) == (gras_if_RL()?0:4) ) { /* 4 first bytes=size in SG mode*/
312 DEBUG2("Nothing to flush (size=%d; pos=%d)",data->out.size,data->out.pos);
316 size = (int)htonl(data->out.size - data->out.pos);
317 DEBUG4("%s the size (=%d) to %s:%d",(gras_if_RL()?"Send":"Embeed"),data->out.size-data->out.pos,
318 gras_socket_peer_name(sock),gras_socket_peer_port(sock));
320 TRY(super->chunk_send(sock,(char*) &size, 4));
322 memcpy(data->out.data, &size, 4);
326 DEBUG3("Send the chunk (size=%d) to %s:%d",data->out.size,
327 gras_socket_peer_name(sock),gras_socket_peer_port(sock));
328 TRY(super->chunk_send(sock, data->out.data, data->out.size));
329 VERB1("Chunk sent (size=%d)",data->out.size);
330 if (XBT_LOG_ISENABLED(trp_buf,xbt_log_priority_debug))
331 hexa_print("chunck sent",data->out.data,data->out.size);