Fixed spacing in event.c

This commit is contained in:
Dmitry Podgorny
2016-08-30 23:34:24 +03:00
parent 75b19be572
commit 3869906357

View File

@@ -92,85 +92,85 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout)
/* send queued data */ /* send queued data */
connitem = ctx->connlist; connitem = ctx->connlist;
while (connitem) { while (connitem) {
conn = connitem->conn; conn = connitem->conn;
if (conn->state != XMPP_STATE_CONNECTED) { if (conn->state != XMPP_STATE_CONNECTED) {
connitem = connitem->next; connitem = connitem->next;
continue; continue;
} }
/* if we're running tls, there may be some remaining data waiting to /* if we're running tls, there may be some remaining data waiting to
* be sent, so push that out */ * be sent, so push that out */
if (conn->tls) { if (conn->tls) {
ret = tls_clear_pending_write(conn->tls); ret = tls_clear_pending_write(conn->tls);
if (ret < 0 && !tls_is_recoverable(tls_error(conn->tls))) { if (ret < 0 && !tls_is_recoverable(tls_error(conn->tls))) {
/* an error occured */ /* an error occured */
xmpp_debug(ctx, "xmpp", "Send error occured, disconnecting."); xmpp_debug(ctx, "xmpp", "Send error occured, disconnecting.");
conn->error = ECONNABORTED; conn->error = ECONNABORTED;
conn_disconnect(conn); conn_disconnect(conn);
} }
} }
/* write all data from the send queue to the socket */ /* write all data from the send queue to the socket */
sq = conn->send_queue_head; sq = conn->send_queue_head;
while (sq) { while (sq) {
towrite = sq->len - sq->written; towrite = sq->len - sq->written;
if (conn->tls) { if (conn->tls) {
ret = tls_write(conn->tls, &sq->data[sq->written], towrite); ret = tls_write(conn->tls, &sq->data[sq->written], towrite);
if (ret < 0 && !tls_is_recoverable(tls_error(conn->tls))) { if (ret < 0 && !tls_is_recoverable(tls_error(conn->tls))) {
/* an error occured */ /* an error occured */
conn->error = tls_error(conn->tls); conn->error = tls_error(conn->tls);
break; break;
} else if (ret < towrite) { } else if (ret < towrite) {
/* not all data could be sent now */ /* not all data could be sent now */
if (ret >= 0) sq->written += ret; if (ret >= 0) sq->written += ret;
break; break;
} }
} else { } else {
ret = sock_write(conn->sock, &sq->data[sq->written], towrite); ret = sock_write(conn->sock, &sq->data[sq->written], towrite);
if (ret < 0 && !sock_is_recoverable(sock_error())) { if (ret < 0 && !sock_is_recoverable(sock_error())) {
/* an error occured */ /* an error occured */
conn->error = sock_error(); conn->error = sock_error();
break; break;
} else if (ret < towrite) { } else if (ret < towrite) {
/* not all data could be sent now */ /* not all data could be sent now */
if (ret >= 0) sq->written += ret; if (ret >= 0) sq->written += ret;
break; break;
} }
} }
/* all data for this queue item written, delete and move on */ /* all data for this queue item written, delete and move on */
xmpp_free(ctx, sq->data); xmpp_free(ctx, sq->data);
tsq = sq; tsq = sq;
sq = sq->next; sq = sq->next;
xmpp_free(ctx, tsq); xmpp_free(ctx, tsq);
/* pop the top item */ /* pop the top item */
conn->send_queue_head = sq; conn->send_queue_head = sq;
/* if we've sent everything update the tail */ /* if we've sent everything update the tail */
if (!sq) conn->send_queue_tail = NULL; if (!sq) conn->send_queue_tail = NULL;
} }
/* tear down connection on error */ /* tear down connection on error */
if (conn->error) { if (conn->error) {
/* FIXME: need to tear down send queues and random other things /* FIXME: need to tear down send queues and random other things
* maybe this should be abstracted */ * maybe this should be abstracted */
xmpp_debug(ctx, "xmpp", "Send error occured, disconnecting."); xmpp_debug(ctx, "xmpp", "Send error occured, disconnecting.");
conn->error = ECONNABORTED; conn->error = ECONNABORTED;
conn_disconnect(conn); conn_disconnect(conn);
} }
connitem = connitem->next; connitem = connitem->next;
} }
/* reset parsers if needed */ /* reset parsers if needed */
for (connitem = ctx->connlist; connitem; connitem = connitem->next) { for (connitem = ctx->connlist; connitem; connitem = connitem->next) {
if (connitem->conn->reset_parser) if (connitem->conn->reset_parser)
conn_parser_reset(connitem->conn); conn_parser_reset(connitem->conn);
} }
@@ -189,41 +189,41 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout)
/* find events to watch */ /* find events to watch */
connitem = ctx->connlist; connitem = ctx->connlist;
while (connitem) { while (connitem) {
conn = connitem->conn; conn = connitem->conn;
switch (conn->state) { switch (conn->state) {
case XMPP_STATE_CONNECTING: case XMPP_STATE_CONNECTING:
/* connect has been called and we're waiting for it to complete */ /* connect has been called and we're waiting for it to complete */
/* connection will give us write or error events */ /* connection will give us write or error events */
/* make sure the timeout hasn't expired */ /* make sure the timeout hasn't expired */
if (time_elapsed(conn->timeout_stamp, time_stamp()) <= if (time_elapsed(conn->timeout_stamp, time_stamp()) <=
conn->connect_timeout) conn->connect_timeout)
FD_SET(conn->sock, &wfds); FD_SET(conn->sock, &wfds);
else { else {
conn->error = ETIMEDOUT; conn->error = ETIMEDOUT;
xmpp_info(ctx, "xmpp", "Connection attempt timed out."); xmpp_info(ctx, "xmpp", "Connection attempt timed out.");
conn_disconnect(conn); conn_disconnect(conn);
} }
break; break;
case XMPP_STATE_CONNECTED: case XMPP_STATE_CONNECTED:
FD_SET(conn->sock, &rfds); FD_SET(conn->sock, &rfds);
break; break;
case XMPP_STATE_DISCONNECTED: case XMPP_STATE_DISCONNECTED:
/* do nothing */ /* do nothing */
default: default:
break; break;
} }
/* Check if there is something in the SSL buffer. */ /* Check if there is something in the SSL buffer. */
if (conn->tls) { if (conn->tls) {
tls_read_bytes += tls_pending(conn->tls); tls_read_bytes += tls_pending(conn->tls);
} }
if (conn->state != XMPP_STATE_DISCONNECTED && conn->sock > max) if (conn->state != XMPP_STATE_DISCONNECTED && conn->sock > max)
max = conn->sock; max = conn->sock;
connitem = connitem->next; connitem = connitem->next;
} }
/* check for events */ /* check for events */
@@ -237,10 +237,10 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout)
/* select errored */ /* select errored */
if (ret < 0) { if (ret < 0) {
if (!sock_is_recoverable(sock_error())) if (!sock_is_recoverable(sock_error()))
xmpp_error(ctx, "xmpp", "event watcher internal error %d", xmpp_error(ctx, "xmpp", "event watcher internal error %d",
sock_error()); sock_error());
return; return;
} }
/* no events happened */ /* no events happened */
@@ -249,24 +249,24 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout)
/* process events */ /* process events */
connitem = ctx->connlist; connitem = ctx->connlist;
while (connitem) { while (connitem) {
conn = connitem->conn; conn = connitem->conn;
switch (conn->state) { switch (conn->state) {
case XMPP_STATE_CONNECTING: case XMPP_STATE_CONNECTING:
if (FD_ISSET(conn->sock, &wfds)) { if (FD_ISSET(conn->sock, &wfds)) {
/* connection complete */ /* connection complete */
/* check for error */ /* check for error */
ret = sock_connect_error(conn->sock); ret = sock_connect_error(conn->sock);
if (ret != 0) { if (ret != 0) {
/* connection failed */ /* connection failed */
xmpp_debug(ctx, "xmpp", "connection failed, error %d", ret); xmpp_debug(ctx, "xmpp", "connection failed, error %d", ret);
conn_disconnect(conn); conn_disconnect(conn);
break; break;
} }
conn->state = XMPP_STATE_CONNECTED; conn->state = XMPP_STATE_CONNECTED;
xmpp_debug(ctx, "xmpp", "connection successful"); xmpp_debug(ctx, "xmpp", "connection successful");
if (conn->tls_legacy_ssl) { if (conn->tls_legacy_ssl) {
xmpp_debug(ctx, "xmpp", "using legacy SSL connection"); xmpp_debug(ctx, "xmpp", "using legacy SSL connection");
@@ -277,52 +277,52 @@ void xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout)
} }
} }
/* send stream init */ /* send stream init */
conn_open_stream(conn); conn_open_stream(conn);
} }
break; break;
case XMPP_STATE_CONNECTED: case XMPP_STATE_CONNECTED:
if (FD_ISSET(conn->sock, &rfds) || (conn->tls && tls_pending(conn->tls))) { if (FD_ISSET(conn->sock, &rfds) || (conn->tls && tls_pending(conn->tls))) {
if (conn->tls) { if (conn->tls) {
ret = tls_read(conn->tls, buf, 4096); ret = tls_read(conn->tls, buf, 4096);
} else { } else {
ret = sock_read(conn->sock, buf, 4096); ret = sock_read(conn->sock, buf, 4096);
} }
if (ret > 0) { if (ret > 0) {
ret = parser_feed(conn->parser, buf, ret); ret = parser_feed(conn->parser, buf, ret);
if (!ret) { if (!ret) {
/* parse error, we need to shut down */ /* parse error, we need to shut down */
/* FIXME */ /* FIXME */
xmpp_debug(ctx, "xmpp", "parse error, disconnecting"); xmpp_debug(ctx, "xmpp", "parse error, disconnecting");
conn_disconnect(conn); conn_disconnect(conn);
} }
} else { } else {
if (conn->tls) { if (conn->tls) {
if (!tls_is_recoverable(tls_error(conn->tls))) if (!tls_is_recoverable(tls_error(conn->tls)))
{ {
xmpp_debug(ctx, "xmpp", "Unrecoverable TLS error, %d.", tls_error(conn->tls)); xmpp_debug(ctx, "xmpp", "Unrecoverable TLS error, %d.", tls_error(conn->tls));
conn->error = tls_error(conn->tls); conn->error = tls_error(conn->tls);
conn_disconnect(conn); conn_disconnect(conn);
} }
} else { } else {
/* return of 0 means socket closed by server */ /* return of 0 means socket closed by server */
xmpp_debug(ctx, "xmpp", "Socket closed by remote host."); xmpp_debug(ctx, "xmpp", "Socket closed by remote host.");
conn->error = ECONNRESET; conn->error = ECONNRESET;
conn_disconnect(conn); conn_disconnect(conn);
} }
} }
} }
break; break;
case XMPP_STATE_DISCONNECTED: case XMPP_STATE_DISCONNECTED:
/* do nothing */ /* do nothing */
default: default:
break; break;
} }
connitem = connitem->next; connitem = connitem->next;
} }
/* fire any ready handlers */ /* fire any ready handlers */
@@ -343,7 +343,7 @@ void xmpp_run(xmpp_ctx_t *ctx)
ctx->loop_status = XMPP_LOOP_RUNNING; ctx->loop_status = XMPP_LOOP_RUNNING;
while (ctx->loop_status == XMPP_LOOP_RUNNING) { while (ctx->loop_status == XMPP_LOOP_RUNNING) {
xmpp_run_once(ctx, DEFAULT_TIMEOUT); xmpp_run_once(ctx, DEFAULT_TIMEOUT);
} }
/* make it possible to start event loop again */ /* make it possible to start event loop again */
@@ -365,5 +365,5 @@ void xmpp_stop(xmpp_ctx_t *ctx)
xmpp_debug(ctx, "event", "Stopping event loop."); xmpp_debug(ctx, "event", "Stopping event loop.");
if (ctx->loop_status == XMPP_LOOP_RUNNING) if (ctx->loop_status == XMPP_LOOP_RUNNING)
ctx->loop_status = XMPP_LOOP_QUIT; ctx->loop_status = XMPP_LOOP_QUIT;
} }