Esempio n. 1
0
 /// <summary>
 /// Called when a a peer gets disconnected from us.
 /// </summary>
 private void OnPeerDisconnected(LiteNetLib.NetPeer peer, LiteNetLib.DisconnectInfo info)
 {
     if (peer == ArbiterPeer)
     {
         Console.Error.WriteLine(">>> Lost connection to arbiter! <<<");
     }
 }
Esempio n. 2
0
        private void CloseConnection(bool force, DisconnectReason reason, int socketErrorCode)
        {
            lock (_connectionCloseLock)
            {
                //Nothing to do
                if (!IsRunning)
                    return;

                //Send goodbye
                if (_peer != null && !force && _connected)
                {
                    //Send disconnect data
                    var disconnectPacket = NetPacket.CreateRawPacket(PacketProperty.Disconnect, 8);
                    FastBitConverter.GetBytes(disconnectPacket, 1, _connectId);
                    SendRaw(disconnectPacket, _peer.EndPoint);
                }

                //Clear data
                _peer = null;
                _connected = false;
                _connectTimer = 0;
                _connectAttempts = 0;
                SocketClearPeers();

                //Send event to Listener
                var netEvent = CreateEvent(NetEventType.Disconnect);
                netEvent.DisconnectReason = reason;
                netEvent.AdditionalData = socketErrorCode;
                EnqueueEvent(netEvent);
            }
        }
Esempio n. 3
0
        public ReliableChannel(NetPeer peer, bool ordered, int windowSize)
        {
            _resendDelay = peer.Handler.ReliableResendTime;
            _windowSize = windowSize;
            _peer = peer;
            _ordered = ordered;

            _outgoingPackets = new Queue<NetPacket>(_windowSize);

            _outgoingAcks = new bool[_windowSize];
            _pendingPackets = new PendingPacket[_windowSize];
            for (int i = 0; i < _pendingPackets.Length; i++)
            {
                _pendingPackets[i] = new PendingPacket();
            }

            if (_ordered)
                _receivedPackets = new NetPacket[_windowSize];
            else
                _earlyReceived = new bool[_windowSize];

            _localWindowStart = 0;
            _localSeqence = 0;
            _remoteSequence = 0;
            _remoteWindowStart = 0;
        }
Esempio n. 4
0
 private void RemovePeer(NetPeer peer)
 {
     lock (_peersToRemove)
     {
         _peersToRemove.Enqueue(peer.EndPoint);
     }
 }
Esempio n. 5
0
            public void OnPeerConnected(NetPeer peer)
            {
                Console.WriteLine("[Client] connected to: {0}:{1}", peer.EndPoint.Host, peer.EndPoint.Port);

                NetDataWriter dataWriter = new NetDataWriter();
                for (int i = 0; i < 5; i++)
                {
                    dataWriter.Reset();
                    dataWriter.Put(0);
                    dataWriter.Put(i);
                    peer.Send(dataWriter, SendOptions.ReliableUnordered);

                    dataWriter.Reset();
                    dataWriter.Put(1);
                    dataWriter.Put(i);
                    peer.Send(dataWriter, SendOptions.ReliableOrdered);

                    dataWriter.Reset();
                    dataWriter.Put(2);
                    dataWriter.Put(i);
                    peer.Send(dataWriter, SendOptions.Sequenced);

                    dataWriter.Reset();
                    dataWriter.Put(3);
                    dataWriter.Put(i);
                    peer.Send(dataWriter, SendOptions.Unreliable);
                }

                //And test fragment
                byte[] testData = new byte[13218];
                testData[0] = 192;
                testData[13217] = 31;
                peer.Send(testData, SendOptions.ReliableOrdered);
            }
Esempio n. 6
0
    public void CreatePlayer(LiteNetLib.NetPeer peer, string name, bool self)
    {
        LobbyPlayer p = Instantiate(playerPrefab, playersList, false);

        p.Init();
        p.SetPlayer(peer, name, self, Players.Count);

        Players.Add(p);
    }
Esempio n. 7
0
 public void OnPeerConnected(NetPeer peer)
 {
     Console.WriteLine("[Server] Peer connected: " + peer.EndPoint);
     var peers = Server.GetPeers();
     foreach (var netPeer in peers)
     {
         Console.WriteLine("ConnectedPeersList: id={0}, ep={1}", netPeer.Id, netPeer.EndPoint);
     }
 }
