Skip to content

Commit 5536791

Browse files
committed
fix borked configure options. client multi-threading bug fixes. cleanup
1 parent 6fdee56 commit 5536791

File tree

7 files changed

+59
-73
lines changed

7 files changed

+59
-73
lines changed

Makefile.am

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ libwsclient_a_SOURCES = $(ECHO_CLIENT) $(COMMON) $(WEBSOCKET_CLIENT)
1717
libwsserver_a_SOURCES = $(ECHO_SERVER) $(COMMON) $(WEBSOCKET_SERVER)
1818

1919
AM_CFLAGS = -Wall
20+
AM_LDFLAGS =
2021

2122
if DEBUG
22-
AM_CFLAGS += -g3
23+
AM_CFLAGS += -g3
2324
else
24-
AM_CFLAGS += -O3
25+
AM_CFLAGS += -O3
2526
endif
2627

2728
so:

autogen.sh

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
#!/bin/sh
22

3-
aclocal --install -I m4 &&
4-
autoreconf -i &&
5-
autoheader
3+
aclocal --install -I m4 && autoreconf -i -v

configure.ac

+25-33
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
AC_PREREQ([2.69])
55
AC_INIT([cwebsocket], [0.01], [root@localhost])
6-
AM_INIT_AUTOMAKE([1.9 foreign])
6+
AM_INIT_AUTOMAKE([cwebsocket], [0.01])
77
AC_CONFIG_SRCDIR([src/websocket-client.c])
88
AC_CONFIG_HEADERS([config.h])
99
AC_CONFIG_MACRO_DIR([m4])
@@ -56,50 +56,42 @@ AC_FUNC_MALLOC
5656
AC_CHECK_FUNCS([gettimeofday memset socket strcasecmp strchr strerror strstr])
5757

5858
AC_CONFIG_FILES([Makefile])
59-
AC_OUTPUT
6059

6160
# Conditionals
6261
AC_ARG_ENABLE(debug,
63-
AS_HELP_STRING(
64-
[--enable-debug],
65-
[Include debug symbols and enable verbose logging - default: yes]),
66-
[case "${enableval}" in
67-
yes) debug=true ;;
68-
no) debug=false ;;
69-
*) AC_MSG_ERROR([bad value ${enableval} for --enable-debug]) ;;
70-
esac],
71-
[debug=true])
72-
AM_CONDITIONAL(DEBUG, test x"$debug" = x"true")
73-
AM_COND_IF(DEBUG,
74-
AC_DEFINE(DEBUG, 1, [Define to 0 if this is a release build]),
75-
AC_DEFINE(DEBUG, 0, [Define to 1 or higher if this is a debug build]))
62+
[--enable-debug Turn on debugging],
63+
[case "${enableval}" in
64+
yes) debug=true ;;
65+
no) debug=false ;;
66+
*) AC_MSG_ERROR(bad value ${enableval} for --enable-debug) ;;
67+
esac],[debug=true])
68+
AM_CONDITIONAL([DEBUG], [test "$debug" = true])
7669

7770
AC_ARG_ENABLE(ssl,
7871
AS_HELP_STRING(
7972
[--enable-ssl],
80-
[WebSocket SSL support - default: yes]),
73+
[enable ssl, default: enabled]),
8174
[case "${enableval}" in
8275
yes) ssl=true ;;
8376
no) ssl=false ;;
8477
*) AC_MSG_ERROR([bad value ${enableval} for --enable-ssl]) ;;
8578
esac],
8679
[ssl=true])
87-
AM_CONDITIONAL(USESSL, test x"$ssl" = x"true")
88-
AM_COND_IF(USESSL,
89-
AC_DEFINE(USESSL, 1, [Set to 1 to enable SSL support]),
90-
AC_DEFINE(USESSL, 0, [Set to 0 to disable SSL support]))
80+
#AM_CONDITIONAL(USESSL, test x"$ssl" = x"true")
81+
if test "$ssl" = true; then
82+
AC_DEFINE(ENABLE_SSL, [1], ["Compile with SSL support"])
83+
fi
9184

