~roger.light/ubuntu/vivid/libwebsockets/fix-for-1422623

« back to all changes in this revision

Viewing changes to test-server/test-echo.c

  • Committer: Roger A. Light
  • Date: 2015-02-19 16:00:08 UTC
  • mfrom: (1.1.1)
  • Revision ID: roger@atchoo.org-20150219160008-162cd9naiu2yekny
New upstream release 1.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
#include <stdio.h>
26
26
#include <stdlib.h>
27
 
#include <unistd.h>
28
27
#include <getopt.h>
29
28
#include <string.h>
30
 
#include <sys/time.h>
31
29
#include <assert.h>
 
30
#include <signal.h>
 
31
 
 
32
#ifndef _WIN32
32
33
#include <syslog.h>
33
 
#include <signal.h>
 
34
#include <sys/time.h>
 
35
#include <unistd.h>
 
36
#endif
 
37
 
 
38
#ifdef CMAKE_BUILD
 
39
#include "lws_config.h"
 
40
#endif
34
41
 
35
42
#include "../lib/libwebsockets.h"
36
43
 
37
 
int force_exit = 0;
 
44
static volatile int force_exit = 0;
38
45
 
39
46
#define MAX_ECHO_PAYLOAD 1400
40
47
#define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
65
72
                        lwsl_err("ERROR %d writing to socket, hanging up\n", n);
66
73
                        return 1;
67
74
                }
 
75
                if (n < (int)pss->len) {
 
76
                        lwsl_err("Partial write\n");
 
77
                        return -1;
 
78
                }
68
79
                break;
69
80
 
70
81
        case LWS_CALLBACK_RECEIVE:
73
84
                        return 1;
74
85
                }
75
86
                memcpy(&pss->buf[LWS_SEND_BUFFER_PRE_PADDING], in, len);
76
 
                pss->len = len;
 
87
                pss->len = (unsigned int)len;
77
88
                libwebsocket_callback_on_writable(context, wsi);
78
89
                break;
79
90
#endif
97
108
                n = libwebsocket_write(wsi, &pss->buf[LWS_SEND_BUFFER_PRE_PADDING], pss->len, LWS_WRITE_TEXT);
98
109
                if (n < 0) {
99
110
                        lwsl_err("ERROR %d writing to socket, hanging up\n", n);
100
 
                        return 1;
 
111
                        return -1;
 
112
                }
 
113
                if (n < (int)pss->len) {
 
114
                        lwsl_err("Partial write\n");
 
115
                        return -1;
101
116
                }
102
117
                break;
103
118
#endif
153
168
        int opts = 0;
154
169
        char interface_name[128] = "";
155
170
        const char *interface = NULL;
 
171
#ifndef WIN32
156
172
        int syslog_options = LOG_PID | LOG_PERROR;
 
173
#endif
157
174
        int client = 0;
158
175
        int listen_port;
159
176
        struct lws_context_creation_info info;
190
207
#ifndef LWS_NO_DAEMONIZE
191
208
                case 'D':
192
209
                        daemonize = 1;
 
210
#ifndef WIN32
193
211
                        syslog_options &= ~LOG_PERROR;
 
212
#endif
194
213
                        break;
195
214
#endif
196
215
#ifndef LWS_NO_CLIENT
231
250
                }
232
251
        }
233
252
 
234
 
#ifndef LWS_NO_DAEMONIZE
 
253
#ifndef LWS_NO_DAEMONIZE 
235
254
        /*
236
255
         * normally lock path would be /var/lock/lwsts or similar, to
237
256
         * simplify getting started without having to take care about
238
257
         * permissions or running as root, set to /tmp/.lwsts-lock
239
258
         */
 
259
#if defined(WIN32) || defined(_WIN32)
 
260
#else
240
261
        if (!client && daemonize && lws_daemonize("/tmp/.lwstecho-lock")) {
241
262
                fprintf(stderr, "Failed to daemonize\n");
242
263
                return 1;
243
264
        }
244
265
#endif
 
266
#endif
245
267
 
 
268
#ifdef WIN32
 
269
#else
246
270
        /* we will only try to log things according to our debug_level */
247
271
        setlogmask(LOG_UPTO (LOG_DEBUG));
248
272
        openlog("lwsts", syslog_options, LOG_DAEMON);
249
273
 
250
274
        /* tell the library what debug level to emit and to send it to syslog */
251
275
        lws_set_log_level(debug_level, lwsl_emit_syslog);
252
 
 
 
276
#endif
253
277
        lwsl_notice("libwebsockets echo test - "
254
278
                        "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - "
255
279
                                                    "licensed under LGPL2.1\n");
314
338
                if (client) {
315
339
                        gettimeofday(&tv, NULL);
316
340
 
317
 
                        if (((unsigned int)tv.tv_usec - oldus) > rate_us) {
 
341
                        if (((unsigned int)tv.tv_usec - oldus) > (unsigned int)rate_us) {
318
342
                                libwebsocket_callback_on_writable_all_protocol(&protocols[0]);
319
343
                                oldus = tv.tv_usec;
320
344
                        }
328
352
        libwebsocket_context_destroy(context);
329
353
 
330
354
        lwsl_notice("libwebsockets-test-echo exited cleanly\n");
331
 
 
 
355
#ifdef WIN32
 
356
#else
332
357
        closelog();
 
358
#endif
333
359
 
334
360
        return 0;
335
361
}