mirror of
https://github.com/deavminet/dnetd
synced 2024-09-21 09:43:37 +02:00
Added function to get list of connections, added sendUserMessage function (WIP) to send a message to a given user
This commit is contained in:
parent
16b4ad8b40
commit
d4384c439e
@ -186,6 +186,10 @@ public class DConnection : Thread
|
|||||||
{
|
{
|
||||||
command = Command.PART;
|
command = Command.PART;
|
||||||
}
|
}
|
||||||
|
else if(commandByte == cast(ulong)5)
|
||||||
|
{
|
||||||
|
command = Command.MSG;
|
||||||
|
}
|
||||||
else if(commandByte == cast(ulong)6)
|
else if(commandByte == cast(ulong)6)
|
||||||
{
|
{
|
||||||
command = Command.LIST;
|
command = Command.LIST;
|
||||||
@ -372,7 +376,9 @@ public class DConnection : Thread
|
|||||||
/* If we are sending to a user */
|
/* If we are sending to a user */
|
||||||
if(messageType == cast(byte)0)
|
if(messageType == cast(byte)0)
|
||||||
{
|
{
|
||||||
/* TODO Implemet me */
|
/* Send the message to the user */
|
||||||
|
bool sendStatus = sendUserMessage(destination, msg);
|
||||||
|
reply = [sendStatus];
|
||||||
}
|
}
|
||||||
/* If we are sending to a channel */
|
/* If we are sending to a channel */
|
||||||
else if(messageType == cast(ubyte)1)
|
else if(messageType == cast(ubyte)1)
|
||||||
@ -438,11 +444,43 @@ public class DConnection : Thread
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send user a message
|
||||||
|
*
|
||||||
|
* Sends the provided user the specified message
|
||||||
|
*/
|
||||||
|
private bool sendUserMessage(string username, string message)
|
||||||
|
{
|
||||||
|
/* Find the user to send to */
|
||||||
|
DConnection user = server.findUser(username);
|
||||||
|
|
||||||
|
/* If the user was found */
|
||||||
|
if(user)
|
||||||
|
{
|
||||||
|
/* Send the messge */
|
||||||
|
/* TODO: Implement me */
|
||||||
|
|
||||||
|
|
||||||
|
/* TODO: Return value should be based off message send success */
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
/* If the user was not found */
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public string getUsername()
|
public string getUsername()
|
||||||
{
|
{
|
||||||
return username;
|
return username;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ConnectionType getConnectionType()
|
||||||
|
{
|
||||||
|
return connType;
|
||||||
|
}
|
||||||
|
|
||||||
public override string toString()
|
public override string toString()
|
||||||
{
|
{
|
||||||
string toStr = "["~to!(string)(connType)~"]: ";
|
string toStr = "["~to!(string)(connType)~"]: ";
|
||||||
|
@ -31,6 +31,7 @@ public class DServer : Thread
|
|||||||
* Connection queue
|
* Connection queue
|
||||||
*/
|
*/
|
||||||
private DConnection[] connectionQueue;
|
private DConnection[] connectionQueue;
|
||||||
|
private Mutex connectionLock;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Channels
|
* Channels
|
||||||
@ -89,7 +90,10 @@ public class DServer : Thread
|
|||||||
|
|
||||||
private void initLocks()
|
private void initLocks()
|
||||||
{
|
{
|
||||||
/* Initialioze the channel lock */
|
/* Initialize the connection lock */
|
||||||
|
connectionLock = new Mutex();
|
||||||
|
|
||||||
|
/* Initialize the channel lock */
|
||||||
channelLock = new Mutex();
|
channelLock = new Mutex();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,8 +116,14 @@ public class DServer : Thread
|
|||||||
/* Spawn a connection handler */
|
/* Spawn a connection handler */
|
||||||
DConnection connection = new DConnection(this, socket);
|
DConnection connection = new DConnection(this, socket);
|
||||||
|
|
||||||
|
/* Lock the connections list */
|
||||||
|
connectionLock.lock();
|
||||||
|
|
||||||
/* Add to the connection queue */
|
/* Add to the connection queue */
|
||||||
connectionQueue ~= connection;
|
connectionQueue ~= connection;
|
||||||
|
|
||||||
|
/* Unlock the connections list */
|
||||||
|
connectionLock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,6 +163,48 @@ public class DServer : Thread
|
|||||||
return channel;
|
return channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the DConnection with the matching
|
||||||
|
* username, null if not found
|
||||||
|
*/
|
||||||
|
public DConnection findUser(string username)
|
||||||
|
{
|
||||||
|
/* Get all the current connections */
|
||||||
|
DConnection[] connections = getConnections();
|
||||||
|
|
||||||
|
/* Find the user with the matching user name */
|
||||||
|
foreach(DConnection connection; connections)
|
||||||
|
{
|
||||||
|
/* The connection must be a user (not unspec or server) */
|
||||||
|
if(connection.getConnectionType() == DConnection.ConnectionType.CLIENT)
|
||||||
|
{
|
||||||
|
/* Match the username */
|
||||||
|
if(cmp(connection.getUsername(), username) == 0)
|
||||||
|
{
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DConnection[] getConnections()
|
||||||
|
{
|
||||||
|
/* The current connections list */
|
||||||
|
DConnection[] currentConnections;
|
||||||
|
|
||||||
|
/* Lock the connections list */
|
||||||
|
connectionLock.lock();
|
||||||
|
|
||||||
|
currentConnections = connectionQueue;
|
||||||
|
|
||||||
|
/* Unlock the connections list */
|
||||||
|
connectionLock.unlock();
|
||||||
|
|
||||||
|
return currentConnections;
|
||||||
|
}
|
||||||
|
|
||||||
public DChannel[] getChannels()
|
public DChannel[] getChannels()
|
||||||
{
|
{
|
||||||
/* The current channels list */
|
/* The current channels list */
|
||||||
|
Loading…
Reference in New Issue
Block a user