aboutsummaryrefslogtreecommitdiff
path: root/src/util/server_tc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/server_tc.c')
-rw-r--r--src/util/server_tc.c198
1 files changed, 198 insertions, 0 deletions
diff --git a/src/util/server_tc.c b/src/util/server_tc.c
new file mode 100644
index 000000000..dc51e1433
--- /dev/null
+++ b/src/util/server_tc.c
@@ -0,0 +1,198 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19*/
20
21/**
22 * @file util/server_tc.c
23 * @brief convenience functions for transmission of
24 * complex responses as a server
25 * @author Christian Grothoff
26 */
27
28#include "platform.h"
29#include "gnunet_common.h"
30#include "gnunet_network_lib.h"
31#include "gnunet_scheduler_lib.h"
32#include "gnunet_server_lib.h"
33#include "gnunet_time_lib.h"
34
35
36
37/**
38 * How much buffer space do we want to have at least
39 * before transmitting another increment?
40 */
41#define MIN_BLOCK_SIZE 128
42
43
44
45struct GNUNET_SERVER_TransmitContext
46{
47 /**
48 * Which client are we transmitting to?
49 */
50 struct GNUNET_SERVER_Client *client;
51
52 /**
53 * Transmission buffer. (current offset for writing).
54 */
55 char *buf;
56
57 /**
58 * Number of bytes in buf.
59 */
60 size_t total;
61
62 /**
63 * Offset for writing in buf.
64 */
65 size_t off;
66
67 /**
68 * Timeout for this request.
69 */
70 struct GNUNET_TIME_Absolute timeout;
71};
72
73
74/**
75 * Helper function for incremental transmission of the response.
76 */
77static size_t
78transmit_response (void *cls, size_t size, void *buf)
79{
80 struct GNUNET_SERVER_TransmitContext *tc = cls;
81 size_t msize;
82 if (buf == NULL)
83 {
84 GNUNET_SERVER_receive_done (tc->client, GNUNET_SYSERR);
85 GNUNET_free_non_null (tc->buf);
86 GNUNET_free (tc);
87 return 0;
88 }
89 if (tc->total - tc->off > size)
90 msize = size;
91 else
92 msize = tc->total - tc->off;
93 memcpy (buf, &tc->buf[tc->off], msize);
94 tc->off += msize;
95 if (tc->total == tc->off)
96 {
97 GNUNET_SERVER_receive_done (tc->client, GNUNET_OK);
98 GNUNET_free_non_null (tc->buf);
99 GNUNET_free (tc);
100 }
101 else
102 {
103 if (NULL == GNUNET_SERVER_notify_transmit_ready (tc->client,
104 GNUNET_MIN
105 (MIN_BLOCK_SIZE,
106 tc->total - tc->off),
107 GNUNET_TIME_absolute_get_remaining
108 (tc->timeout),
109 &transmit_response,
110 tc))
111 {
112 GNUNET_break (0);
113 GNUNET_SERVER_receive_done (tc->client, GNUNET_SYSERR);
114 GNUNET_free_non_null (tc->buf);
115 GNUNET_free (tc);
116 }
117 }
118 return msize;
119}
120
121
122/**
123 * Create a new transmission context for the
124 * given client.
125 *
126 * @param client client to create the context for.
127 * @return NULL on error
128 */
129struct GNUNET_SERVER_TransmitContext *
130GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client)
131{
132 struct GNUNET_SERVER_TransmitContext *tc;
133
134 GNUNET_assert (client != NULL);
135 tc = GNUNET_malloc (sizeof (struct GNUNET_SERVER_TransmitContext));
136 tc->client = client;
137 return tc;
138}
139
140
141/**
142 * Append a message to the transmission context.
143 * All messages in the context will be sent by
144 * the transmit_context_run method.
145 *
146 * @param tc context to use
147 * @param data what to append to the result message
148 * @param length length of data
149 * @param type type of the message
150 */
151void
152GNUNET_SERVER_transmit_context_append (struct GNUNET_SERVER_TransmitContext
153 *tc, const void *data, size_t length,
154 uint16_t type)
155{
156 struct GNUNET_MessageHeader *msg;
157 size_t size;
158
159 GNUNET_assert (length < GNUNET_SERVER_MAX_MESSAGE_SIZE);
160 size = length + sizeof (struct GNUNET_MessageHeader);
161 GNUNET_assert (size > length);
162 tc->buf = GNUNET_realloc (tc->buf, tc->total + size);
163 msg = (struct GNUNET_MessageHeader *) &tc->buf[tc->total];
164 tc->total += size;
165 msg->size = htons (size);
166 msg->type = htons (type);
167 memcpy (&msg[1], data, length);
168}
169
170
171/**
172 * Execute a transmission context. If there is
173 * an error in the transmission, the receive_done
174 * method will be called with an error code (GNUNET_SYSERR),
175 * otherwise with GNUNET_OK.
176 *
177 * @param tc transmission context to use
178 * @param timeout when to time out and abort the transmission
179 */
180void
181GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
182 struct GNUNET_TIME_Relative timeout)
183{
184 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
185 if (NULL ==
186 GNUNET_SERVER_notify_transmit_ready (tc->client,
187 GNUNET_MIN (MIN_BLOCK_SIZE,
188 tc->total), timeout,
189 &transmit_response, tc))
190 {
191 GNUNET_break (0);
192 GNUNET_SERVER_receive_done (tc->client, GNUNET_SYSERR);
193 GNUNET_free_non_null (tc->buf);
194 GNUNET_free (tc);
195 }
196}
197
198/* end of server_tc.c */