/// <summary> /// Called by the asynchronous callback when there is a new incoming connection. /// </summary> private void EndAccept(IAsyncResult res) { const Int32 MAX_BUFFER_SIZE = 2048; Socket socket = null; try { socket = mSocket.EndAccept(res); } catch { Accept(); return; } socket.SendBufferSize = MAX_BUFFER_SIZE; socket.ReceiveBufferSize = MAX_BUFFER_SIZE; TcpSocket client = TcpSocket.Create(this, socket, MAX_BUFFER_SIZE); if (client == null) { try { socket.Close(); } catch { Accept(); return; } } if (OnConnect != null) { OnConnect(client); } Accept(); }
/// <summary> /// Force the disconnection of the specified client. /// </summary> public void InvokeDisconnect(TcpSocket aClient) { if (aClient == null || !aClient.IsAlive) { return; } aClient.Disconnect(); if (OnDisconnect != null) { OnDisconnect(aClient); } }
/// <summary> /// Create the client of the specified server. /// </summary> public static TcpSocket Create(TcpServer aServer, Socket aSocket, int aBufSize) { if (aSocket == null || aServer == null || aBufSize == 0) { throw new ArgumentException(); } TcpSocket client = new TcpSocket(aServer, aSocket, aBufSize); client.BeginReceive(); client.mIsAlive = true; return(client); }