Inheritance: IDisposable
Exemplo n.º 1
0
        public Task <USocket> AcceptAsync()
        {
            if (this.AcceptTcs != null)
            {
                throw new Exception("do not accept twice!");
            }

            var tcs = new TaskCompletionSource <USocket>();

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

                USocket socket = new USocket(ptr, this);
                this.USocketManager.Add(ptr, socket);
                tcs.SetResult(socket);
            }
            else
            {
                this.AcceptTcs = tcs;
            }
            return(tcs.Task);
        }
Exemplo n.º 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;
		}
Exemplo n.º 3
0
        public void Update()
        {
            this.OnEvents();

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

            while (true)
            {
                ENetEvent eNetEvent = this.TryGetEvent();
                if (eNetEvent == null)
                {
                    return;
                }

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

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

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

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

                case EventType.Disconnect:
                {
                    USocket uSocket = this.USocketManager[eNetEvent.Peer];
                    this.USocketManager.Remove(uSocket.PeerPtr);
                    uSocket.PeerPtr = IntPtr.Zero;
                    uSocket.OnDisconnect(eNetEvent);
                    break;
                }
                }
            }
        }
Exemplo n.º 4
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); };
 }
Exemplo n.º 5
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); };
		}
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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;
                }
                }
            }
        }
Exemplo n.º 8
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); };
 }
Exemplo n.º 9
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); };
		}
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
        public override AChannel GetChannel(string host, int port)
        {
            UChannel channel = null;

            USocket newSocket = new USocket(this.poller);

            channel = new UChannel(newSocket, host, port, this);
            newSocket.Disconnect       += () => this.OnChannelError(channel.Id, SocketError.SocketError);
            this.idChannels[channel.Id] = channel;
            return(channel);
        }
Exemplo n.º 12
0
        public void Update()
        {
            this.OnEvents();

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

            while (true)
            {
                ENetEvent eNetEvent = this.GetEvent();
                if (eNetEvent == null)
                {
                    return;
                }

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

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

                case EventType.Disconnect:
                {
                    USocket uSocket = this.USocketManager[eNetEvent.Peer];
                    this.USocketManager.Remove(uSocket.PeerPtr);
                    uSocket.PeerPtr = IntPtr.Zero;
                    uSocket.OnDisconnect(eNetEvent);
                    break;
                }
                }
            }
        }
Exemplo n.º 13
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);
        }
Exemplo n.º 14
0
 public UChannel(USocket socket, string host, int port, UService service) : base(service)
 {
     this.socket        = socket;
     this.service       = service;
     this.remoteAddress = host + ":" + port;
 }
Exemplo n.º 15
0
		public void Add(IntPtr peerPtr, USocket uSocket)
		{
			this.sockets.Add(peerPtr, uSocket);
		}
Exemplo n.º 16
0
 public void Add(IntPtr peerPtr, USocket uSocket)
 {
     this.sockets.Add(peerPtr, uSocket);
 }
Exemplo n.º 17
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);
		}
Exemplo n.º 18
0
		public Task<USocket> AcceptAsync()
		{
			if (this.AcceptTcs != null)
			{
				throw new Exception("do not accept twice!");
			}

			var tcs = new TaskCompletionSource<USocket>();

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

				USocket socket = new USocket(ptr, this);
				this.USocketManager.Add(ptr, socket);
				tcs.SetResult(socket);
			}
			else
			{
				this.AcceptTcs = tcs;
			}
			return tcs.Task;
		}