Esempio n. 8
0
    public void SetPlayer(LiteNetLib.NetPeer p, string name, bool b, int id)
    {
        peer               = p;
        playerName         = name;
        playerNameTxt.text = name;

        this.id = id;

        kickBtn.gameObject.SetActive(!b);
    }
Esempio n. 9
0
 /// <summary>
 /// Disconnect peer from server
 /// </summary>
 /// <param name="peer">peer to disconnect</param>
 public void DisconnectPeer(NetPeer peer)
 {
     if (peer != null && _peers.ContainsKey(peer.EndPoint))
     {
         peer.CreateAndSend(PacketProperty.Disconnect);
         var netEvent = CreateEvent(NetEventType.Disconnect);
         netEvent.Peer = peer;
         netEvent.DisconnectReason = DisconnectReason.DisconnectPeerCalled;
         EnqueueEvent(netEvent);
         RemovePeer(peer);
     }
 }
Esempio n. 10
0
    public void OnNetworkReceive(NetPeer peer, NetDataReader reader)
    {
        _newBallPosX = reader.GetFloat();

        var pos = _clientBall.transform.position;

        _oldBallPosX = pos.x;
        pos.x = _newBallPosX;

        _clientBall.transform.position = pos;

        _lerpTime = 0f;
    }
Esempio n. 11
0
 public void OnNetworkReceive(NetPeer peer, NetDataReader reader)
 {
     if (reader.AvailableBytes == 13218)
     {
         Console.WriteLine("[{0}] TestFrag: {1}, {2}", peer.Handler.LocalEndPoint.Port, reader.Data[0], reader.Data[13217]);
     }
     else
     {
         int type = reader.GetInt();
         int num = reader.GetInt();
         _messagesReceivedCount++;
         Console.WriteLine("[{0}] CNT: {1}, TYPE: {2}, NUM: {3}", peer.Handler.LocalEndPoint.Port, _messagesReceivedCount, type, num);
     }
 }
Esempio n. 12
0
    public LobbyPlayer GetPeer(LiteNetLib.NetPeer peer)
    {
        LobbyPlayer player = null;

        for (int i = 0; i < Players.Count; i++)
        {
            LobbyPlayer lp = Players[i];

            if (lp.peer == peer)
            {
                player = lp;
            }
        }

        return(player);
    }
Esempio n. 13
0
        /// <summary>
        /// Get copy of current connected peers
        /// </summary>
        /// <returns>Array with connected peers</returns>
        public NetPeer[] GetPeers()
        {
            NetPeer[] peers;
            int num = 0;

            lock (_peers)
            {
                peers = new NetPeer[_peers.Count];
                foreach (NetPeer netPeer in _peers.Values)
                {
                    peers[num++] = netPeer;
                }
            }

            return peers;
        }
