2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/server_tc.c
23 * @brief convenience functions for transmission of
24 * complex responses as a server
25 * @author Christian Grothoff
29 #include "gnunet_common.h"
30 #include "gnunet_connection_lib.h"
31 #include "gnunet_scheduler_lib.h"
32 #include "gnunet_server_lib.h"
33 #include "gnunet_time_lib.h"
36 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
40 * How much buffer space do we want to have at least
41 * before transmitting another increment?
43 #define MIN_BLOCK_SIZE 128
47 struct GNUNET_SERVER_TransmitContext
50 * Which client are we transmitting to?
52 struct GNUNET_SERVER_Client *client;
55 * Transmission buffer. (current offset for writing).
60 * Number of bytes in buf.
65 * Offset for writing in buf.
70 * Timeout for this request.
72 struct GNUNET_TIME_Absolute timeout;
77 * Helper function for incremental transmission of the response.
80 transmit_response (void *cls, size_t size, void *buf)
82 struct GNUNET_SERVER_TransmitContext *tc = cls;
87 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR);
90 if (tc->total - tc->off > size)
93 msize = tc->total - tc->off;
94 memcpy (buf, &tc->buf[tc->off], msize);
96 if (tc->total == tc->off)
99 GNUNET_SERVER_receive_done (tc->client, GNUNET_OK);
100 GNUNET_SERVER_client_drop (tc->client);
101 GNUNET_free_non_null (tc->buf);
107 GNUNET_SERVER_notify_transmit_ready (tc->client,
108 GNUNET_MIN (MIN_BLOCK_SIZE,
109 tc->total - tc->off),
110 GNUNET_TIME_absolute_get_remaining
111 (tc->timeout), &transmit_response,
115 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR);
123 * Create a new transmission context for the
126 * @param client client to create the context for.
127 * @return NULL on error
129 struct GNUNET_SERVER_TransmitContext *
130 GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client)
132 struct GNUNET_SERVER_TransmitContext *tc;
134 GNUNET_assert (client != NULL);
135 tc = GNUNET_malloc (sizeof (struct GNUNET_SERVER_TransmitContext));
136 GNUNET_SERVER_client_keep (client);
143 * Append a message to the transmission context.
144 * All messages in the context will be sent by
145 * the transmit_context_run method.
147 * @param tc context to use
148 * @param data what to append to the result message
149 * @param length length of data
150 * @param type type of the message
153 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
154 *tc, const void *data,
155 size_t length, uint16_t type)
157 struct GNUNET_MessageHeader *msg;
160 GNUNET_assert (length < GNUNET_SERVER_MAX_MESSAGE_SIZE);
161 size = length + sizeof (struct GNUNET_MessageHeader);
162 GNUNET_assert (size > length);
163 tc->buf = GNUNET_realloc (tc->buf, tc->total + size);
164 msg = (struct GNUNET_MessageHeader *) &tc->buf[tc->total];
166 msg->size = htons (size);
167 msg->type = htons (type);
168 memcpy (&msg[1], data, length);
173 * Append a message to the transmission context.
174 * All messages in the context will be sent by
175 * the transmit_context_run method.
177 * @param tc context to use
178 * @param msg message to append
181 GNUNET_SERVER_transmit_context_append_message (struct
182 GNUNET_SERVER_TransmitContext
184 const struct GNUNET_MessageHeader
187 struct GNUNET_MessageHeader *m;
190 size = ntohs (msg->size);
191 tc->buf = GNUNET_realloc (tc->buf, tc->total + size);
192 m = (struct GNUNET_MessageHeader *) &tc->buf[tc->total];
194 memcpy (m, msg, size);
199 * Execute a transmission context. If there is
200 * an error in the transmission, the receive_done
201 * method will be called with an error code (GNUNET_SYSERR),
202 * otherwise with GNUNET_OK.
204 * @param tc transmission context to use
205 * @param timeout when to time out and abort the transmission
208 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
209 struct GNUNET_TIME_Relative timeout)
211 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
213 GNUNET_SERVER_notify_transmit_ready (tc->client,
214 GNUNET_MIN (MIN_BLOCK_SIZE,
216 &transmit_response, tc))
219 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR);
225 * Destroy a transmission context. This function must not be called
226 * after 'GNUNET_SERVER_transmit_context_run'.
228 * @param tc transmission context to destroy
229 * @param success code to give to 'GNUNET_SERVER_receive_done' for
230 * the client: GNUNET_OK to keep the connection open and
231 * continue to receive
232 * GNUNET_NO to close the connection (normal behavior)
233 * GNUNET_SYSERR to close the connection (signal
237 GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext
240 GNUNET_SERVER_receive_done (tc->client, success);
241 GNUNET_SERVER_client_drop (tc->client);
242 GNUNET_free_non_null (tc->buf);
247 /* end of server_tc.c */