+static int
+gras_trp_buf_recv(gras_socket_t sock, char *chunk, unsigned long int size)
+{
+
+ gras_trp_bufdata_t *data = sock->bufdata;
+ long int chunk_pos = 0;
+
+ XBT_IN;
+
+ while (chunk_pos < size) {
+ /* size of the chunk to receive in that shot */
+ long int thissize;
+
+ if (data->in_buf.size == data->in_buf.pos) { /* out of data. Get more */
+
+ DEBUG2("Get more data (size=%d,bufsize=%d)",
+ (int) MIN(size - chunk_pos, data->buffsize),
+ (int) data->buffsize);
+
+
+ data->in_buf.size =
+ gras_trp_tcp_recv_withbuffer(sock, data->in_buf.data,
+ MIN(size - chunk_pos, data->buffsize),
+ data->buffsize);
+
+ data->in_buf.pos = 0;
+ }
+
+ thissize = min(size - chunk_pos, data->in_buf.size - data->in_buf.pos);
+ memcpy(chunk + chunk_pos, data->in_buf.data + data->in_buf.pos, thissize);
+
+ data->in_buf.pos += thissize;
+ chunk_pos += thissize;
+ DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)",
+ data->in_buf.pos, size - chunk_pos, size,
+ hexa_str((unsigned char *) chunk, chunk_pos, 0));
+ }
+ /* indicate on need to the gras_select function that there is more to read on this socket so that it does not actually select */
+ sock->moredata = (data->in_buf.size > data->in_buf.pos);
+ DEBUG1("There is %smore data", (sock->moredata ? "" : "no "));
+
+ XBT_OUT;
+ return chunk_pos;
+}
+
+/*****************************************/
+/****[ end of BUFFERED DATA EXCHANGE ]****/
+/*****************************************/
+
+/********************************/
+/****[ VECTOR DATA EXCHANGE ]****/
+/********************************/
+#ifdef HAVE_READV
+static void
+gras_trp_iov_send(gras_socket_t sock,
+ const char *chunk, unsigned long int size, int stable)
+{
+ struct iovec elm;
+ gras_trp_bufdata_t *data = (gras_trp_bufdata_t *) sock->bufdata;
+
+
+ DEBUG1("Buffer one chunk to be sent later (%s)",
+ hexa_str((char *) chunk, size, 0));
+
+ elm.iov_len = (size_t) size;
+
+ if (!stable) {
+ /* data storage won't last until flush. Save it in a buffer if we can */
+
+ if (size > data->buffsize - data->out_buf.size) {
+ /* buffer too small:
+ flush the socket, using data in its actual storage */
+ elm.iov_base = (void *) chunk;
+ xbt_dynar_push(data->out_buf_v, &elm);
+
+ gras_trp_bufiov_flush(sock);
+ return;
+ } else {
+ /* buffer big enough:
+ copy data into it, and chain it for upcoming writev */
+ memcpy(data->out_buf.data + data->out_buf.size, chunk, size);
+ elm.iov_base = (void *) (data->out_buf.data + data->out_buf.size);
+ data->out_buf.size += size;
+
+ xbt_dynar_push(data->out_buf_v, &elm);
+ }
+
+ } else {
+ /* data storage stable. Chain it */
+
+ elm.iov_base = (void *) chunk;
+ xbt_dynar_push(data->out_buf_v, &elm);
+ }
+}
+
+static int
+gras_trp_iov_recv(gras_socket_t sock, char *chunk, unsigned long int size)
+{
+ struct iovec elm;
+
+ DEBUG0("Buffer one chunk to be received later");
+ elm.iov_base = (void *) chunk;
+ elm.iov_len = (size_t) size;
+ xbt_dynar_push(sock->bufdata->in_buf_v, &elm);
+
+ return size;
+}
+
+#endif
+/***************************************/
+/****[ end of VECTOR DATA EXCHANGE ]****/
+/***************************************/
+
+
+/***
+ *** Prototypes of BUFFERED
+ ***/
+
+void gras_trp_buf_socket_client(gras_trp_plugin_t self, gras_socket_t sock);
+void gras_trp_buf_socket_server(gras_trp_plugin_t self, gras_socket_t sock);
+gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock);
+
+void gras_trp_buf_socket_close(gras_socket_t sd);
+
+
+gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock)
+{
+ gras_trp_bufdata_t *data = xbt_new(gras_trp_bufdata_t, 1);
+
+ data->buffsize = 100 * 1024; /* 100k */
+
+ data->in_buf.size = 0;
+ data->in_buf.data = xbt_malloc(data->buffsize);
+ data->in_buf.pos = 0; /* useless, indeed, since size==pos */
+
+ data->out_buf.size = 0;
+ data->out_buf.data = xbt_malloc(data->buffsize);
+ data->out_buf.pos = data->out_buf.size;
+
+#ifdef HAVE_READV
+ data->in_buf_v = data->out_buf_v = NULL;
+ data->in_buf_v = xbt_dynar_new(sizeof(struct iovec), NULL);
+ data->out_buf_v = xbt_dynar_new(sizeof(struct iovec), NULL);
+ data->out = buffering_iov;
+#else
+ data->out = buffering_buf;
+#endif
+
+ data->in = buffering_buf;
+
+ sock->bufdata = data;
+ return sock;
+}
+
+/***
+ *** Code
+ ***/
+void gras_trp_tcp_setup(gras_trp_plugin_t plug)
+{
+
+ plug->socket_client = gras_trp_buf_socket_client;
+ plug->socket_server = gras_trp_buf_socket_server;
+ plug->socket_accept = gras_trp_buf_socket_accept;
+ plug->socket_close = gras_trp_buf_socket_close;
+
+#ifdef HAVE_READV
+ plug->send = gras_trp_iov_send;
+#else
+ plug->send = gras_trp_buf_send;
+#endif
+ plug->recv = gras_trp_buf_recv;
+
+ plug->raw_send = gras_trp_tcp_send;
+ plug->raw_recv = gras_trp_tcp_recv;
+
+ plug->flush = gras_trp_bufiov_flush;
+
+ plug->data = NULL;
+ plug->exit = NULL;
+}
+
+void gras_trp_buf_socket_client(gras_trp_plugin_t self,
+ /* OUT */ gras_socket_t sock)
+{
+
+ gras_trp_sock_socket_client(NULL, sock);
+ gras_trp_buf_init_sock(sock);
+}
+
+/**
+ * gras_trp_buf_socket_server:
+ *
+ * Open a socket used to receive messages.
+ */
+void gras_trp_buf_socket_server(gras_trp_plugin_t self,
+ /* OUT */ gras_socket_t sock)
+{
+
+ gras_trp_sock_socket_server(NULL, sock);
+ gras_trp_buf_init_sock(sock);
+}
+
+gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock)
+{
+ return gras_trp_buf_init_sock(gras_trp_sock_socket_accept(sock));
+}
+
+void gras_trp_buf_socket_close(gras_socket_t sock)
+{
+ gras_trp_bufdata_t *data = sock->bufdata;
+
+ if (data->in_buf.size != data->in_buf.pos) {
+ WARN3("Socket closed, but %d bytes were unread (size=%d,pos=%d)",
+ data->in_buf.size - data->in_buf.pos,
+ data->in_buf.size, data->in_buf.pos);
+ }
+ if (data->in_buf.data)
+ free(data->in_buf.data);
+
+ if (data->out_buf.size != data->out_buf.pos) {
+ DEBUG2("Flush the socket before closing (in=%d,out=%d)",
+ data->in_buf.size, data->out_buf.size);
+ gras_trp_bufiov_flush(sock);
+ }
+ if (data->out_buf.data)
+ free(data->out_buf.data);
+
+#ifdef HAVE_READV
+ if (data->in_buf_v) {
+ if (xbt_dynar_length(data->in_buf_v))
+ WARN0("Socket closed, but some bytes were unread");
+ xbt_dynar_free(&data->in_buf_v);
+ }
+ if (data->out_buf_v) {
+ if (xbt_dynar_length(data->out_buf_v)) {
+ DEBUG0("Flush the socket before closing");
+ gras_trp_bufiov_flush(sock);
+ }
+ xbt_dynar_free(&data->out_buf_v);
+ }
+#endif
+
+ free(data);
+ gras_trp_sock_socket_close(sock);
+}
+
+/****************************/
+/****[ HELPER FUNCTIONS ]****/
+/****************************/