Example #1
0
 private void CloseSocket(DummySingleSocket s)
 {
     poll.Unregister(s.Socket);
     singleSockets.Remove(s.Socket.Handle);
     s.Socket.Close();
     s.Socket = null;
     s.Handler.LoseConnection(s);
 }
Example #2
0
        public DummySingleSocket StartConnection(IPEndPoint dns, DummyEncrypter handler)
        {
            if (handler == null)
            {
                handler = this.handler;
            }
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            sock.Blocking = false;
            try
            {
                sock.Connect(dns);
            }
            catch { }
            poll.Register(sock, PollMode.PollIn);
            DummySingleSocket s = new DummySingleSocket(this, sock, handler);

            singleSockets[sock.Handle] = s;
            return(s);
        }
Example #3
0
 public void AddToDeadFromWrite(DummySingleSocket item)
 {
     deadFromWrite.Add(item);
 }
Example #4
0
 public void HandleEvents(List <PollItem> events)
 {
     foreach (PollItem pi in events)
     {
         if (pi.Socket.Handle == server.Handle)
         {
             if ((pi.Mode & (PollMode.PollError | PollMode.PollHangUp)) != 0)
             {
                 poll.Unregister(server);
                 server.Close();
                 //TODO: print "lost server socket"
             }
             else
             {
                 Socket newsock = server.Accept();
                 newsock.Blocking = false;
                 DummySingleSocket nss = new DummySingleSocket(this, newsock, handler);
                 singleSockets[newsock.Handle] = nss;
                 poll.Register(newsock, PollMode.PollIn);
                 handler.MakeExternalConnection(nss);
             }
         }
         else
         {
             DummySingleSocket s = (DummySingleSocket)singleSockets[pi.Socket.Handle];
             if (s == null)
             {
                 continue;
             }
             s.IsConnected = true;
             if ((pi.Mode & (PollMode.PollError | PollMode.PollHangUp)) != 0)
             {
                 CloseSocket(s);
                 continue;
             }
             if ((pi.Mode & PollMode.PollIn) != 0)
             {
                 try
                 {
                     s.LastHit = DateTime.Now;
                     int available = s.Socket.Available;
                     if (available == 0)
                     {
                         CloseSocket(s);
                     }
                     else
                     {
                         byte[] data = new byte[available];
                         s.Socket.Receive(data, 0, available, SocketFlags.None);
                         s.Handler.DataCameIn(s, data);
                     }
                 }
                 catch (SocketException ex)
                 {
                     if (ex.ErrorCode != 10035) // WSAEWOULDBLOCK
                     {
                         CloseSocket(s);
                         continue;
                     }
                 }
             }
             if (((pi.Mode & PollMode.PollOut) != 0) && (s.Socket != null) && (!s.IsFlushed))
             {
                 s.TryWrite();
                 if (s.IsFlushed)
                 {
                     s.Handler.FlushConnection(s);
                 }
             }
         }
     }
 }