Connection/disconnection OK
This commit is contained in:
parent
72816ed1a7
commit
2a887f5142
|
@ -42,5 +42,8 @@ void main() {
|
||||||
sock.sendWait(`{"action":"bind","target":"testnpc"}`);
|
sock.sendWait(`{"action":"bind","target":"testnpc"}`);
|
||||||
writeln(sock.receiveWait);
|
writeln(sock.receiveWait);
|
||||||
|
|
||||||
|
Thread.sleep(dur!"seconds"(1));
|
||||||
sock.shutdown(SocketShutdown.BOTH);
|
sock.shutdown(SocketShutdown.BOTH);
|
||||||
|
|
||||||
|
Thread.sleep(dur!"seconds"(1));
|
||||||
}
|
}
|
|
@ -130,30 +130,46 @@ namespace CLRScript
|
||||||
void RequestHandler(IAsyncResult state)
|
void RequestHandler(IAsyncResult state)
|
||||||
{
|
{
|
||||||
Socket sockClient = sockServer.EndAccept(state);
|
Socket sockClient = sockServer.EndAccept(state);
|
||||||
|
|
||||||
sockServer.BeginAccept(new AsyncCallback(RequestHandler), null);
|
sockServer.BeginAccept(new AsyncCallback(RequestHandler), null);
|
||||||
sockClient.Blocking = true;
|
|
||||||
|
|
||||||
while (sockClient.Connected) {
|
sockClient.Blocking = false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
while (sockClient.Connected && IsSocketConnected(sockClient)) {
|
||||||
|
byte[] rawData = new byte[0];
|
||||||
|
byte[] buf = new byte[256];
|
||||||
|
int recv;
|
||||||
|
|
||||||
|
|
||||||
if (sockClient.Available > 0) {
|
while (sockClient.Available>0 && (recv = sockClient.Receive(buf))>0) {
|
||||||
byte[] rawData = new byte[sockClient.Available];
|
rawData = rawData.Concat(buf.Take(recv)).ToArray();
|
||||||
sockClient.Receive(rawData, rawData.Length, 0);
|
Thread.Sleep(10);
|
||||||
|
}
|
||||||
|
|
||||||
string data = Encoding.UTF8.GetString(rawData);
|
if (rawData.Length > 0) {
|
||||||
Debug("Queued "+data);
|
string data = Encoding.UTF8.GetString(rawData);
|
||||||
sockBufferIn.Enqueue(new Tuple<string,Socket>(data, sockClient));
|
|
||||||
|
Debug("Queued "+data);
|
||||||
|
sockBufferIn.Enqueue(new Tuple<string,Socket>(data, sockClient));
|
||||||
|
}
|
||||||
|
Thread.Sleep(100);
|
||||||
}
|
}
|
||||||
Thread.Sleep(10);
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
Debug(e.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
//Remove stored bind
|
//Remove stored bind
|
||||||
|
Debug("EXIT");
|
||||||
var sock = sockClients.First(kvp => kvp.Value==sockClient);
|
var sock = sockClients.First(kvp => kvp.Value==sockClient);
|
||||||
sockClients.Remove(sock.Key);
|
sockClients.Remove(sock.Key);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
public static bool IsSocketConnected(Socket socket)
|
||||||
|
{
|
||||||
|
try { return !(socket.Poll(1, SelectMode.SelectRead) && socket.Available == 0); }
|
||||||
|
catch (SocketException) { return false; }
|
||||||
|
}
|
||||||
|
|
||||||
Socket sockServer;
|
Socket sockServer;
|
||||||
Queue<Tuple<string,Socket>> sockBufferIn;//Request & client
|
Queue<Tuple<string,Socket>> sockBufferIn;//Request & client
|
||||||
|
|
Loading…
Reference in New Issue