mirror of
https://github.com/payden/libwsclient
synced 2024-10-27 17:54:01 +00:00
Thread out main client run loop.
No reason to block here if you don't want/need to. This frees up the developer to do other useful work while the client is running.
This commit is contained in:
parent
fda4e30b01
commit
0002a5bec3
6
test.c
6
test.c
@ -11,20 +11,20 @@ int onmessage(wsclient *c, libwsclient_message *msg) {
|
||||
|
||||
int onopen(wsclient *c) {
|
||||
fprintf(stderr, "onopen called.\n");
|
||||
libwsclient_close(c);
|
||||
libwsclient_send(c, "testing");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
wsclient *client = libwsclient_new("ws://localhost:3333");
|
||||
wsclient *client = libwsclient_new("ws://websocket.mtgox.com/mtgox");
|
||||
if(!client) {
|
||||
fprintf(stderr, "Unable to initialize new WS client.\n");
|
||||
exit(1);
|
||||
}
|
||||
libwsclient_onopen(client, &onopen);
|
||||
libwsclient_onmessage(client, &onmessage);
|
||||
libwsclient_send(client, "testing");
|
||||
libwsclient_run(client);
|
||||
libwsclient_finish(client);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
24
wsclient.c
24
wsclient.c
@ -14,8 +14,6 @@
|
||||
#include "sha1.h"
|
||||
|
||||
void libwsclient_run(wsclient *c) {
|
||||
char buf[1024];
|
||||
int n, i;
|
||||
pthread_mutex_lock(&c->lock);
|
||||
if(c->flags & CLIENT_CONNECTING) {
|
||||
pthread_mutex_unlock(&c->lock);
|
||||
@ -26,6 +24,13 @@ void libwsclient_run(wsclient *c) {
|
||||
c->URI = NULL;
|
||||
}
|
||||
pthread_mutex_unlock(&c->lock);
|
||||
pthread_create(&c->run_thread, NULL, libwsclient_run_thread, (void *)c);
|
||||
}
|
||||
|
||||
void *libwsclient_run_thread(void *ptr) {
|
||||
wsclient *c = (wsclient *)ptr;
|
||||
char buf[1024];
|
||||
int n, i;
|
||||
do {
|
||||
memset(buf, 0, 1024);
|
||||
n = recv(c->sockfd, buf, 1023, 0);
|
||||
@ -40,6 +45,10 @@ void libwsclient_run(wsclient *c) {
|
||||
free(c);
|
||||
}
|
||||
|
||||
void libwsclient_finish(wsclient *client) {
|
||||
pthread_join(client->run_thread, NULL);
|
||||
}
|
||||
|
||||
void libwsclient_onclose(wsclient *client, int (*cb)(wsclient *c)) {
|
||||
pthread_mutex_lock(&client->lock);
|
||||
client->onclose = cb;
|
||||
@ -310,7 +319,7 @@ wsclient *libwsclient_new(const char *URI) {
|
||||
client->flags |= CLIENT_CONNECTING;
|
||||
pthread_mutex_unlock(&client->lock);
|
||||
|
||||
if(pthread_create(&(client->handshake_thread), NULL, libwsclient_handshake_thread, (void *)client)) {
|
||||
if(pthread_create(&client->handshake_thread, NULL, libwsclient_handshake_thread, (void *)client)) {
|
||||
perror("pthread");
|
||||
exit(4);
|
||||
}
|
||||
@ -478,10 +487,10 @@ void *libwsclient_handshake_thread(void *ptr) {
|
||||
|
||||
pthread_mutex_lock(&client->lock);
|
||||
client->flags &= ~CLIENT_CONNECTING;
|
||||
pthread_mutex_unlock(&client->lock);
|
||||
if(client->onopen != NULL) {
|
||||
client->onopen(client);
|
||||
}
|
||||
pthread_mutex_unlock(&client->lock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -506,12 +515,9 @@ int libwsclient_send(wsclient *client, char *strdata) {
|
||||
return 0;
|
||||
}
|
||||
if(client->flags & CLIENT_CONNECTING) {
|
||||
fprintf(stderr, "Attempted to send message before client was connected. Not sending.\n");
|
||||
pthread_mutex_unlock(&client->lock);
|
||||
pthread_join(client->handshake_thread, NULL);
|
||||
pthread_mutex_lock(&client->lock);
|
||||
client->flags &= ~CLIENT_CONNECTING;
|
||||
free(client->URI);
|
||||
client->URI = NULL;
|
||||
return 0;
|
||||
}
|
||||
int sockfd = client->sockfd;
|
||||
pthread_mutex_unlock(&client->lock);
|
||||
|
@ -39,6 +39,7 @@ typedef struct _libwsclient_message {
|
||||
|
||||
typedef struct _wsclient {
|
||||
pthread_t handshake_thread;
|
||||
pthread_t run_thread;
|
||||
pthread_mutex_t lock;
|
||||
char *URI;
|
||||
int sockfd;
|
||||
@ -60,6 +61,8 @@ int stricmp(const char *s1, const char *s2);
|
||||
int libwsclient_complete_frame(libwsclient_frame *frame);
|
||||
void libwsclient_handle_control_frame(wsclient *c, libwsclient_frame *ctl_frame);
|
||||
void libwsclient_run(wsclient *c);
|
||||
void libwsclient_finish(wsclient *client);
|
||||
void *libwsclient_run_thread(void *ptr);
|
||||
void *libwsclient_handshake_thread(void *ptr);
|
||||
void libwsclient_cleanup_frames(libwsclient_frame *first);
|
||||
void libwsclient_in_data(wsclient *c, char in);
|
||||
|
Loading…
Reference in New Issue
Block a user