Refactor
This commit is contained in:
parent
730b2af803
commit
cd76e303c8
|
@ -1,37 +1,10 @@
|
|||
module ircd.app;
|
||||
|
||||
import std.stdio;
|
||||
import std.algorithm;
|
||||
import std.range;
|
||||
import core.time;
|
||||
|
||||
import vibe.core.core;
|
||||
|
||||
import ircd.message;
|
||||
import ircd.connection;
|
||||
import ircd.server;
|
||||
|
||||
shared static this()
|
||||
{
|
||||
Connection[] connections = [];
|
||||
|
||||
runTask(delegate()
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
foreach(connection; connections)
|
||||
{
|
||||
connection.send(Message(null, "PING", [connection.nick]));
|
||||
}
|
||||
sleep(10.seconds);
|
||||
}
|
||||
});
|
||||
|
||||
listenTCP(6667, delegate(TCPConnection connection)
|
||||
{
|
||||
auto c = new Connection(connection);
|
||||
connections ~= c;
|
||||
c.handle();
|
||||
connections = connections.filter!(a => a != c).array;
|
||||
},"127.0.0.1");
|
||||
auto server = new Server();
|
||||
server.listen();
|
||||
}
|
||||
|
||||
|
|
|
@ -6,13 +6,13 @@ import std.string;
|
|||
import vibe.core.core;
|
||||
import vibe.stream.operations;
|
||||
|
||||
import ircd.packageVersion;
|
||||
|
||||
import ircd.message;
|
||||
import ircd.server;
|
||||
|
||||
class Connection
|
||||
{
|
||||
private TCPConnection _connection;
|
||||
private Server _server;
|
||||
|
||||
//TODO: Make into auto-properties (via template)
|
||||
string nick;
|
||||
|
@ -21,9 +21,10 @@ class Connection
|
|||
|
||||
bool connected;
|
||||
|
||||
this(TCPConnection connection)
|
||||
this(TCPConnection connection, Server server)
|
||||
{
|
||||
_connection = connection;
|
||||
_server = server;
|
||||
}
|
||||
|
||||
void send(Message message)
|
||||
|
@ -56,9 +57,9 @@ class Connection
|
|||
writeln("unused: " ~ message.parameters[2]);
|
||||
|
||||
send(Message("localhost", "001", [nick, "Welcome to the Internet Relay Network " ~ nick ~ "!" ~ user ~ "@hostname"], true));
|
||||
send(Message("localhost", "002", [nick, "Your host is localhost, running version salty-ircd-" ~ packageVersion], true));
|
||||
send(Message("localhost", "003", [nick, "This server was created " ~ packageTimestampISO], true));
|
||||
send(Message("localhost", "004", [nick, "localhost", "salty-ircd-" ~ packageVersion, "w", "snt"]));
|
||||
send(Message("localhost", "002", [nick, "Your host is " ~ _server.name ~ ", running version " ~ _server.versionString], true));
|
||||
send(Message("localhost", "003", [nick, "This server was created " ~ _server.creationDate], true));
|
||||
send(Message("localhost", "004", [nick, _server.name, _server.versionString, "w", "snt"]));
|
||||
break;
|
||||
case "PING":
|
||||
send(Message(null, "PONG", [message.parameters[0]], true));
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
module ircd.server;
|
||||
|
||||
import std.stdio;
|
||||
import std.algorithm;
|
||||
import std.range;
|
||||
import std.conv;
|
||||
import std.socket;
|
||||
import core.time;
|
||||
|
||||
import vibe.core.core;
|
||||
|
||||
import ircd.packageVersion;
|
||||
|
||||
import ircd.message;
|
||||
import ircd.connection;
|
||||
|
||||
class Server
|
||||
{
|
||||
Connection[] connections;
|
||||
|
||||
enum creationDate = packageTimestampISO.until('T').text; //TODO: Also show time when RFC-strictness is off
|
||||
enum versionString = "salty-ircd-" ~ packageVersion;
|
||||
|
||||
string name;
|
||||
|
||||
this()
|
||||
{
|
||||
name = Socket.hostName;
|
||||
|
||||
runTask(&pingLoop);
|
||||
}
|
||||
|
||||
private void pingLoop()
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
foreach(connection; connections)
|
||||
{
|
||||
connection.send(Message(null, "PING", [connection.nick]));
|
||||
}
|
||||
sleep(10.seconds);
|
||||
}
|
||||
}
|
||||
|
||||
private void acceptConnection(TCPConnection tcpConnection)
|
||||
{
|
||||
auto connection = new Connection(tcpConnection, this);
|
||||
connections ~= connection;
|
||||
connection.handle();
|
||||
connections = connections.filter!(c => c != connection).array;
|
||||
}
|
||||
|
||||
void listen(ushort port = 6667)
|
||||
{
|
||||
listenTCP(port, &acceptConnection);
|
||||
}
|
||||
|
||||
void listen(ushort port, string address)
|
||||
{
|
||||
listenTCP(port, &acceptConnection, address);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue