Added initial code

This commit is contained in:
Tristan 🅱. Kildaire 2020-09-25 01:08:51 +02:00
parent 0f1d7df00d
commit 2684a68e6f
4 changed files with 109 additions and 15 deletions

View File

@ -4,7 +4,7 @@
],
"copyright": "Copyright © 2020, Tristan B. Kildaire",
"dependencies": {
"tristanable": "~>0.0.28"
"tristanable": "~>0.0.29"
},
"description": "dnet client",
"license": "GPLv3",

View File

@ -2,6 +2,6 @@
"fileVersion": 1,
"versions": {
"bformat": "1.0.8",
"tristanable": "0.0.28"
"tristanable": "0.0.29"
}
}

View File

@ -2,20 +2,66 @@ import std.stdio;
import tristanable.manager;
import std.socket;
import client;
import std.string : cmp, split, strip;
import std.conv : to;
void main()
{
writeln("Edit source/app.d to start your project.");
/* COnnect to the server */
Socket socket = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.TCP);
socket.connect(parseAddress("0.0.0.0",7777));
/* Create a new tristanable manager */
Manager manager = new Manager(socket);
manager.sendMessage(1, [0,4,65,66,66,65,69,69]);
writeln(manager.receiveMessage(1));
commandLine();
}
void commandLine()
{
/* Current conneciton */
DClient client;
/* The current command */
string commandLine;
while(true)
{
/* Read in a command line */
write("> ");
commandLine = readln();
if(cmp(strip(commandLine), "") == 0)
{
continue;
}
string[] elements = split(commandLine);
string command = elements[0];
/* If the command is `exit` */
if(cmp(command, "exit") == 0)
{
break;
}
/* If the command is `connect` */
else if(cmp(command, "connect") == 0)
{
string address = elements[1];
string port = elements[2];
Address addr = parseAddress(address, to!(ushort)(port));
writeln("Connecting to "~to!(string)(addr)~"...");
client = new DClient(addr);
writeln("Connected!");
}
/* If the command is `auth` */
else if(cmp(command, "auth") == 0)
{
string username = elements[1];
string password = elements[2];
client.auth(username, password);
}
}
if(client)
{
}
}

48
source/client.d Normal file
View File

@ -0,0 +1,48 @@
import tristanable.manager : Manager;
import std.socket;
import std.stdio;
import std.conv : to;
public class DClient
{
/**
* tristanabale tag manager
*/
private Manager manager;
this(Address address)
{
/* Initialize the socket */
Socket socket = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.TCP);
socket.connect(address);
/* Initialize the manager */
manager = new Manager(socket);
//init();
}
public void init()
{
manager.sendMessage(1, [0,4,65,66,66,65,69,69]);
writeln(manager.receiveMessage(1));
}
public void auth(string username, string password)
{
byte[] data = [0];
data ~= cast(byte)username.length;
data ~= username;
data ~= password;
writeln(data);
manager.sendMessage(1, data);
byte[] resp = manager.receiveMessage(1);
writeln("auth resp: "~to!(string)(resp));
}
public void disconnect()
{
manager.stopManager();
writeln("manager stopped");
}
}