Inheritance: IDisposable
Esempio 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);
        }
Esempio 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;
		}
Esempio 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;
                }
                }
            }
        }
Esempio 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); };
 }
Esempio 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); };
		}
Esempio 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);
        }
Esempio 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;
                }
                }
            }
        }
Esempio 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); };
 }
Esempio 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); };
		}
Esempio 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);
        }
Esempio 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);
        }
Esempio 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;
                }
                }
            }
        }
Esempio 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);
        }
Esempio 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;
 }
Esempio n. 15
0
		public void Add(IntPtr peerPtr, USocket uSocket)
		{
			this.sockets.Add(peerPtr, uSocket);
		}
Esempio n. 16
0
 public void Add(IntPtr peerPtr, USocket uSocket)
 {
     this.sockets.Add(peerPtr, uSocket);
 }
Esempio 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);
		}
Esempio 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;
		}