Use screen_error() for all fatal errors
authorMatthew Mondor <mmondor@pulsar-zone.net>
Sat, 5 Jul 2014 18:15:18 +0000 (18:15 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Sat, 5 Jul 2014 18:15:18 +0000 (18:15 +0000)
mmsoftware/mystic_ships/client/src/packets.c
mmsoftware/mystic_ships/client/src/recvq.c
mmsoftware/mystic_ships/client/src/thread_net_recv.c
mmsoftware/mystic_ships/client/src/thread_net_send.c

index 560a53a..6e9be11 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: packets.c,v 1.5 2014/06/25 15:14:33 mmondor Exp $ */
+/* $Id: packets.c,v 1.6 2014/07/05 18:15:18 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -23,6 +23,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <conf.h>
+#include <screen.h>
 #include <packets_common.h>
 #include <packets.h>
 #include <tms-endian.h>
@@ -31,7 +33,6 @@
 #include <debug.h>
 #include <hmac.h>
 #include <enc.h>
-#include <conf.h>
 
 
 
@@ -79,10 +80,9 @@ spackets_handle(thread_amsg_t *amsg)
        uint8_t *data;
        size_t  size, off, len;
 
-       if (amsg->size == -1) {
-               (void) fprintf(stderr, "Error reading from server socket\n");
-               exit(EXIT_FAILURE);
-       }
+       if (amsg->size == -1)
+               screen_error(
+                   "spackets_handle() - Error reading from server socket");
 
        data = (uint8_t *)amsg->data;
        size = amsg->size;
@@ -105,11 +105,9 @@ spackets_handle(thread_amsg_t *amsg)
 
                /* Kill client on packet processing error */
                ASSERT(spacket_index[id].handler != NULL);
-               if (spacket_index[id].handler((uint16_t *)&data[off]) == -1) {
-                       (void) fprintf(stderr,
-                           "Error processing server packet 0x%02x\n", id);
-                       exit(EXIT_FAILURE);
-               }
+               if (spacket_index[id].handler((uint16_t *)&data[off]) == -1)
+                       screen_error("spackets_handle() - "
+                           "Error processing server packet 0x%02x", id);
        }
 }
 
