コード例 #1
0
 private void RepeatCallback(IAsyncResult result)
 {
     try {
         int n = _socket.EndReceive(result);
         if (n > 0)
         {
             _data.SetLength(0, n);
             _handler.OnData(_data);
             if (_socketStatus != SocketStatus.Closed)
             {
                 RepeatAsyncRead();
             }
         }
         else if (n < 0)
         {
             //in case of Win9x, EndReceive() returns 0 every 288 seconds even if no data is available
             RepeatAsyncRead();
         }
         else
         {
             _handler.OnClosed();
         }
     }
     catch (Exception ex) {
         if ((ex is SocketException) && ((SocketException)ex).ErrorCode == 995)
         {
             //in case of .NET1.1 on Win9x, EndReceive() changes the behavior. it throws SocketException with an error code 995.
             RepeatAsyncRead();
         }
         else if (_socketStatus != SocketStatus.Closed)
         {
             _handler.OnError(ex);
         }
     }
 }
コード例 #2
0
 /// <summary>
 /// Receiving thread
 /// </summary>
 private void ReceivingThread()
 {
     try {
         while (true)
         {
             int n = _socket.Receive(_data.Data, 0, _data.Data.Length, SocketFlags.None);
             if (n > 0)
             {
                 _data.SetLength(0, n);
                 _handler.OnData(_data);
             }
             else if (n == 0)
             {
                 // shut down detected
                 FireOnClosed();
                 break;
             }
         }
     }
     catch (ObjectDisposedException) {
         // _socket has been closed
         FireOnClosed();
     }
     catch (Exception ex) {
         if (_socketStatus != SocketStatus.Closed)
         {
             _handler.OnError(ex);
         }
     }
 }
コード例 #3
0
ファイル: X11Socket.cs プロジェクト: poderosaproject/poderosa
        public void StartReceivingThread(Action<DataFragment> onDataCallback, Action onClosedCallback)
        {
            if (_socket == null || _receivingThread != null) {
                return;
            }

            _receivingThread = new Thread(() => {
                byte[] buffer = new byte[0x20000];
                DataFragment dataFrag = new DataFragment(buffer, 0, 0);

                try {
                    while (true) {
                        int received = _socket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
                        if (received > 0) {
                            dataFrag.SetLength(0, received);
                            onDataCallback(dataFrag);
                        }
                    }
                }
                catch (Exception e) {
                    if (!_shutdown) {
                        System.Diagnostics.Debug.WriteLine(e.Message);
                        System.Diagnostics.Debug.WriteLine(e.StackTrace);
                    }

                    if (!_socket.Connected && !_shutdown) {
                        // closed by the peer
                        onClosedCallback();
                    }
                }
            });
            _receivingThread.Start();
        }