コード例 #1
0
ファイル: SocketListener.cs プロジェクト: Kiphaz/PureActive
        /// <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);
            }
        }
コード例 #2
0
 /// <summary>
 /// Provides the observer with new data.
 /// </summary>
 /// <param name="value">The current notification information.</param>
 public void OnNext(T value)
 {
     _logger.LogDebug(OnNextMsgTemplate, _name, value);
 }