示例#1
0
        /// <inheritdoc/>
        public IPeerManager Connect(IPEndPoint endPoint)
        {
            var peerManager = new LiteNetLibPeerManager(netServer.Connect(endPoint, ApplicationKey));

            peers.Add(peerManager.PeerEndPoint, peerManager);
            return(peerManager);
        }
示例#2
0
 /// <inheritdoc/>
 public void OnPeerConnected(NetPeer peer)
 {
     if (!peers.TryGetValue(peer.EndPoint, out var peerManager))
     {
         peerManager = new LiteNetLibPeerManager(peer);
     }
     PeerConnected?.Invoke(peerManager);
 }
示例#3
0
 /// <inheritdoc/>
 public IPeerManager Connect(IPEndPoint endPoint)
 {
     if (masterPeer != null)
     {
         throw new ArgumentException("Client can be connected only to a single peer.");
     }
     masterPeer = new LiteNetLibPeerManager(NetClient.Connect(endPoint, ApplicationKey));
     return(masterPeer);
 }
示例#4
0
        /// <inheritdoc/>
        public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
        {
            var disconnectedPeer = masterPeer;

            if (masterPeer.Peer == peer)
            {
                masterPeer = null;
            }
            PeerDisconnected?.Invoke(disconnectedPeer);
        }
示例#5
0
 /// <inheritdoc/>
 public void OnPeerConnected(NetPeer peer)
 {
     if (masterPeer == null)
     {
         masterPeer = new LiteNetLibPeerManager(peer);
     }
     else if (!Equals(masterPeer.PeerEndPoint, peer.EndPoint))
     {
         throw new ArgumentException("Client can be connected only to a single peer.");
     }
     PeerConnected?.Invoke(masterPeer);
 }
        /// <inheritdoc/>
        public IPeerManager Connect(IPEndPoint endPoint, string identifier)
        {
            var writer = new NetDataWriter();

            writer.Put(ApplicationKey);
            writer.Put(identifier);
            var peerManager = new LiteNetLibPeerManager(netServer.Connect(endPoint, writer));

            peers.Add(peerManager.PeerEndPoint, peerManager);
            Log.Info($"{GetType().Name} starts the connection to a peer with address '{endPoint.ToString()}'.");
            return(peerManager);
        }
示例#7
0
        /// <inheritdoc/>
        public void OnPeerConnected(NetPeer peer)
        {
            if (MasterPeer != null)
            {
                activeConnections.Remove(peer.EndPoint);
                return;
            }
            if (!activeConnections.TryGetValue(peer.EndPoint, out masterPeer))
            {
                masterPeer = new LiteNetLibPeerManager(peer);
                activeConnections.Add(peer.EndPoint, MasterPeer);
            }

            Log.Info($"{GetType().Name} has connected to the master peer '{MasterPeer.PeerEndPoint}'.");
            PeerConnected?.Invoke(MasterPeer);
        }
示例#8
0
        /// <inheritdoc/>
        public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
        {
            if (MasterPeer == null || MasterPeer.Peer != peer)
            {
                activeConnections.Remove(peer.EndPoint);
                if (activeConnections.Count == 0)
                {
                    DroppedAllConnections?.Invoke();
                }
                return;
            }
            var disconnectedPeer = MasterPeer;

            masterPeer = null;
            activeConnections.Remove(peer.EndPoint);
            PeerDisconnected?.Invoke(disconnectedPeer);
        }
示例#9
0
        /// <inheritdoc/>
        public void Broadcast(DistributedMessage distributedMessage)
        {
            var bytesStack = distributedMessage.Content;

            try
            {
                NetworkStatistics.ReportSentPackage(bytesStack.Count);
                NetClient.SendToAll(bytesStack.RawData, 0, bytesStack.Count,
                                    LiteNetLibPeerManager.GetDeliveryMethod(distributedMessage.Type));
            }
            catch (TooBigPacketException)
            {
                Log.Error($"Too large message to be sent: {bytesStack.Count}.");
            }

            distributedMessage.Release();
        }
示例#10
0
        /// <inheritdoc/>
        public IPeerManager Connect(IPEndPoint endPoint, string peerIdentifier)
        {
            if (activeConnections.ContainsKey(endPoint))
            {
                Log.Warning($"{GetType().Name} already got a connection active to the endpoint '{endPoint}'.");
                return(null);
            }

            var writer = new NetDataWriter();

            writer.Put(ApplicationKey);
            writer.Put(peerIdentifier);
            var peer = new LiteNetLibPeerManager(NetClient.Connect(endPoint, writer));

            activeConnections.Add(endPoint, peer);
            Log.Info(
                $"{GetType().Name} tries to connect with a peer at address '{endPoint}, current UTC time: {DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)}.");
            return(peer);
        }