Пример #1
0
 /// <summary>
 /// accept
 /// </summary>
 public UChannel(USocket socket, UService service) : base(service, ChannelType.Accept)
 {
     this.socket             = socket;
     this.service            = service;
     this.RemoteAddress      = socket.RemoteAddress;
     this.socket.Received   += this.OnRecv;
     this.socket.Disconnect += () => { this.OnError(this, SocketError.SocketError); };
 }
Пример #2
0
        public override AChannel ConnectChannel(string host, int port)
        {
            USocket  newSocket = new USocket(this.poller);
            UChannel channel   = new UChannel(newSocket, host, port, this);

            this.idChannels[channel.Id] = channel;
            return(channel);
        }
Пример #3
0
        public override AChannel ConnectChannel(IPEndPoint ipEndPoint)
        {
            USocket  newSocket = new USocket(this.poller);
            UChannel channel   = new UChannel(newSocket, ipEndPoint, this);

            this.idChannels[channel.Id] = channel;
            return(channel);
        }
Пример #4
0
        public void Update()
        {
            this.OnEvents();

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

            while (true)
            {
                if (NativeMethods.enet_host_check_events(this.host, ref this.eNetEventCache) <= 0)
                {
                    return;
                }

                switch (this.eNetEventCache.Type)
                {
                case EventType.Connect:
                {
                    // 这是一个connect peer
                    if (this.USocketManager.ContainsKey(this.eNetEventCache.Peer))
                    {
                        USocket uSocket = this.USocketManager[this.eNetEventCache.Peer];
                        uSocket.OnConnected();
                        break;
                    }

                    // 这是accept peer
                    if (this.AcceptTcs != null)
                    {
                        this.OnAccepted(this.eNetEventCache);
                        break;
                    }

                    // 如果server端没有acceptasync,则请求放入队列
                    this.connQueue.Enqueue(this.eNetEventCache.Peer);
                    break;
                }

                case EventType.Receive:
                {
                    USocket uSocket = this.USocketManager[this.eNetEventCache.Peer];
                    uSocket.OnReceived(this.eNetEventCache);
                    break;
                }

                case EventType.Disconnect:
                {
                    USocket uSocket = this.USocketManager[this.eNetEventCache.Peer];
                    this.USocketManager.Remove(uSocket.PeerPtr);
                    uSocket.PeerPtr = IntPtr.Zero;
                    uSocket.OnDisconnect(this.eNetEventCache);
                    break;
                }
                }
            }
        }
Пример #5
0
 /// <summary>
 /// connect
 /// </summary>
 public UChannel(USocket socket, string host, int port, UService service) : base(service, ChannelType.Connect)
 {
     this.socket        = socket;
     this.service       = service;
     this.RemoteAddress = host + ":" + port;
     this.socket.ConnectAsync(host, (ushort)port);
     this.socket.Received   += this.OnRecv;
     this.socket.Disconnect += () => { this.OnError(this, SocketError.SocketError); };
 }
Пример #6
0
        public override async Task <AChannel> AcceptChannel()
        {
            USocket socket = await this.poller.AcceptAsync();

            UChannel channel = new UChannel(socket, this);

            this.idChannels[channel.Id] = channel;
            return(channel);
        }
Пример #7
0
 /// <summary>
 /// connect
 /// </summary>
 public UChannel(USocket socket, IPEndPoint ipEndPoint, UService service) : base(service, ChannelType.Connect)
 {
     this.socket        = socket;
     this.service       = service;
     this.RemoteAddress = ipEndPoint;
     this.socket.ConnectAsync(ipEndPoint);
     this.socket.Received   += this.OnRecv;
     this.socket.Disconnect += () => { this.OnError(this, SocketError.SocketError); };
 }
Пример #8
0
        private void OnAccepted(ENetEvent eEvent)
        {
            if (eEvent.Type == EventType.Disconnect)
            {
                this.AcceptTcs.TrySetException(new Exception("socket disconnected in accpet"));
            }

            USocket socket = new USocket(eEvent.Peer, this);

            this.USocketManager.Add(socket.PeerPtr, socket);
            socket.OnAccepted();

            var tcs = this.AcceptTcs;

            this.AcceptTcs = null;
            tcs.SetResult(socket);
        }
Пример #9
0
        public Task <USocket> AcceptAsync()
        {
            if (this.AcceptTcs != null)
            {
                throw new Exception("do not accept twice!");
            }

            // 如果有请求连接缓存的包,从缓存中取
            if (this.connQueue.Count > 0)
            {
                IntPtr ptr = this.connQueue.Dequeue();

                USocket socket = new USocket(ptr, this);
                this.USocketManager.Add(ptr, socket);
                return(Task.FromResult(socket));
            }

            this.AcceptTcs = new TaskCompletionSource <USocket>();
            return(this.AcceptTcs.Task);
        }
Пример #10
0
 public void Add(IntPtr peerPtr, USocket uSocket)
 {
     this.sockets.Add(peerPtr, uSocket);
 }