Receiving multiple messages on same socket
This commit is contained in:
parent
fdb3b56623
commit
20fbc943ce
@ -13,8 +13,10 @@ void main() {
|
||||
sock.connect(new InternetAddress("127.0.0.1", 8080));
|
||||
writeln("Connected");
|
||||
|
||||
auto msg = `{"action":"bind","target":"testnpc"}`;
|
||||
sock.send(msg.toUTF8);
|
||||
sock.send(`{"action":"bind","target":"testnpc"}`.toUTF8);
|
||||
writeln("Sent");
|
||||
Thread.sleep(dur!"seconds"(1));
|
||||
sock.send(`{"action":"getenv","target":"testnpc"}`.toUTF8);
|
||||
writeln("Sent2");
|
||||
Thread.sleep(dur!"seconds"(1));
|
||||
}
|
@ -57,7 +57,7 @@ namespace CLRScript
|
||||
debugFile = File.AppendText("nwnx4\\customlog.txt");
|
||||
Debug("====================================> Start");
|
||||
|
||||
sockBufferIn = new Queue<string>();
|
||||
sockBufferIn = new Queue<Tuple<string,Socket>>();
|
||||
sockClients = new Dictionary<string,Socket>();
|
||||
SocketConfigure();
|
||||
|
||||
@ -83,38 +83,37 @@ namespace CLRScript
|
||||
sockServer.Bind(iep);
|
||||
sockServer.Listen(10);
|
||||
|
||||
sockThread = new Thread(new ThreadStart(SocketHandler));
|
||||
sockThread.Start();
|
||||
}
|
||||
|
||||
void SocketHandler()
|
||||
{
|
||||
while(true){
|
||||
Debug("Accepting");
|
||||
sockServer.BeginAccept(new AsyncCallback(RequestHandler), null);
|
||||
//Wait for EndAccept?
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
sockServer.BeginAccept(new AsyncCallback(RequestHandler), null);
|
||||
}
|
||||
|
||||
void RequestHandler(IAsyncResult state)
|
||||
{
|
||||
Socket sockClient = sockServer.EndAccept(state);
|
||||
|
||||
byte[] rawData = new byte[1024];
|
||||
sockClient.Receive(rawData, rawData.Length, 0);
|
||||
sockServer.BeginAccept(new AsyncCallback(RequestHandler), null);
|
||||
sockClient.Blocking = true;
|
||||
|
||||
while (sockClient.Connected) {
|
||||
byte[] rawData = new byte[sockClient.Available];
|
||||
if (sockClient.Available > 0) {
|
||||
sockClient.Receive(rawData, rawData.Length, 0);
|
||||
|
||||
string data = Encoding.UTF8.GetString(rawData);
|
||||
Debug("Queued "+data);
|
||||
sockBufferIn.Enqueue(new Tuple<string,Socket>(data, sockClient));
|
||||
}
|
||||
else
|
||||
Thread.Sleep(100);//TODO: Find another way than sleeping
|
||||
|
||||
|
||||
}
|
||||
|
||||
string data = Encoding.UTF8.GetString(rawData);
|
||||
Debug("Queued "+data);
|
||||
sockBufferIn.Enqueue(data);
|
||||
}
|
||||
|
||||
Socket sockServer;
|
||||
Thread sockThread;
|
||||
Queue<string> sockBufferIn;
|
||||
Queue<Tuple<string,Socket>> sockBufferIn;//Request & client
|
||||
|
||||
//PNJ tag => Client socket
|
||||
Dictionary<string, Socket> sockClients;
|
||||
Dictionary<string, Socket> sockClients;//PNJ tag => Client socket
|
||||
|
||||
private void Debug(string msg){
|
||||
debugFile.WriteLine(DateTime.Now.ToLongTimeString()+": "+msg);
|
||||
|
Loading…
Reference in New Issue
Block a user