9285
AC_ARG_ENABLE(threads,
93-
AS_HELP_STRING(
94-
[--enable-threads],
95-
[Enable multi-threading - default: yes]),
96-
[case "${enableval}" in
97-
yes) threads=true ;;
98-
no) threads=false ;;
99-
*) AC_MSG_ERROR([bad value ${enableval} for --enable-threads]) ;;
100-
esac],
101-
[threads=true])
102-
AM_CONDITIONAL(THREADED, test x"$threads" = x"true")
103-
AM_COND_IF(THREADED,
104-
AC_DEFINE(THREADED, 1, [Set to 1 to enable pthread support]),
105-
AC_DEFINE(THREADED, 0, [Set to 0 to disable pthread support]))
86+
[--enable-threads Enable multi-threading],
87+
[case "${enableval}" in
88+
yes) threads=true ;;
89+
no) threads=false ;;
90+
*) AC_MSG_ERROR(bad value ${enableval} for --enable-threads) ;;
91+
esac],[threads=true])
92+
#AM_CONDITIONAL([THREADED], [test "x$threads" = "xtrue"])
93+
if test "$threads" = true; then
94+
AC_DEFINE(ENABLE_THREADS, [1], ["Compile with client multi-threading support"])
95+
fi
96+
97+
AC_OUTPUT

src/cwebsocket/client.c

