/// <summary> /// Bind socket /// </summary> /// <param name="local">Local EndPoint</param> public static TuringSocket Bind(IPEndPoint local) { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Bind(local); socket.Listen(50); TuringSocket ret = new TuringSocket(socket, local); socket.BeginAccept(ret.OnAccept, ret); return(ret); }
/// <summary> /// Connect to /// </summary> /// <param name="remote">Remote EndPoint</param> public static TuringSocket ConnectTo(IPEndPoint remote) { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(remote); TuringSocket ret = new TuringSocket(socket, remote); // WaitMessage ReadMessageAsync(new TuringMessageState(ret)); return(ret); }
void RaiseOnMessage(TuringSocket sender, TuringMessage message) { if (message == null) { return; } // Enqueue if (EnqueueMessages) { lock (_Readed) { _Readed.Enqueue(message); } _Signal.Set(); } OnMessage?.Invoke(sender, message); }
void OnAccept(IAsyncResult result) { // Get Sockets TuringSocket main; Socket source; try { main = (TuringSocket)result.AsyncState; if (main == null) { return; } source = main._Socket.EndAccept(result); if (source == null) { return; } } catch { return; } // Connect try { TuringSocket ret = new TuringSocket(source, (IPEndPoint)source.RemoteEndPoint); // CopyEvents ret.OnMessage += RaiseOnMessage; ReadMessageAsync(new TuringMessageState(ret)); } catch// (Exception e) { } // Re-accept try { main._Socket.BeginAccept(OnAccept, main); } catch { } }
/// <summary> /// Constructor /// </summary> /// <param name="source">Source socket</param> public TuringMessageState(TuringSocket source) { Source = source; Clear(); }