60 lines
1.3 KiB
D
60 lines
1.3 KiB
D
#!dmd -run
|
|
|
|
import core.thread;
|
|
import std.stdio;
|
|
import std.socket;
|
|
import std.conv;
|
|
import std.utf;
|
|
import std.string;
|
|
import std.array;
|
|
|
|
string receiveWait(Socket sock){
|
|
bool blocking = sock.blocking;
|
|
|
|
sock.blocking = false;
|
|
ubyte[] rawData;
|
|
ubyte[256] buf;
|
|
int recv;
|
|
while((recv = sock.receive(buf))>=0 || rawData.length==0){
|
|
if(recv>0){
|
|
rawData ~= buf[0..recv];
|
|
}
|
|
|
|
Thread.sleep(dur!"msecs"(10));
|
|
}
|
|
sock.blocking = blocking;
|
|
return (cast(char*)rawData.ptr).fromStringz.to!string;
|
|
}
|
|
void sendWait(Socket sock, string data){
|
|
bool blocking = sock.blocking;
|
|
|
|
sock.blocking = true;
|
|
sock.send(data.toUTF8);
|
|
sock.blocking = blocking;
|
|
}
|
|
|
|
void main() {
|
|
writeln("Start");stdout.flush;
|
|
auto sock = new TcpSocket;
|
|
sock.blocking = true;
|
|
sock.connect(new InternetAddress("127.0.0.1", 8080));
|
|
writeln("Connected");stdout.flush;
|
|
|
|
sock.sendWait(`{"type":"bind","target":"testnpc"}`);
|
|
writeln(sock.receiveWait);
|
|
|
|
while(1){
|
|
write(">");stdout.flush;
|
|
auto cmd = stdin.readln().strip;
|
|
if(cmd == "")break;
|
|
|
|
writeln("==>",`{"type":"command","cmd":["`~cmd.replace(`"`,`\"`)~`"]}`);stdout.flush();
|
|
sock.sendWait(`{"type":"command","cmd":["`~cmd.replace(`"`,`\"`)~`"]}`);
|
|
//writeln(sock.receiveWait);
|
|
}
|
|
|
|
writeln("Close");stdout.flush;
|
|
sock.shutdown(SocketShutdown.BOTH);
|
|
Thread.sleep(dur!"seconds"(1));
|
|
writeln("Exit");stdout.flush;
|
|
} |