Added error handling for receiving case and also when the socket has been closed

This commit is contained in:
Tristan B. Kildaire 2020-04-27 22:19:14 +02:00
parent d2d140b354
commit 543a6168ed
4 changed files with 31 additions and 6 deletions

12
source/base/net.d Normal file
View File

@ -0,0 +1,12 @@
module base.net;
import base.types;
import std.socket : Socket;
public class NetworkException : BesterException
{
this(Socket socketResponsible)
{
super("Socket error with: " ~ socketResponsible.remoteAddress().toAddrString());
}
}

View File

@ -2,9 +2,9 @@ module base.types;
public class BesterException : Exception
{
this()
this(string message)
{
/* TODO: Implement me */
super("");
/* TODO: Implement me */
super(message);
}
}

View File

@ -12,6 +12,7 @@ import listeners.listener;
import server.server;
import handlers.response;
import connection.message;
import base.net;
public final class BesterConnection : Thread
{
@ -85,12 +86,19 @@ public final class BesterConnection : Thread
/**
* If the message was received successfully then
* process the message. */
processMessage(receivedMessage);
processMessage(receivedMessage);
/* TODO: Check socket status here, the client might have issued a command to close the connection */
if(!clientConnection.isAlive())
{
debugPrint("Socket is dead.");
break;
}
}
else
{
debugPrint("[ReadSendLoop] Error with receiving from socket, ending...");
break;
throw new NetworkException(clientConnection);
}
}
debugPrint("<<< End read/send loop >>>");
@ -115,6 +123,11 @@ public final class BesterConnection : Thread
bool receiveStatus = receiveMessage(handlerSocket, response);
/* TODO: Use `receiveStatus` */
if(!receiveStatus)
{
/* TODO: Add throw here */
throw new NetworkException(handlerSocket);
}
return response;
}

View File

@ -202,6 +202,6 @@ public final class ResponseError : BesterException
this(JSONValue messageResponse, ulong statusCode)
{
/* TODO: Set message afterwards again */
super();
super("");
}
}