1
0
mirror of https://github.com/deavminet/dnetd synced 2024-09-21 01:33:24 +02:00

Leave all channels you were a member of on socket read error (caused by disconnect)

This commit is contained in:
Tristan B. Kildaire 2020-09-28 11:42:14 +02:00
parent e12688c38a
commit 5983f090e3
3 changed files with 71 additions and 7 deletions

View File

@ -102,6 +102,30 @@ public class DChannel
return memberCount;
}
public bool isMember(DConnection client)
{
/* Whether or not you are a member */
bool isMem;
/* Lock the members list */
memberLock.lock();
/* CHeck if you are in this channel */
foreach(DConnection member; members)
{
if(member is client)
{
isMem = true;
break;
}
}
/* Unlock the members list */
memberLock.unlock();
return isMem;
}
/**
* Removes the given client from this channel
*/
@ -125,6 +149,8 @@ public class DChannel
/* Set it as the new list */
members = newMembers;
/* TODO: Send left message here */
/* Unlock the members list */
memberLock.unlock();
}
@ -132,9 +158,12 @@ public class DChannel
public bool sendMessage(DConnection sender, string message)
{
bool status;
/* TODO: Generate message */
/* TODO: Spec out in protocol */
/* TODO: Reserved tag 0 for notifications */
/* The protocol data to send */
byte[] msg;
/* Set the notificaiton type */
msg ~= [0];
/**
* Format
@ -143,7 +172,7 @@ public class DChannel
* byte length of name of channel/person (dm case)
* message-bytes
*/
byte[] msg = [cast(byte)1,(cast(byte)sender.getUsername().length)]~cast(byte[])sender.getUsername()~cast(byte[])message;
msg ~= [cast(byte)1,(cast(byte)sender.getUsername().length)]~cast(byte[])sender.getUsername()~cast(byte[])message;
/* Send the message to everyone else in the channel */
foreach(DConnection member; members)
@ -152,9 +181,9 @@ public class DChannel
if(!(member is sender))
{
/* Send the message */
writeln("Delivering message for channel '"~name~"' to user '"~member.getUsername()~"'...");
writeln("Delivering message '"~message~"' for channel '"~name~"' to user '"~member.getUsername()~"'...");
status = member.writeSocket(0, msg);
writeln("Delivered message for channel '"~name~"' to user '"~member.getUsername()~"'!");
writeln("Delivered message '"~message~"' for channel '"~name~"' to user '"~member.getUsername()~"'!");
/* TODO: Errors from status */
}

View File

@ -125,8 +125,36 @@ public class DConnection : Thread
else
{
/* TODO: Error handling */
writeln("Error with receive: "~to!(string)(this));
break;
}
}
/* Clean up */
cleanUp();
}
private void cleanUp()
{
writeln(to!(string)(this)~" Cleaning up connection...");
/* Remove this user from all channels he is in */
DChannel[] channels = server.getChannels();
/* Loop through each channel */
foreach(DChannel currentChannel; channels)
{
/* Check if you are a member of it */
if(currentChannel.isMember(this))
{
/* Leave the channel */
currentChannel.leave(this);
writeln(to!(string)(this)~" Leaving '"~currentChannel.getName()~"'...");
}
}
/* Remove this user from the connection queue */
/* TODO: Implement me */
}
/* TODO: add mutex for writing with message and funciton for doing so */
@ -482,7 +510,12 @@ public class DConnection : Thread
public override string toString()
{
string toStr = "["~to!(string)(connType)~"]: ";
string toStr = "["~to!(string)(connType)~" (";
toStr ~= socket.remoteAddress.toString();
toStr ~= ")]: ";
if(connType == ConnectionType.CLIENT)
{

View File

@ -17,6 +17,7 @@ import dnetd.dchannel;
import std.string : cmp;
import core.sync.mutex : Mutex;
import std.stdio;
import std.conv : to;
public class DServer : Thread
{
@ -121,6 +122,7 @@ public class DServer : Thread
/* Add to the connection queue */
connectionQueue ~= connection;
writeln("Added new connection to queue "~to!(string)(connection));
/* Unlock the connections list */
connectionLock.unlock();