Esempio n. 1
0
        public Task <byte[]> RecvAsync()
        {
            var tcs = new TaskCompletionSource <byte[]>();

            // 如果有缓存的包,从缓存中取
            if (this.recvQueue.Count > 0)
            {
                byte[] bytes = this.recvQueue.Dequeue();
                tcs.TrySetResult(bytes);
            }
            // 没有缓存封包,设置回调等待
            else
            {
                this.Received = eEvent =>
                {
                    if (eEvent.Type == EventType.Disconnect)
                    {
                        tcs.TrySetException(new UException("socket disconnected in receive"));
                    }

                    using (UPacket packet = new UPacket(eEvent.Packet))
                    {
                        byte[] bytes = packet.Bytes;
                        tcs.TrySetResult(bytes);
                    }
                };
            }
            return(tcs.Task);
        }
Esempio n. 2
0
        public void SendAsync(byte[] data, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
        {
            UPacket packet = new UPacket(data, flags);

            NativeMethods.ENetPeerSend(this.peerPtr, channelID, packet.PacketPtr);
            // enet_peer_send函数会自动删除packet,设置为0,防止Dispose或者析构函数再次删除
            packet.PacketPtr = IntPtr.Zero;
        }
Esempio n. 3
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);
     }
 }
Esempio n. 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);
			}
		}
Esempio n. 5
0
		public Task<byte[]> RecvAsync()
		{
			var tcs = new TaskCompletionSource<byte[]>();

			// 如果有缓存的包,从缓存中取
			if (this.recvQueue.Count > 0)
			{
				byte[] bytes = this.recvQueue.Dequeue();
				tcs.TrySetResult(bytes);
			}
			// 没有缓存封包,设置回调等待
			else
			{
				this.Received = eEvent =>
				{
					if (eEvent.Type == EventType.Disconnect)
					{
						tcs.TrySetException(new UException("socket disconnected in receive"));
					}

					using (UPacket packet = new UPacket(eEvent.Packet))
					{
						byte[] bytes = packet.Bytes;
						tcs.TrySetResult(bytes);
					}
				};
			}
			return tcs.Task;
		}
Esempio n. 6
0
		public void SendAsync(byte[] data, byte channelID = 0, PacketFlags flags = PacketFlags.Reliable)
		{
			UPacket packet = new UPacket(data, flags);
			NativeMethods.ENetPeerSend(this.peerPtr, channelID, packet.PacketPtr);
			// enet_peer_send函数会自动删除packet,设置为0,防止Dispose或者析构函数再次删除
			packet.PacketPtr = IntPtr.Zero;
		}