diff options
Diffstat (limited to 'src/network.cc')
-rw-r--r-- | src/network.cc | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/src/network.cc b/src/network.cc index 8e99855..3c7fd7e 100644 --- a/src/network.cc +++ b/src/network.cc @@ -31,8 +31,9 @@ #include <string.h> #include <sys/socket.h> -Network::Network(Socket *gs) +Network::Network(Socket *gs, Error* err) { + error = err; s = gs; } @@ -43,14 +44,18 @@ Network::~Network() int Network::write(void *buf, int size) { if(!s->isConnected()) { - fprintf(stderr, "Write attempted to a socket not connected!\n"); + char errbuf[] = "Write attempted to a socket not connected!\0"; + if(error) error->pushError(errbuf); + else fprintf(stderr, errbuf); return 0; } int n = send(s->ssocket, buf, size, MSG_WAITALL); - if(n == -1) printf("An error occurred!\n"); - - // printf("Sent: %s\n", buf); + if(n == -1) { + char errbuf[] = "An error occurred!\0"; + if(error) error->pushError(errbuf); + else fprintf(stderr, errbuf); + } return n; } @@ -58,14 +63,18 @@ int Network::write(void *buf, int size) int Network::read(void *buf, int size) { if(!s->isConnected()) { - fprintf(stderr, "Read attempted from a socket not connected!\n"); + char errbuf[] = "Read attempted from a socket not connected!\0"; + if(error) error->pushError(errbuf); + else fprintf(stderr, errbuf); return 0; } int n = recv(s->ssocket, buf, size, MSG_WAITALL); - if(n == -1) printf("An error occurred!\n"); - - // printf("Recieved: %s\n", buf); + if(n == -1) { + char errbuf[] = "An error occurred!\0"; + if(error) error->pushError(errbuf); + else fprintf(stderr, errbuf); + } return n; } @@ -100,7 +109,9 @@ int Network::sendPackage(n_header *h, void* buf, int bufsz) int n = sendmsg(s->ssocket, &msg, 0); if(n < 0) { - perror("sendmsg"); + char errbuf[] = "A network error ocurred during sendPackage!\0"; + if(error) error->pushError(errbuf); + else perror(errbuf); return -1; } @@ -126,12 +137,16 @@ int Network::recvPackage(n_header *h, void* buf, int bufsz) int n = recvmsg(s->ssocket, &msg, MSG_WAITALL); if(n < 0) { - perror("recvmsg"); + char errbuf[] = "A network error ocurred during recvPackage!\0"; + if(error) error->pushError(errbuf); + else perror(errbuf); return -1; } if(msg.msg_iovlen != 2) { - fprintf(stderr, "Wrong package format!\n"); + char errbuf[] = "Wrong package format!\0"; + if(error) error->pushError(errbuf); + else perror(errbuf); return -1; } return n; |