summaryrefslogtreecommitdiff
path: root/vchat-connection.c
diff options
context:
space:
mode:
Diffstat (limited to 'vchat-connection.c')
-rw-r--r--vchat-connection.c29
1 files changed, 14 insertions, 15 deletions
diff --git a/vchat-connection.c b/vchat-connection.c
index 01cf2c2..9770115 100644
--- a/vchat-connection.c
+++ b/vchat-connection.c
@@ -197,21 +197,20 @@ void vc_sendmessage(const char *msg) {
197 writecf(FS_ERR, "Message sending fuzzy."); 197 writecf(FS_ERR, "Message sending fuzzy.");
198} 198}
199 199
200/* offset in buffer (for linebreaks at packet borders) */
201#define BUFSIZE 4096
202static char _buf[BUFSIZE];
203static size_t _buf_fill;
204 200
205/* get data from servers connection */ 201/* get data from servers connection */
206void vc_receive(void) { 202void vc_receive(void) {
203/* offset in buffer (for linebreaks at packet borders) */
204 static char buf[4096];
205 static size_t buf_fill;
207 char *endmsg; 206 char *endmsg;
208 size_t freebytes = BUFSIZE - _buf_fill; 207 size_t freebytes = sizeof(buf) - buf_fill;
209 ssize_t bytes; 208 ssize_t bytes;
210 209
211 if (!getintoption(CF_USESSL)) 210 if (!getintoption(CF_USESSL))
212 bytes = read(serverfd, _buf + _buf_fill, freebytes); 211 bytes = read(serverfd, buf + buf_fill, freebytes);
213 else 212 else
214 bytes = vc_tls_receivemessage(_buf + _buf_fill, freebytes); 213 bytes = vc_tls_receivemessage(buf + buf_fill, freebytes);
215 214
216 /* Our tls functions may require retries with handshakes etc, this is 215 /* Our tls functions may require retries with handshakes etc, this is
217 * signalled by -2 */ 216 * signalled by -2 */
@@ -236,26 +235,26 @@ void vc_receive(void) {
236 return; 235 return;
237 } 236 }
238 237
239 _buf_fill += bytes; 238 buf_fill += bytes;
240 239
241 /* as long as there are lines .. */ 240 /* as long as there are lines .. */
242 while ((endmsg = memchr(_buf, '\n', _buf_fill)) != NULL) { 241 while ((endmsg = memchr(buf, '\n', buf_fill)) != NULL) {
243 if (endmsg > _buf) { 242 if (endmsg > buf) {
244 /* Zero terminate message, optionally chomp CR */ 243 /* Zero terminate message, optionally chomp CR */
245 endmsg[0] = 0; 244 endmsg[0] = 0;
246 if (endmsg[-1] == '\r') 245 if (endmsg[-1] == '\r')
247 endmsg[-1] = 0; 246 endmsg[-1] = 0;
248 /* If terminating and chomping left us with a message, give it to line 247 /* If terminating and chomping left us with a message, give it to line
249 * handler */ 248 * handler */
250 if (_buf[0]) { 249 if (buf[0]) {
251#ifdef DEBUG 250#ifdef DEBUG
252 /* debugging? log network input! */ 251 /* debugging? log network input! */
253 fprintf(stderr, "<| %s\n", _buf); 252 fprintf(stderr, "<| %s\n", buf);
254#endif 253#endif
255 protocol_parsemsg(_buf); 254 protocol_parsemsg(buf);
256 } 255 }
257 } 256 }
258 _buf_fill -= 1 + endmsg - _buf; 257 buf_fill -= 1 + endmsg - buf;
259 memmove(_buf, endmsg + 1, _buf_fill); 258 memmove(buf, endmsg + 1, buf_fill);
260 } 259 }
261} 260}