+19-19
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void cwebsocket_client_parse_uri(cwebsocket_client *websocket, const char *uri,
8080
strcpy(querystring, "");
8181
return;
8282
}
83-
#ifdef USESSL
83+
#ifdef ENABLE_SSL
8484
else if(sscanf(uri, "wss://%[^:]:%[^/]%[^?]%s", hostname, port, resource, querystring) == 4) {
8585
websocket->flags |= WEBSOCKET_FLAG_SSL;
8686
return;
@@ -137,7 +137,7 @@ int cwebsocket_client_connect(cwebsocket_client *websocket) {
137137
return -1;
138138
}
139139

140-
#ifdef THREADED
140+
#ifdef ENABLE_THREADS
141141
if(pthread_mutex_init(&websocket->lock, NULL) != 0) {
142142
syslog(LOG_ERR, "cwebsocket_client_connect: unable to initialize websocket mutex: %s\n", strerror(errno));
143143
cwebsocket_client_onerror(websocket, strerror(errno));
@@ -239,7 +239,7 @@ int cwebsocket_client_connect(cwebsocket_client *websocket) {
239239
return -1;
240240
}
241241

242-
#ifdef USESSL
242+
#ifdef ENABLE_SSL
243243

244244
websocket->ssl = NULL;
245245
websocket->sslctx = NULL;
@@ -275,7 +275,7 @@ int cwebsocket_client_connect(cwebsocket_client *websocket) {
275275
}
276276
#endif
277277

278-
#ifdef THREADED
278+
#ifdef ENABLE_THREADS
279279
pthread_mutex_lock(&websocket->lock);
280280
websocket->state = WEBSOCKET_STATE_CONNECTED;
281281
pthread_mutex_unlock(&websocket->lock);
@@ -295,7 +295,7 @@ int cwebsocket_client_connect(cwebsocket_client *websocket) {
295295
return -1;
296296
}
297297

298-
#ifdef THREADED
298+
#ifdef ENABLE_THREADS
299299
pthread_mutex_lock(&websocket->lock);
300300
websocket->state = WEBSOCKET_STATE_OPEN;
301301
pthread_mutex_unlock(&websocket->lock);
@@ -426,9 +426,9 @@ void cwebsocket_client_listen(cwebsocket_client *websocket) {
426426
syslog(LOG_DEBUG, "cwebsocket_client_listen: shutting down");
427427
}
428428

429-
#ifdef THREADED
430-
void *cwebsocket_onmessage_thread(void *ptr) {
431-
cwebsocket_thread_args *args = (cwebsocket_thread_args *)ptr;
429+
#ifdef ENABLE_THREADS
430+
void *cwebsocket_client_onmessage_thread(void *ptr) {
431+
cwebsocket_client_thread_args *args = (cwebsocket_client_thread_args *)ptr;
432432
cwebsocket_client_onmessage(args->socket, args->message);
433433
free(args->message->payload);
434434
free(args->message);
@@ -617,7 +617,7 @@ int cwebsocket_client_read_data(cwebsocket_client *websocket) {
617617

618618
if(websocket->subprotocol != NULL && websocket->subprotocol->onmessage != NULL) {
619619

620-
#ifdef THREADED
620+
#ifdef ENABLE_THREADS
621621
cwebsocket_message *message = malloc(sizeof(cwebsocket_message));
622622
if(message == NULL) {
623623
perror("out of memory");
@@ -634,12 +634,12 @@ int cwebsocket_client_read_data(cwebsocket_client *websocket) {
634634
strncpy(message->payload, payload, payload_length+1);
635635
free(payload);
636636

637-
cwebsocket_client_thread_args *args = malloc(sizeof(cwebsocket_thread_args));
637+
cwebsocket_client_thread_args *args = malloc(sizeof(cwebsocket_client_thread_args));
638638
if(args == NULL) {
639639
perror("out of memory");
640640
exit(-1);
641641
}
642-
memset(args, 0, sizeof(cwebsocket_thread_args));
642+
memset(args, 0, sizeof(cwebsocket_client_thread_args));
643643
args->socket = websocket;
644644
args->message = message;
645645

@@ -679,14 +679,14 @@ int cwebsocket_client_read_data(cwebsocket_client *websocket) {
679679

680680
if(websocket->subprotocol->onmessage != NULL) {
681681

682-
#ifdef THREADED
682+
#ifdef ENABLE_THREADS
683683
cwebsocket_message *message = malloc(sizeof(cwebsocket_message));
684684
message->opcode = frame.opcode;
685685
message->payload_len = frame.payload_len;
686686
message->payload = malloc(sizeof(char) * payload_length);
687687
memcpy(message->payload, payload, payload_length);
688688

689-
cwebsocket_thread_args *args = malloc(sizeof(cwebsocket_thread_args));
689+
cwebsocket_client_thread_args *args = malloc(sizeof(cwebsocket_client_thread_args));
690690
args->socket = websocket;
691691
args->message = message;
692692

@@ -848,7 +848,7 @@ void cwebsocket_client_close(cwebsocket_client *websocket, uint16_t code, const
848848
return;
849849
}
850850

851-
#ifdef THREADED
851+
#ifdef ENABLE_THREADS
852852
pthread_mutex_lock(&websocket->lock);
853853
websocket->state = WEBSOCKET_STATE_CLOSING;
854854
pthread_mutex_unlock(&websocket->lock);
@@ -876,7 +876,7 @@ void cwebsocket_client_close(cwebsocket_client *websocket, uint16_t code, const
876876
cwebsocket_client_send_control_frame(websocket, CLOSE, "CLOSE", NULL, 0);
877877
}
878878

879-
#ifdef USESSL
879+
#ifdef ENABLE_SSL
880880
if(websocket->ssl != NULL) {
881881
SSL_shutdown(websocket->ssl);
882882
SSL_free(websocket->ssl);
@@ -899,7 +899,7 @@ void cwebsocket_client_close(cwebsocket_client *websocket, uint16_t code, const
899899

900900
cwebsocket_client_onclose(websocket, code32, message);
901901

902-
#ifdef THREADED
902+
#ifdef ENABLE_THREADS
903903
pthread_mutex_lock(&websocket->lock);
904904
websocket->state = WEBSOCKET_STATE_CLOSED;
905905
pthread_mutex_unlock(&websocket->lock);
@@ -917,7 +917,7 @@ void cwebsocket_client_close(cwebsocket_client *websocket, uint16_t code, const
917917
}
918918

919919
ssize_t inline cwebsocket_client_read(cwebsocket_client *websocket, void *buf, int len) {
920-
#ifdef USESSL
920+
#ifdef ENABLE_SSL
921921
return (websocket->flags & WEBSOCKET_FLAG_SSL) ?
922922
SSL_read(websocket->ssl, buf, len) :
923923
read(websocket->fd, buf, len);
@@ -927,7 +927,7 @@ ssize_t inline cwebsocket_client_read(cwebsocket_client *websocket, void *buf, i
927927
}
928928

929929
ssize_t inline cwebsocket_client_write(cwebsocket_client *websocket, void *buf, int len) {
930-
#ifdef THREADED
930+
#ifdef ENABLE_THREADS
931931
ssize_t bytes_written;
932932
pthread_mutex_lock(&websocket->write_lock);
933933
#ifdef USESSL
@@ -940,7 +940,7 @@ ssize_t inline cwebsocket_client_write(cwebsocket_client *websocket, void *buf,
940940
pthread_mutex_unlock(&websocket->write_lock);
941941
return bytes_written;
942942
#else
943-
#ifdef USESSL
943+
#ifdef ENABLE_SSL
944944
return (websocket->flags & WEBSOCKET_FLAG_SSL) ?
945945
SSL_write(websocket->ssl, buf, len) :
946946
write(websocket->fd, buf, len);

src/cwebsocket/client.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ typedef struct _cwebsocket {
4444
char *uri;
4545
uint8_t flags;
4646
uint8_t state;
47-
#ifdef USESSL
47+
#ifdef ENABLE_SSL
4848
SSL_CTX *sslctx;
4949
SSL *ssl;
5050
#endif
51-
#ifdef THREADED
51+
#ifdef ENABLE_THREADS
5252
pthread_t thread;
5353
pthread_mutex_t lock;
5454
pthread_mutex_t write_lock;
@@ -61,7 +61,7 @@ typedef struct _cwebsocket {
6161
typedef struct {
6262
cwebsocket_client *socket;
6363
cwebsocket_message *message;
64-
} cwebsocket_thread_args;
64+
} cwebsocket_client_thread_args;
6565

6666
// "public"
6767
void cwebsocket_client_init(cwebsocket_client *websocket, cwebsocket_subprotocol *subprotocols[], int subprotocol_len);

src/cwebsocket/common.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@
4545
#include <openssl/buffer.h>
4646
#include "utf8.h"
4747

48-
#include <limits.h>
48+
#ifdef HAVE_CONFIG_H
49+
#include "../../config.h"
50+
#endif
4951

50-
#ifdef USESSL
52+
#ifdef ENABLE_SSL
5153
#include <openssl/rand.h>
5254
#include <openssl/ssl.h>
5355
#include <openssl/err.h>

src/websocket-client.c

+4-11
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,17 @@ int main(int argc, char **argv) {
109109
openlog("cwebsocket", LOG_CONS | LOG_PERROR, LOG_USER);
110110
syslog(LOG_DEBUG, "starting cwebsocket client");
111111

112-
113-
//cwebsocket_client_init(&websocket_client, NULL, 0);
114-
115-
116-
cwebsocket_subprotocol *subprotocols[1];
117-
subprotocols[0] = cwebsocket_subprotocol_echo_client_new();
118-
cwebsocket_client_init(&websocket_client, subprotocols, 1);
119-
112+
cwebsocket_client_init(&websocket_client, NULL, 0);
113+
websocket_client.subprotocol = cwebsocket_subprotocol_echo_client_new(); // Hardcoding instead of negotiating
120114
websocket_client.uri = argv[1];
121-
//websocket_client.flags |= WEBSOCKET_FLAG_AUTORECONNECT; // OPTIONAL - retry failed connections
122-
//websocket_client.retry = 5; // OPTIONAL - seconds to wait before retrying
115+
//websocket_client.flags |= WEBSOCKET_FLAG_AUTORECONNECT; // OPTIONAL - retry failed connections
116+
//websocket_client.retry = 5; // OPTIONAL - seconds to wait before retrying
123117
if(cwebsocket_client_connect(&websocket_client) == -1) {
124118
return main_exit(EXIT_FAILURE);
125119
}
126120

127121
run_websocket_org_echo_test(&websocket_client);
128122

129123
cwebsocket_client_close(&websocket_client, 1000, "main: run loop complete");
130-
//free(subprotocols[0]);
131124
return main_exit(EXIT_SUCCESS);
132125
}

0 commit comments

Comments
 (0)