Beispiel #1
0
        internal void OnDisconnect(ENetEvent eNetEvent)
        {
            if (this.Disconnect == null)
            {
                return;
            }

            Action <ENetEvent> localDisconnect = this.Disconnect;

            this.Disconnect = null;
            localDisconnect(eNetEvent);
        }
Beispiel #2
0
        internal void OnConnected(ENetEvent eNetEvent)
        {
            if (this.Connected == null)
            {
                return;
            }
            Action <ENetEvent> localConnected = this.Connected;

            this.Connected = null;
            // 此调用将让await ConnectAsync返回,所以null必须在此之前设置
            localConnected(eNetEvent);
        }
Beispiel #3
0
        private ENetEvent GetEvent()
        {
            if (this.eNetEventCache == null)
            {
                this.eNetEventCache = new ENetEvent();
            }
            if (NativeMethods.ENetHostCheckEvents(this.host, this.eNetEventCache) <= 0)
            {
                return(null);
            }
            ENetEvent eNetEvent = this.eNetEventCache;

            this.eNetEventCache = null;
            return(eNetEvent);
        }
Beispiel #4
0
 internal void OnReceived(ENetEvent eNetEvent)
 {
     // 如果应用层还未调用readasync则将包放到缓存队列
     if (this.Received == null)
     {
         using (UPacket packet = new UPacket(eNetEvent.Packet))
         {
             byte[] bytes = packet.Bytes;
             this.recvQueue.Enqueue(bytes);
         }
     }
     else
     {
         Action <ENetEvent> localReceived = this.Received;
         this.Received = null;
         // 此调用将让await ReadAsync返回,所以null必须在此之前设置
         localReceived(eNetEvent);
     }
 }
Beispiel #5
0
		internal static extern int ENetHostService(IntPtr host, ENetEvent ev, uint timeout);
Beispiel #6
0
		internal static extern int ENetHostCheckEvents(IntPtr host, ENetEvent ev);
Beispiel #7
0
		private ENetEvent GetEvent()
		{
			if (this.eNetEventCache == null)
			{
				this.eNetEventCache = new ENetEvent();
			}
			if (NativeMethods.ENetHostCheckEvents(this.host, this.eNetEventCache) <= 0)
			{
				return null;
			}
			ENetEvent eNetEvent = this.eNetEventCache;
			this.eNetEventCache = null;
			return eNetEvent;
		}
Beispiel #8
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;
                    }

                    // 这是accept peer
                    if (this.uSocketManager.ContainsKey(IntPtr.Zero))
                    {
                        USocket uSocket = this.uSocketManager[IntPtr.Zero];
                        uSocket.OnConnected(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:
                {
                    // 如果链接还在缓存中,则删除
                    if (this.connQueue.Remove(eNetEvent.Peer))
                    {
                        break;
                    }

                    // 链接已经被应用层接收
                    USocket uSocket = this.uSocketManager[eNetEvent.Peer];
                    this.uSocketManager.Remove(eNetEvent.Peer);

                    // 等待的task将抛出异常
                    if (uSocket.Connected != null)
                    {
                        uSocket.OnConnected(eNetEvent);
                        break;
                    }
                    if (uSocket.Received != null)
                    {
                        uSocket.OnReceived(eNetEvent);
                        break;
                    }
                    if (uSocket.Disconnect != null)
                    {
                        uSocket.OnDisconnect(eNetEvent);
                        break;
                    }
                    break;
                }
                }
            }
        }
Beispiel #9
0
		internal void OnDisconnect(ENetEvent eNetEvent)
		{
			if (this.Disconnect == null)
			{
				return;
			}

			Action<ENetEvent> localDisconnect = this.Disconnect;
			this.Disconnect = null;
			localDisconnect(eNetEvent);
		}
Beispiel #10
0
		internal void OnReceived(ENetEvent eNetEvent)
		{
			// 如果应用层还未调用readasync则将包放到缓存队列
			if (this.Received == null)
			{
				using (UPacket packet = new UPacket(eNetEvent.Packet))
				{
					byte[] bytes = packet.Bytes;
					this.recvQueue.Enqueue(bytes);
				}
			}
			else
			{
				Action<ENetEvent> localReceived = this.Received;
				this.Received = null;
				// 此调用将让await ReadAsync返回,所以null必须在此之前设置
				localReceived(eNetEvent);
			}
		}
Beispiel #11
0
		internal void OnConnected(ENetEvent eNetEvent)
		{
			if (this.Connected == null)
			{
				return;
			}
			Action<ENetEvent> localConnected = this.Connected;
			this.Connected = null;
			// 此调用将让await ConnectAsync返回,所以null必须在此之前设置
			localConnected(eNetEvent);
		}