/// <summary> /// Reads socket and processes packet /// </summary> /// <param name="socket">The active socket.</param> protected virtual void OnSocket(Socket socket) { try { if (socket.Poll(-1, SelectMode.SelectRead)) { var args = new ClientConnectedEventArgs(socket, Logger, _bufferSize); EndPoint remoteEndPoint = new IPEndPoint(0, 0); if (socket.Available == 0) { return; } args.ChannelBuffer.BytesTransferred = socket.ReceiveFrom(args.ChannelBuffer.Buffer, SocketFlags.None, ref remoteEndPoint); args.Channel.RemoteEndpoint = remoteEndPoint; if (args.ChannelBuffer.BytesTransferred > 0) { OnClientConnected(args); if (args.AllowConnect == false) { if (args.Response != null) { int sentBytes; while ((sentBytes = args.Response.Read(_sendBuffer, 0, _sendBuffer.Length)) > 0) { socket.Send(_sendBuffer, sentBytes, SocketFlags.None); } } Logger?.LogDebug( "PACKET request on {LocalEndPoint} from {RemoteEndPoint} with channel id {ChannelId} was denied access to connect.", socket.LocalEndPoint, args.Channel.RemoteEndpoint, args.Channel.ChannelId); } } } else { Thread.Sleep(10); } } catch (SocketException ex) { if (ex.ErrorCode == (int)SocketError.ConnectionReset) { Logger.LogError(ex, "SocketListener: socket connection reset"); } } catch (Exception ex) { HandleDisconnect(SocketError.SocketError, ex); } }
/// <summary> /// A client has connected (nothing have been sent or received yet) /// </summary> /// <param name="args">The <see cref="ClientConnectedEventArgs"/> instance containing the event data.</param> protected virtual void OnClientConnected(ClientConnectedEventArgs args) { ClientConnected(this, args); }