Beispiel #1
0
        public void RunOnce(int timeout = 0)
        {
            if (timeout < 0)
            {
                throw new ArgumentOutOfRangeException(string.Format("timeout: {0}", timeout));
            }

            this.OnEvents();

            if (this.Service(timeout) < 0)
            {
                return;
            }

            while (true)
            {
                EEvent eEvent = this.GetEvent();
                if (eEvent == null)
                {
                    return;
                }

                switch (eEvent.Type)
                {
                case EventType.Connect:
                {
                    // 这是一个connect peer
                    if (this.PeersManager.ContainsKey(eEvent.PeerPtr))
                    {
                        ESocket eSocket = this.PeersManager[eEvent.PeerPtr];
                        eSocket.OnConnected(eEvent);
                    }
                    // accept peer
                    else
                    {
                        // 如果server端没有acceptasync,则请求放入队列
                        if (!this.PeersManager.ContainsKey(IntPtr.Zero))
                        {
                            this.connEEvents.AddLast(eEvent);
                        }
                        else
                        {
                            ESocket eSocket = this.PeersManager[IntPtr.Zero];
                            eSocket.OnConnected(eEvent);
                        }
                    }
                    break;
                }

                case EventType.Receive:
                {
                    ESocket eSocket = this.PeersManager[eEvent.PeerPtr];
                    eSocket.OnReceived(eEvent);
                    break;
                }

                case EventType.Disconnect:
                {
                    // 如果链接还在缓存中,则删除
                    foreach (EEvent connEEvent in this.connEEvents)
                    {
                        if (connEEvent.PeerPtr != eEvent.PeerPtr)
                        {
                            continue;
                        }
                        this.connEEvents.Remove(connEEvent);
                        return;
                    }

                    // 链接已经被应用层接收
                    eEvent.EventState = EventState.DISCONNECTED;
                    ESocket eSocket = this.PeersManager[eEvent.PeerPtr];
                    this.PeersManager.Remove(eEvent.PeerPtr);

                    // 等待的task将抛出异常
                    if (eSocket.Connected != null)
                    {
                        eSocket.OnConnected(eEvent);
                    }
                    else if (eSocket.Received != null)
                    {
                        eSocket.OnReceived(eEvent);
                    }
                    else if (eSocket.Disconnect != null)
                    {
                        eSocket.OnDisconnect(eEvent);
                    }

                    eSocket.OnError(ErrorCode.ClientDisconnect);
                    break;
                }
                }
            }
        }
Beispiel #2
0
 public void Add(IntPtr peerPtr, ESocket eSocket)
 {
     this.peersManager.Add(peerPtr, eSocket);
 }