Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
  • Loading branch information
Jianhui Zhao committed Sep 8, 2018
1 parent dc664e2 commit f09eef4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 45 deletions.
35 changes: 17 additions & 18 deletions example/helloworld.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ static void stdin_read_cb(struct ev_loop *loop, struct ev_io *w, int revents)
char buf[128] = "";
int n;


n = read(w->fd, buf, sizeof(buf));
if (n > 1) {
buf[n - 1] = 0;
Expand All @@ -56,7 +55,8 @@ static void uwsc_onopen(struct uwsc_client *cl)
printf("Please input:\n");
}

static void uwsc_onmessage(struct uwsc_client *cl, void *data, size_t len, bool binary)
static void uwsc_onmessage(struct uwsc_client *cl,
void *data, size_t len, bool binary)
{
printf("Recv:");

Expand Down Expand Up @@ -101,42 +101,42 @@ static void usage(const char *prog)
{
fprintf(stderr, "Usage: %s [option]\n"
" -u url # ws://localhost:8080/ws\n"
" -c file # Load CA certificates from file\n"
" -n # don't validate the server's certificate\n"
" wss://localhost:8080/ws\n"
" -P n # Ping interval\n"
, prog);
exit(1);
}

int main(int argc, char **argv)
{
int opt;
const char *url = "ws://localhost:8080/ws";
struct ev_loop *loop = EV_DEFAULT;
const char *url = "ws://localhost:8080/ws";
struct ev_signal signal_watcher;
int ping_interval = 10; /* second */
struct uwsc_client *cl;
const char *crt_file = NULL;
bool verify = true;
int opt;

while ((opt = getopt(argc, argv, "u:nc:v")) != -1) {
while ((opt = getopt(argc, argv, "u:P:")) != -1) {
switch (opt) {
case 'u':
url = optarg;
break;
case 'n':
verify = false;
break;
case 'c':
crt_file = optarg;
break;
case 'P':
ping_interval = atoi(optarg);
break;
default: /* '?' */
usage(argv[0]);
}
}

cl = uwsc_new_ssl_v2(url, crt_file, verify, loop);
uwsc_log_info("Libuwsc: %s\n", UWSC_VERSION_STRING);

cl = uwsc_new(loop, url, ping_interval);
if (!cl)
return -1;


uwsc_log_info("Start connect...\n");

cl->onopen = uwsc_onopen;
cl->onmessage = uwsc_onmessage;
cl->onerror = uwsc_onerror;
Expand All @@ -147,7 +147,6 @@ int main(int argc, char **argv)

ev_run(loop, 0);

cl->send(cl, NULL, 0, UWSC_OP_CLOSE);
free(cl);

return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ int uwsc_ssl_init(struct uwsc_ssl_ctx **ctx, int sock)
SSL_load_error_strings();

c->ctx = SSL_CTX_new(SSLv23_client_method());
SSL_CTX_set_verify(c->ctx, SSL_VERIFY_NONE, 0);
SSL_CTX_set_verify(c->ctx, SSL_VERIFY_NONE, NULL);

c->ssl = SSL_new(c->ctx);
SSL_set_fd(c->ssl, sock);
Expand Down
13 changes: 5 additions & 8 deletions src/uwsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,13 +498,7 @@ static void uwsc_timer_cb(struct ev_loop *loop, struct ev_timer *w, int revents)
cl->wait_pong = true;
}

static void uwsc_set_ping_interval(struct uwsc_client *cl, int interval)
{
cl->ping_interval = interval;
}

struct uwsc_client *uwsc_new_ssl_v2(const char *url, const char *ca_crt_file,
bool verify, struct ev_loop *loop)
struct uwsc_client *uwsc_new(struct ev_loop *loop, const char *url, int ping_interval)
{
struct uwsc_client *cl = NULL;
const char *path = "/";
Expand Down Expand Up @@ -538,11 +532,14 @@ struct uwsc_client *uwsc_new_ssl_v2(const char *url, const char *ca_crt_file,
if (!inprogress)
cl->state = CLIENT_STATE_HANDSHAKE;

if (!loop)
loop = EV_DEFAULT;

cl->loop = loop;
cl->sock = sock;
cl->send = uwsc_send;
cl->ping = uwsc_ping;
cl->set_ping_interval = uwsc_set_ping_interval;
cl->ping_interval = ping_interval;

if (ssl) {
#if (UWSC_SSL_SUPPORT)
Expand Down
25 changes: 7 additions & 18 deletions src/uwsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,12 @@ struct uwsc_client {
void (*ping)(struct uwsc_client *cl);
};

struct uwsc_client *uwsc_new_ssl_v2(const char *url, const char *ca_crt_file, bool verify,
struct ev_loop *loop);

static inline struct uwsc_client *uwsc_new_ssl(const char *url, const char *ca_crt_file,
bool verify)
{
return uwsc_new_ssl_v2(url, ca_crt_file, verify, EV_DEFAULT);
}

static inline struct uwsc_client *uwsc_new(const char *url)
{
return uwsc_new_ssl(url, NULL, false);
}

static inline struct uwsc_client *uwsc_new_v2(const char *url, struct ev_loop *loop)
{
return uwsc_new_ssl_v2(url, NULL, false, loop);
}
/*
* uwsc_new - creat an uwsc_client struct and connect to server
* @loop: If NULL will use EV_DEFAULT
* @url: A websock url. ws://xxx.com/xx or wss://xxx.com/xx
* @ping_interval: ping interval
*/
struct uwsc_client *uwsc_new(struct ev_loop *loop, const char *url, int ping_interval);

#endif

0 comments on commit f09eef4

Please sign in to comment.