Esempio n. 14
0
        private void Listener_NetworkReceiveEvent(LiteNetLib.NetPeer peer, NetDataReader reader, DeliveryMethod deliveryMethod)
        {
            _packetProcessor.ReadAllPackets(reader, peer);

            // check if we even know this peer
            var key = peer.EndPoint.ToString();

            if (_knownPlayers.ContainsKey(key))
            {
            }
            else
            {
                Debug.Assert(false, "The Connected Peer was Unknown to us! (Did not exist in KnownPlayer list)");
                // TODO: should assert or at least log this properly for future debugging
                Console.WriteLine($"Unknown Client connected as:{key}!");
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Called when a a peer connects to us; sends a `NodeOnline` message, but only to the arbiter.
        /// </summary>
        private void OnPeerConnected(LiteNetLib.NetPeer peer)
        {
            if (peer == ArbiterPeer)
            {
                // TODO: Connect to http://icanhazip.org or similar to get the external IP instead?
                var apiUri          = new Uri(this.ApiUrl);
                var externalAddress = IPAddress.Parse(apiUri.Host);

                // WARNING: Ensure that ApiUrl is visible from outside - otherwise, all requests will fail to be forwarded to this node!
                var currentConfig = new SShared.Messages.NodeConfig()
                {
                    BusAddress = externalAddress,
                    BusPort    = this.LocalBusPort,
                    ApiUrl     = this.ApiUrl,
                    Path       = QuadTreeNode.Path(),
                    Bounds     = QuadTreeNode.Bounds,
                };
                this.Bus.SendMessage(currentConfig, peer);
            }
        }
Esempio n. 16
0
 public void OnPeerDisconnected(NetPeer peer, DisconnectReason reason, int socketErrorCode)
 {
     Debug.Log("[CLIENT] We disconnected because " + reason);
 }
Esempio n. 17
0
 void INetEventListener.OnNetworkReceive(NetPeer peer, NetDataReader reader)
 {
     if (NetworkReceiveEvent != null)
         NetworkReceiveEvent(peer, reader);
 }
Esempio n. 18
0
 /// <summary>
 /// Disconnect peer from server and send additional data (Size must be less or equal MTU - 8)
 /// </summary>
 /// <param name="peer">peer to disconnect</param>
 /// <param name="data">additional data</param>
 public void DisconnectPeer(NetPeer peer, byte[] data)
 {
     DisconnectPeer(peer, data, 0, data.Length);
 }
Esempio n. 19
0
 public void SendToClients(byte[] data, int start, int length, SendOptions options, NetPeer excludePeer)
 {
     lock (_peers)
     foreach (NetPeer netPeer in _peers.Values)
     {
         if (netPeer != excludePeer)
         {
             netPeer.Send(data, start, length, options);
         }
     }
 }
Esempio n. 20
0
        private void SendConnectAccept(NetPeer peer, ulong id)
        {
            //Reset connection timer
            peer.StartConnectionTimer();

            //Make initial packet
            var connectPacket = NetPacket.CreateRawPacket(PacketProperty.ConnectAccept, 8);

            //Add data
            FastBitConverter.GetBytes(connectPacket, 1, id);

            //Send raw
            SendRaw(connectPacket, peer.EndPoint);
        }
Esempio n. 21
0
 /// <summary>
 /// Disconnect peer from server and send additional data (Size must be less or equal MTU - 8)
 /// </summary>
 /// <param name="peer">peer to disconnect</param>
 /// <param name="writer">additional data</param>
 public void DisconnectPeer(NetPeer peer, NetDataWriter writer)
 {
     DisconnectPeer(peer, writer.Data, 0, writer.Length);
 }
Esempio n. 22
0
 public void OnPeerDisconnected(NetPeer peer, DisconnectReason disconnectReason, int socketErrorCode)
 {
     Console.WriteLine("[Client] disconnected: " + disconnectReason);
 }
Esempio n. 23
0
 internal void ConnectionLatencyUpdated(NetPeer fromPeer, int latency)
 {
     var evt = CreateEvent(NetEventType.ConnectionLatencyUpdated);
     evt.Peer = fromPeer;
     evt.AdditionalData = latency;
     EnqueueEvent(evt);
 }
Esempio n. 24
0
 protected NetPeer CreatePeer(NetEndPoint remoteEndPoint)
 {
     var peer = new NetPeer(this, remoteEndPoint);
     peer.PingInterval = PingInterval;
     return peer;
 }
Esempio n. 25
0
 /// <summary>
 /// Send data to all connected peers
 /// </summary>
 /// <param name="data">Data</param>
 /// <param name="options">Send options (reliable, unreliable, etc.)</param>
 /// <param name="excludePeer">Excluded peer</param>
 public void SendToAll(byte[] data, DeliveryMethod options, NetPeer excludePeer)
 {
     SendToAll(data, 0, data.Length, options, excludePeer);
 }
Esempio n. 26
0
 void INetEventListener.OnNetworkLatencyUpdate(NetPeer peer, int latency)
 {
     if (NetworkLatencyUpdateEvent != null)
         NetworkLatencyUpdateEvent(peer, latency);
 }
Esempio n. 27
0
 /// <summary>
 /// Send data to all connected peers
 /// </summary>
 /// <param name="writer">DataWriter with data</param>
 /// <param name="options">Send options (reliable, unreliable, etc.)</param>
 /// <param name="excludePeer">Excluded peer</param>
 public void SendToAll(NetDataWriter writer, DeliveryMethod options, NetPeer excludePeer)
 {
     SendToAll(writer.Data, 0, writer.Length, options, excludePeer);
 }
Esempio n. 28
0
        public void Connect(NetEndPoint target)
        {
            if (!IsRunning)
            {
                throw new Exception("Client is not running");
            }
            if (_peer != null)
            {
                //Already connected
                return;
            }

            //Create connect id for proper connection
            _connectId = (ulong)DateTime.UtcNow.Ticks;
            NetUtils.DebugWrite(ConsoleColor.Cyan, "[CC] ConnectId: {0}", _connectId);

            //Create reliable connection
            _peer = CreatePeer(target);
            _peer.DebugTextColor = ConsoleColor.Yellow;

            //Create connection packet and send
            SendConnectRequest();

            _connectAttempts = 0;
        }
Esempio n. 29
0
 public void OnNetworkLatencyUpdate(NetPeer peer, int latency)
 {
     
 }
Esempio n. 30
0
 public SequencedChannel(NetPeer peer)
 {
     _outgoingPackets = new Queue<NetPacket>();
     _peer = peer;
 }
Esempio n. 31
0
            public void OnNetworkReceive(NetPeer peer, NetDataReader reader)
            {
                //echo
                peer.Send(reader.Data, SendOptions.ReliableUnordered);

                //fragment log
                if (reader.AvailableBytes == 13218)
                {
                    Console.WriteLine("[Server] TestFrag: {0}, {1}", reader.Data[0], reader.Data[13217]);
                }
            }
Esempio n. 32
0
 public void SendToClients(NetDataWriter writer, SendOptions options, NetPeer excludePeer)
 {
     lock (_peers)
     foreach (NetPeer netPeer in _peers.Values)
     {
         if (netPeer != excludePeer)
         {
             netPeer.Send(writer, options);
         }
     }
 }
Esempio n. 33
0
 public void OnPeerConnected(NetPeer peer)
 {
     Debug.Log("[CLIENT] We connected to " + peer.EndPoint);
 }
Esempio n. 34
0
 void INetEventListener.OnPeerConnected(NetPeer peer)
 {
     if (PeerConnectedEvent != null)
         PeerConnectedEvent(peer);
 }
Esempio n. 35
0
            public void OnNetworkReceive(NetPeer peer, NetDataReader reader)
            {

            }
Esempio n. 36
0
 /// <summary>
 /// Disconnect peer from server
 /// </summary>
 /// <param name="peer">peer to disconnect</param>
 public void DisconnectPeer(NetPeer peer)
 {
     DisconnectPeer(peer, null, 0, 0);
 }
Esempio n. 37
0
 void INetEventListener.OnPeerDisconnected(NetPeer peer, DisconnectReason disconnectReason, int socketErrorCode)
 {
     if (PeerDisconnectedEvent != null)
         PeerDisconnectedEvent(peer, disconnectReason, socketErrorCode);
 }
Esempio n. 38
0
 public void OnPeerConnected(NetPeer peer)
 {
     Console.WriteLine("[Client {0}] connected to: {1}:{2}", Client.LocalEndPoint.Port, peer.EndPoint.Host, peer.EndPoint.Port);
 }
Esempio n. 39
0
 /// <summary>
 /// Send data to all connected peers
 /// </summary>
 /// <param name="data">Data</param>
 /// <param name="start">Start of data</param>
 /// <param name="length">Length of data</param>
 /// <param name="options">Send options (reliable, unreliable, etc.)</param>
 /// <param name="excludePeer">Excluded peer</param>
 public void SendToAll(byte[] data, int start, int length, DeliveryMethod options, NetPeer excludePeer)
 {
     lock (_peers)
     {
         for (int i = 0; i < _peers.Count; i++)
         {
             var netPeer = _peers[i];
             if (netPeer != excludePeer)
             {
                 netPeer.Send(data, start, length, options);
             }
         }
     }
 }
Esempio n. 40
0
 public void OnPeerConnected(NetPeer peer)
 {
     Debug.Log("[SERVER] We have new peer " + peer.EndPoint);
     _ourPeer = peer;
 }
Esempio n. 41
0
 public void OnPeerDisconnected(NetPeer peer, DisconnectReason reason, int socketErrorCode)
 {
     Debug.Log("[SERVER] peer disconnected " + peer.EndPoint + ", info: " + reason);
     if (peer == _ourPeer)
         _ourPeer = null;
 }
Esempio n. 42
0
 public void OnPeerDisconnected(NetPeer peer, DisconnectReason disconnectReason, int socketErrorCode)
 {
     Console.WriteLine("[Server] Peer disconnected: " + peer.EndPoint + ", reason: " + disconnectReason);
 }
Esempio n. 43
0
 /// <summary>
 /// Immediately disconnect peer from server without additional data
 /// </summary>
 /// <param name="peer">peer to disconnect</param>
 public void DisconnectPeerForce(NetPeer peer)
 {
     DisconnectPeer(peer, DisconnectReason.DisconnectPeerCalled, 0, true, null, 0, 0);
 }