Пример #1
0
 public void RegisterSocketAccept(BufferedSocket socket, Action<BufferedSocket> callback)
 {
     var thread = new Thread(() =>
                                 {
                                     try
                                     {
                                         socket.Socket.Blocking = true;
                                         while (socket.IsBound)
                                         {
                                             BufferedSocket newConnection = socket.Accept();
                                             //newConnection.Blocking = false;
                                             lock (this)
                                             {
                                                 Thread.BeginCriticalRegion();
                                                 callback(newConnection);
                                                 Thread.EndCriticalRegion();
                                             }
                                         }
                                     }
                                     catch (ThreadAbortException)
                                     {
                                         Console.WriteLine("Accept for socket Aborted");
                                         return;
                                     }
                                     catch (SocketException ex)
                                     {
                                         if (ex.ErrorCode == 10004)
                                         {
                                             Console.WriteLine("Accept for socket Aborted");
                                             return;
                                         }
                                         else
                                         {
                                             Console.WriteLine("Unknown exception in accept thread: " +
                                                               ex.Message);
                                         }
                                     }
                                 });
     thread.Name = "Socket Accept " + socket.LocalEndPointIp;
     _onAcceptSockets.Add(socket, thread);
     thread.Start();
 }
Пример #2
0
        public void RegisterSocketAccept(BufferedSocket socket, Action<BufferedSocket> callback)
        {
            #if DEBUG_VERBOSE
            Console.WriteLine("RegisterSocketAccept");
            #endif

            var handle = (int) socket.Handle;
            if (handle == -1)
            {
                Console.WriteLine("RegisterSocketWrite socket already closed");
                return;
            }
            if (!AddSocket(handle, EpollEvents.EPOLLIN))
            {
            }
            Action acceptHandle = () =>
                                      {
                                          BufferedSocket s2 = null;
                                          try
                                          {
                                              s2 = socket.Accept();
                                          }
                                          catch (Exception)
                                          {
                                              //TODO: unregister uing intger handle
                                              UnregisterSocketAccept(socket);
                                              return;
                                          }

                                          callback(s2);
                                      };

            if (_onAcceptSockets.ContainsKey(handle))
                _onAcceptSockets[handle] = acceptHandle;
            else
                _onAcceptSockets.Add(handle, acceptHandle);
        }
Пример #3
0
        public void UpdateSocketAccept(BufferedSocket socket, Action<BufferedSocket> callback)
        {
            var handle = (int) socket.Handle;
            if (handle == -1)
            {
                Console.WriteLine("UpdateSocketAccept socket already closed");
                return;
            }
            Action acceptHandle = () =>
                                      {
                                          BufferedSocket s2 = null;
                                          try
                                          {
                                              s2 = socket.Accept();
                                          }
                                          catch (Exception)
                                          {
                                              UnregisterSocketAccept(socket);
                                              return;
                                          }

                                          callback(s2);
                                      };

            if (_onAcceptSockets.ContainsKey(handle))
                _onAcceptSockets[handle] = acceptHandle;
        }