Connection/disconnection OK
This commit is contained in:
		
							parent
							
								
									72816ed1a7
								
							
						
					
					
						commit
						2a887f5142
					
				
					 2 changed files with 31 additions and 12 deletions
				
			
		| 
						 | 
					@ -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;
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
                if (sockClient.Available > 0) {
 | 
					            try {
 | 
				
			||||||
                    byte[] rawData = new byte[sockClient.Available];
 | 
					                while (sockClient.Connected && IsSocketConnected(sockClient)) {
 | 
				
			||||||
                    sockClient.Receive(rawData, rawData.Length, 0);
 | 
					                    byte[] rawData = new byte[0];
 | 
				
			||||||
 | 
					                    byte[] buf = new byte[256];
 | 
				
			||||||
 | 
					                    int recv;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                    while (sockClient.Available>0 && (recv = sockClient.Receive(buf))>0) {
 | 
				
			||||||
 | 
					                        rawData = rawData.Concat(buf.Take(recv)).ToArray();
 | 
				
			||||||
 | 
					                        Thread.Sleep(10);
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    if (rawData.Length > 0) {
 | 
				
			||||||
                        string data = Encoding.UTF8.GetString(rawData);
 | 
					                        string data = Encoding.UTF8.GetString(rawData);
 | 
				
			||||||
 | 
					                    
 | 
				
			||||||
                        Debug("Queued "+data);
 | 
					                        Debug("Queued "+data);
 | 
				
			||||||
                        sockBufferIn.Enqueue(new Tuple<string,Socket>(data, sockClient));
 | 
					                        sockBufferIn.Enqueue(new Tuple<string,Socket>(data, sockClient));
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                Thread.Sleep(10);
 | 
					                    Thread.Sleep(100);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
                
 | 
					            }
 | 
				
			||||||
 | 
					            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…
	
	Add table
		
		Reference in a new issue