@@ -118,11 +116,8 @@ spacket_auth_handler(uint16_t *ptr)
 {
        struct spacket_auth     *p = (struct spacket_auth *)ptr;
 
-       if (p->protocol_version != CLIENT_VERSION) {
-               (void) fprintf(stderr,
-                   "Client and server versions do not match\n");
-               exit(EXIT_FAILURE);
-       }
+       if (p->protocol_version != CLIENT_VERSION)
+               screen_error("Client and server versions do not match");
 
        return cpacket_auth_send(CLIENT_LOGIN, CLIENT_PASSWD,
            p->noncerand1, p->noncerand2);
@@ -233,11 +228,8 @@ cpacket_send(const void *buf, size_t size)
 {
        thread_amsg_t   *amsg;
 
-       if ((amsg = thread_amsg_create()) == NULL) {
-               (void) fprintf(stderr,
-                   "cpacket_send() - thread_amsg_create()\n");
-               exit(EXIT_FAILURE);
-       }
+       if ((amsg = thread_amsg_create()) == NULL)
+               screen_error("cpacket_send() - thread_amsg_create()");
 
        (void) memcpy(amsg->data, buf, size);
        amsg->size = size;
@@ -253,11 +245,8 @@ cpacket_send_callback(const void *buf, size_t size,
 {
        thread_amsg_t   *amsg;
 
-       if ((amsg = thread_amsg_create()) == NULL) {
-               (void) fprintf(stderr,
-                   "cpacket_send() - thread_amsg_create()\n");
-               exit(EXIT_FAILURE);
-       }
+       if ((amsg = thread_amsg_create()) == NULL)
+               screen_error("cpacket_send() - thread_amsg_create()");
 
        thread_amsg_setcallback(amsg, callback, callback_arg);
        (void) memcpy(amsg->data, buf, size);
index bb15078..9f17420 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: recvq.c,v 1.2 2014/06/19 18:22:03 mmondor Exp $ */
+/* $Id: recvq.c,v 1.3 2014/07/05 18:15:18 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -14,6 +14,7 @@
 #include <SDL_net.h>
 
 #include <conf.h>
+#include <screen.h>
 
 #ifdef USE_COMPRESSION
 #include <zlib.h>
@@ -112,7 +113,7 @@ recvq_read(recvq_t *q)
        if ((s = SDLNet_TCP_Recv(q->sock, rbuffer, RBUFFER_SIZE)) == -1 ||
            s == 0) {
                (void) fprintf(stderr,
-                   "SDLNet_TCP_Recv() == %d - %d - %s\n",
+                   "recvq_read() - SDLNet_TCP_Recv() == %d - %d - %s\n",
                    s, errno, SDLNet_GetError());
                return -1;
        }
@@ -131,8 +132,7 @@ recvq_read(recvq_t *q)
        q->zin.avail_out = IBUFFER_SIZE;
        if ((ret = inflate(&q->zin, Z_SYNC_FLUSH)) != Z_OK &&
            ret != Z_STREAM_END) {
-               (void) fprintf(stderr,
-                   "inflate() error\n");
+               (void) fprintf(stderr, "recvq_read() - inflate() error");
                return -1;
        }
        buf = (int8_t *)ibuffer;
index 14346ee..4070280 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: thread_net_recv.c,v 1.2 2014/06/25 15:33:48 mmondor Exp $ */
+/* $Id: thread_net_recv.c,v 1.3 2014/07/05 18:15:18 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
 #include <stdlib.h>
 #include <string.h>
 
+#include <conf.h>
 #include <main.h>
 #include <thread_net_recv.h>
 #include <recvq.h>
 #include <screen.h>
-#include <conf.h>
 
 
 
@@ -45,18 +45,14 @@ thread_net_recv(void *data)
 {
 
        /* Initialize reply port and ring */
-       if (thread_ring_init(&recv_ring) == -1) {
-               (void) fprintf(stderr,
-                   "thread_net_recv() - thread_ring_init(recv_port) - %s\n",
+       if (thread_ring_init(&recv_ring) == -1)
+               screen_error(
+                   "thread_net_recv() - thread_ring_init(recv_port) - %s",
                    SDL_GetError());
-               exit(EXIT_FAILURE);
-       }
-       if (thread_port_init(&recv_port) == -1) {
-               (void) fprintf(stderr,
-                   "thread_net_recv() - thread_port_init(recv_port) - %s\n",
+       if (thread_port_init(&recv_port) == -1)
+               screen_error(
+                   "thread_net_recv() - thread_port_init(recv_port) - %s",
                    SDL_GetError());
-               exit(EXIT_FAILURE);
-       }
        thread_port_set_ring(&recv_port, &recv_ring);
 
        if (state_connect() == 0)
@@ -147,18 +143,14 @@ state_recv(void)
                 * it's fatal since the receiving main thread will never be
                 * notified.
                 */
-               if ((amsg = thread_amsg_create()) == NULL) {
-                       (void) fprintf(stderr,
-                           "state_recv() - thread_amsg_create()\n");
-                       exit(EXIT_FAILURE);
-               }
+               if ((amsg = thread_amsg_create()) == NULL)
+                       screen_error("state_recv() - thread_amsg_create()");
 
                /*
                 * Call recvq_read() until we either have 1, in which case
                 * we'll send an async message, or -1 for error.
                 */
                while ((res = recvq_read(&recvq)) == 0) ;
-
                if (res == -1)
                        screen_error("Lost server connection");
 
@@ -167,11 +159,8 @@ state_recv(void)
                 * it asynchroneously.
                 */
                recvq_content(&recvq, &buf, &size);
-               if (size > AMSG_MAXSIZE) {
-                       (void) fprintf(stderr,
-                           "state_recv() - increase AMSG_MAXSIZE!\n");
-                       exit(EXIT_FAILURE);
-               }
+               if (size > AMSG_MAXSIZE)
+                       screen_error("state_recv() - increase AMSG_MAXSIZE!");
                (void) memcpy(amsg->data, buf, size);
                amsg->size = size;
                recvq_content_reset(&recvq);
index 0d3a73f..e192a7e 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: thread_net_send.c,v 1.1 2006/12/31 08:32:39 mmondor Exp $ */
+/* $Id: thread_net_send.c,v 1.2 2014/07/05 18:15:18 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -16,6 +16,7 @@
 #include <stdlib.h>
 
 #include <conf.h>
+#include <screen.h>
 
 #ifdef USE_COMPRESSION
 #include <zlib.h>
@@ -57,25 +58,19 @@ thread_net_send(void *data)
        zout.zalloc = NULL;
        zout.zfree = NULL;
        zout.opaque = NULL;
-       if (deflateInit(&zout, Z_DEFAULT_COMPRESSION) != Z_OK) {
-               (void) fprintf(stderr, "thread_net_recv() - deflateInit()\n");
-               exit(EXIT_FAILURE);
-       }
+       if (deflateInit(&zout, Z_DEFAULT_COMPRESSION) != Z_OK)
+               screen_error("thread_net_recv() - deflateInit()");
 #endif
 
        /* Initialize reply port and ring */
-       if (thread_ring_init(&send_ring) == -1) {
-               (void) fprintf(stderr,
-                   "thread_net_recv() - thread_ring_init(send_port) - %s\n",
+       if (thread_ring_init(&send_ring) == -1)
+               screen_error(
+                   "thread_net_recv() - thread_ring_init(send_port) - %s",
                    SDL_GetError());
-               exit(EXIT_FAILURE);
-       }
-       if (thread_port_init(&send_port) == -1) {
-               (void) fprintf(stderr,
-                   "thread_net_send() - thread_port_init(send_port) - %s\n",
+       if (thread_port_init(&send_port) == -1)
+               screen_error(
+                   "thread_net_send() - thread_port_init(send_port) - %s",
                    SDL_GetError());
-               exit(EXIT_FAILURE);
-       }
        thread_port_set_ring(&send_port, &send_ring);
 
        /*
@@ -95,11 +90,8 @@ thread_net_send(void *data)
                        zout.avail_in = amsg->size;
                        zout.next_out = (Bytef *)dbuffer;
                        zout.avail_out = DBUFFER_SIZE;
-                       if (deflate(&zout, Z_SYNC_FLUSH) != Z_OK) {
-                               (void) fprintf(stderr,
-                                   "thread_net_send() - deflate()\n");
-                               exit(EXIT_FAILURE);
-                       }
+                       if (deflate(&zout, Z_SYNC_FLUSH) != Z_OK)
+                               screen_error("thread_net_send() - deflate()");
                        buf = (uint8_t *)dbuffer;
                        s = DBUFFER_SIZE - zout.avail_out;
 #else
@@ -114,8 +106,9 @@ thread_net_send(void *data)
 #endif
                                if (SDLNet_TCP_Send(server_socket, buf, s)
                                    != s)
-                                       (void) fprintf(stderr,
-                                          "Error writing to server socket\n");
+                                       screen_error("thread_net_send() - "
+                                           "SDLNet_TCP_Send() - %s",
+                                           SDL_GetError());
                        }
                        thread_amsg_callback(amsg);
                        thread_amsg_destroy(amsg);