59 lines
1.5 KiB
D
59 lines
1.5 KiB
D
module angel.conn;
|
|
|
|
// Internal imports
|
|
import angel.utils.logging;
|
|
import angel.utils.constants;
|
|
import angel.config : config;
|
|
|
|
// External imports
|
|
import std.socket;
|
|
import std.conv;
|
|
import std.format;
|
|
import std.stdio;
|
|
|
|
class Listener
|
|
{
|
|
private TcpSocket sock;
|
|
|
|
this()
|
|
{
|
|
this.initialize();
|
|
this.sockc();
|
|
}
|
|
|
|
private void initialize()
|
|
{
|
|
Logger.log(LogLevel.Debug, format("Establishing conn: %s:%s", config.server.host, config
|
|
.server.port));
|
|
try {
|
|
this.sock = new TcpSocket();
|
|
this.sock.connect(new InternetAddress(config.server.host, cast(ushort) config.server.port));
|
|
}
|
|
catch (SocketOSException err) {
|
|
Logger.log(LogLevel.Error, format("Failed to establ conn: %s", err));
|
|
this.sock = null;
|
|
}
|
|
catch (Exception err) {
|
|
Logger.log(LogLevel.Error, format("Unknown err occurred: %s", err));
|
|
this.sock = null;
|
|
}
|
|
}
|
|
|
|
private void sockc()
|
|
{
|
|
if (this.sock is null) {
|
|
Logger.log(LogLevel.Warning, "Cannot send data: No active connection.");
|
|
return;
|
|
}
|
|
|
|
ubyte[] init_id = [to!ubyte(config.server.initializer)];
|
|
try {
|
|
this.sock.send(init_id);
|
|
Logger.log(LogLevel.Event, "Initializer ID sent.");
|
|
}
|
|
catch (SocketOSException err) {
|
|
Logger.log(LogLevel.Error, format("Failed to send data: %s", err));
|
|
}
|
|
}
|
|
}
|