Ejemplo n.º 1
0
        public Task <Peer> ConnectAsync(
            Address address, uint channelLimit = NativeMethods.ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, uint data = 0)
        {
            CheckChannelLimit(channelLimit);

            var         tcs           = new TaskCompletionSource <Peer>();
            ENetAddress nativeAddress = address.Struct;
            IntPtr      peerPtr       = NativeMethods.enet_host_connect(this.host, ref nativeAddress, channelLimit, data);

            if (peerPtr == IntPtr.Zero)
            {
                throw new ENetException(0, "Host connect call failed.");
            }
            var peer = new Peer(peerPtr);

            this.PeersManager.Add(peerPtr, peer);
            peer.PeerEvent.Connected += e => tcs.TrySetResult(peer);
            return(tcs.Task);
        }