Ejemplo n.º 1
0
        public void WaitForData(Socket socket, int clientNumber)
        {
            try
            {
                if (_workerReceiveCallBack == null)
                {
                    _workerReceiveCallBack = new AsyncCallback(OnDataReceived);
                }
                var socketPacket = new SocketPacket(socket, clientNumber);

                socket.BeginReceive(socketPacket.Buffer,
                                    0,
                                    socketPacket.Buffer.Length,
                                    SocketFlags.None,
                                    _workerReceiveCallBack,
                                    socketPacket);
            }
            catch (ObjectDisposedException)
            {
                //System.Diagnostics.Trace.WriteLine("#" + clientNumber.ToString() + " - WaitForData: Socket has been closed");
                return;
            }
            catch (SocketException se)
            {
                if (se.SocketErrorCode == SocketError.ConnectionReset) // connection reset by peer
                {
                    // remove worker socket of closed client
                    DisconnectClient(clientNumber);
                }
                else if (se.SocketErrorCode == SocketError.ConnectionAborted) // connection aborted
                {
                    // remove worker socket of closed client
                    DisconnectClient(clientNumber);
                }
                else
                {
                    Traces.NntpServerTraceEvent(System.Diagnostics.TraceEventType.Critical, "[Client {0}] Server.WaitForData failed: {1}", clientNumber, Traces.ExceptionToString(se));
                }
                return;
            }
            catch (Exception ex)
            {
                Traces.NntpServerTraceEvent(System.Diagnostics.TraceEventType.Critical, "[Client {0}] Server.WaitForData failed: {1}", clientNumber, Traces.ExceptionToString(ex));
            }
        }
Ejemplo n.º 2
0
        protected void SendData(string data, int clientNumber)
        {
            byte[] buffer;

            try
            {
                buffer = _encodingSend.GetBytes(data);
            }
            catch (Exception ex)
            {
                Traces.NntpServerTraceEvent(System.Diagnostics.TraceEventType.Critical, "[Client {0}] Server.SendData failed to encode data: {1}\r\n{2}", clientNumber, data, Traces.ExceptionToString(ex));
                return;
            }

            Socket workerSocket;

            try
            {
                lock (_workerSockets)
                {
                    workerSocket = _workerSockets[clientNumber];
                }
            }
            catch
            {
                // continue
                //System.Diagnostics.Trace.WriteLine("Server::SendData - unable to access entry [" + clientNumber + "]");
                return;
            }
            if (workerSocket == null)
            {
                Traces.NntpServerTraceEvent(System.Diagnostics.TraceEventType.Warning, "[Client {0}] Server.SendData failed (client removed from the worker list)", clientNumber);
                return;
            }
            try
            {
                if (_workerSendCallBack == null)
                {
                    _workerSendCallBack = new AsyncCallback(OnDataSend);
                }
                var socketPacket = new SocketPacket(workerSocket, clientNumber);

                workerSocket.BeginSend(buffer,
                                       0,
                                       buffer.Length,
                                       SocketFlags.None,
                                       _workerSendCallBack,
                                       socketPacket);
            }
            catch (ObjectDisposedException)
            {
                //System.Diagnostics.Trace.WriteLine("#" + clientNumber.ToString() + " - OnDataSend: Socket has been closed");
                return;
            }
            catch (SocketException se)
            {
                if (se.SocketErrorCode == SocketError.ConnectionReset) // connection reset by peer
                {
                    // remove worker socket of closed client
                    DisconnectClient(clientNumber);
                }
                else if (se.SocketErrorCode == SocketError.ConnectionAborted) // connection aborted
                {
                    // remove worker socket of closed client
                    DisconnectClient(clientNumber);
                }
                else
                {
                    Traces.NntpServerTraceEvent(System.Diagnostics.TraceEventType.Critical, "[Client {0}] Server.SendData failed: {1}", clientNumber, Traces.ExceptionToString(se));
                }
                return;
            }
            catch (Exception ex)
            {
                Traces.NntpServerTraceEvent(System.Diagnostics.TraceEventType.Critical, "[Client {0}] Server.SendData failed: {1}", clientNumber, Traces.ExceptionToString(ex));
            }
        }