CreateRawPacket() public static méthode

public static CreateRawPacket ( PacketProperty property, NetDataWriter dataWriter ) : byte[]
property PacketProperty
dataWriter LiteNetLib.Utils.NetDataWriter
Résultat byte[]
        public void NatIntroduce(
            NetEndPoint hostInternal,
            NetEndPoint hostExternal,
            NetEndPoint clientInternal,
            NetEndPoint clientExternal,
            string additionalInfo)
        {
            NetDataWriter dw = new NetDataWriter();

            //First packet (server)
            //send to client
            dw.Put(ClientByte);
            dw.Put(hostInternal);
            dw.Put(hostExternal);
            dw.Put(additionalInfo, MaxTokenLength);

            _socket.SendTo(NetPacket.CreateRawPacket(PacketProperty.NatIntroduction, dw), clientExternal);

            //Second packet (client)
            //send to server
            dw.Reset();
            dw.Put(HostByte);
            dw.Put(clientInternal);
            dw.Put(clientExternal);
            dw.Put(additionalInfo, MaxTokenLength);

            _socket.SendTo(NetPacket.CreateRawPacket(PacketProperty.NatIntroduction, dw), hostExternal);
        }
Exemple #2
0
        private void CloseConnection(bool force, string info)
        {
            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);
                }

                //Close threads and socket close
                base.Stop();

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

                //Send event to Listener
                var netEvent = CreateEvent(NetEventType.Disconnect);
                netEvent.AdditionalInfo = info;
                EnqueueEvent(netEvent);
            }
        }
Exemple #3
0
        public bool SendDiscoveryResponse(byte[] data, int start, int length, NetEndPoint remoteEndPoint)
        {
            if (!IsRunning)
            {
                return(false);
            }
            var packet = NetPacket.CreateRawPacket(PacketProperty.DiscoveryResponse, data, start, length);

            return(SendRaw(packet, remoteEndPoint));
        }
Exemple #4
0
        public bool SendDiscoveryRequest(byte[] data, int start, int length, int port)
        {
            if (!IsRunning)
            {
                return(false);
            }
            var packet = NetPacket.CreateRawPacket(PacketProperty.DiscoveryRequest, data, start, length);

            return(_socket.SendBroadcast(packet, 0, packet.Length, port));
        }
Exemple #5
0
        /// <summary>
        /// Send message without connection
        /// </summary>
        /// <param name="message">Raw data</param>
        /// <param name="start">data start</param>
        /// <param name="length">data length</param>
        /// <param name="remoteEndPoint">Packet destination</param>
        /// <returns>Operation result</returns>
        public bool SendUnconnectedMessage(byte[] message, int start, int length, NetEndPoint remoteEndPoint)
        {
            if (!IsRunning)
            {
                return(false);
            }
            var packet = NetPacket.CreateRawPacket(PacketProperty.UnconnectedMessage, message, start, length);

            return(SendRaw(packet, remoteEndPoint));
        }
Exemple #6
0
        private void SendConnectAccept()
        {
            //Reset connection timer
            _lastPacketReceivedStart = DateTime.UtcNow;

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

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

            //Send raw
            _peerListener.SendRaw(connectPacket, _remoteEndPoint);
        }
Exemple #7
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);
        }
Exemple #8
0
 private void CloseConnection(bool force)
 {
     if (_peer != null && !force)
     {
         //Send disconnect data
         var disconnectPacket = NetPacket.CreateRawPacket(PacketProperty.Disconnect, 8);
         FastBitConverter.GetBytes(disconnectPacket, 1, _connectId);
         _peer.SendRawData(disconnectPacket);
     }
     _peer            = null;
     _connected       = false;
     _connectTimer    = 0;
     _connectAttempts = 0;
     SocketClearPeers();
 }
Exemple #9
0
        private void SendConnectRequest()
        {
            //Get connect key bytes
            byte[] keyData = Encoding.UTF8.GetBytes(_connectKey);

            //Make initial packet
            var connectPacket = NetPacket.CreateRawPacket(PacketProperty.ConnectRequest, 8 + keyData.Length);

            //Add data
            FastBitConverter.GetBytes(connectPacket, 1, _connectId);
            Buffer.BlockCopy(keyData, 0, connectPacket, 9, keyData.Length);

            //Send raw
            _peer.SendRawData(connectPacket);
        }
Exemple #10
0
        private void SendConnectRequest()
        {
            //Get connect key bytes
            byte[] keyData = Encoding.UTF8.GetBytes(_peerListener.ConnectKey);

            //Make initial packet
            var connectPacket = NetPacket.CreateRawPacket(PacketProperty.ConnectRequest, 12 + keyData.Length);

            //Add data
            FastBitConverter.GetBytes(connectPacket, 1, NetConstants.ProtocolId);
            FastBitConverter.GetBytes(connectPacket, 5, _connectId);
            Buffer.BlockCopy(keyData, 0, connectPacket, 13, keyData.Length);

            //Send raw
            _peerListener.SendRaw(connectPacket, _remoteEndPoint);
        }
Exemple #11
0
        public void SendNatIntroduceRequest(NetEndPoint masterServerEndPoint, string additionalInfo)
        {
            if (!_netBase.IsRunning)
            {
                return;
            }

            //prepare outgoing data
            NetDataWriter dw            = new NetDataWriter();
            string        networkIp     = NetUtils.GetLocalIP();
            int           networkPort   = _netBase.LocalEndPoint.Port;
            NetEndPoint   localEndPoint = new NetEndPoint(networkIp, networkPort);

            dw.Put(localEndPoint);
            dw.Put(additionalInfo, MaxTokenLength);

            //prepare packet
            _socket.SendTo(NetPacket.CreateRawPacket(PacketProperty.NatIntroductionRequest, dw), masterServerEndPoint);
        }
Exemple #12
0
        /// <summary>
        /// Force closes connection and stop all threads.
        /// </summary>
        public void Stop()
        {
            //Send disconnect packets
            lock (_peers)
                foreach (NetPeer netPeer in _peers.Values)
                {
                    var disconnectPacket = NetPacket.CreateRawPacket(PacketProperty.Disconnect, 8);
                    FastBitConverter.GetBytes(disconnectPacket, 1, netPeer.ConnectId);
                    SendRaw(disconnectPacket, netPeer.EndPoint);
                }

            //Clear
            ClearPeers();

            //Stop
            if (IsRunning)
            {
                _logicThread.Stop();
                _socket.Close();
            }
        }
Exemple #13
0
        private void DisconnectPeer(
            NetPeer peer,
            DisconnectReason reason,
            int socketErrorCode,
            bool sendDisconnectPacket,
            byte[] data,
            int start,
            int count)
        {
            if (sendDisconnectPacket)
            {
                if (count + 8 >= peer.Mtu)
                {
                    //Drop additional data
                    data  = null;
                    count = 0;
                    NetUtils.DebugWriteError("Disconnect additional data size more than MTU - 8!");
                }

                var disconnectPacket = NetPacket.CreateRawPacket(PacketProperty.Disconnect, 8 + count);
                FastBitConverter.GetBytes(disconnectPacket, 1, peer.ConnectId);
                if (data != null)
                {
                    Buffer.BlockCopy(data, start, disconnectPacket, 9, count);
                }
                SendRaw(disconnectPacket, peer.EndPoint);
            }
            var netEvent = CreateEvent(NetEventType.Disconnect);

            netEvent.Peer             = peer;
            netEvent.AdditionalData   = socketErrorCode;
            netEvent.DisconnectReason = reason;
            EnqueueEvent(netEvent);
            lock (_peersToRemove)
            {
                _peersToRemove.Enqueue(peer.EndPoint);
            }
        }
Exemple #14
0
        private void HandleNatIntroduction(NetDataReader dr)
        {
            // read intro
            byte        hostByte       = dr.GetByte();
            NetEndPoint remoteInternal = dr.GetNetEndPoint();
            NetEndPoint remoteExternal = dr.GetNetEndPoint();
            string      token          = dr.GetString(MaxTokenLength);

            NetUtils.DebugWrite(ConsoleColor.Cyan, "[NAT] introduction received; we are designated " + (hostByte == HostByte ? "host" : "client"));
            NetDataWriter writer = new NetDataWriter();

            // send internal punch
            writer.Put(hostByte);
            writer.Put(token);
            _socket.SendTo(NetPacket.CreateRawPacket(PacketProperty.NatPunchMessage, writer), remoteInternal);
            NetUtils.DebugWrite(ConsoleColor.Cyan, "[NAT] punch sent to " + remoteInternal);

            // send external punch
            writer.Reset();
            writer.Put(hostByte);
            writer.Put(token);
            _socket.SendTo(NetPacket.CreateRawPacket(PacketProperty.NatPunchMessage, writer), remoteExternal);
            NetUtils.DebugWrite(ConsoleColor.Cyan, "[NAT] punch sent to " + remoteExternal);
        }
Exemple #15
0
        protected override void ReceiveFromSocket(byte[] reusableBuffer, int count, NetEndPoint remoteEndPoint)
        {
            //Parse packet
            //Peer null when P2P connection packets
            NetPacket packet = _peer == null ? new NetPacket() : _peer.GetPacketFromPool(init: false);

            if (!packet.FromBytes(reusableBuffer, count))
            {
                if (_peer != null)
                {
                    _peer.Recycle(packet);
                }
                return;
            }

            //Check P2P mode
            if (PeerToPeerMode && packet.Property == PacketProperty.ConnectRequest)
            {
                NetUtils.DebugWrite(ConsoleColor.Cyan, "[NC] Received peer connect request");

                string peerKey = Encoding.UTF8.GetString(packet.RawData, 9, packet.RawData.Length - 9);
                if (peerKey != _connectKey)
                {
                    NetUtils.DebugWrite(ConsoleColor.Cyan, "[NC] Peer connect reject. Invalid key: " + peerKey);
                    return;
                }

                NetUtils.DebugWrite(ConsoleColor.Cyan, "[NC] Peer connect accepting");

                //Make initial packet and put id from received packet
                var connectPacket = NetPacket.CreateRawPacket(PacketProperty.ConnectAccept, 8);
                Buffer.BlockCopy(packet.RawData, 1, connectPacket, 1, 8);

                //Check our peer and create
                if (_peer == null)
                {
                    //Create connect id for proper connection
                    Connect(remoteEndPoint);
                }

                //Send raw
                _peer.SendRawData(connectPacket);

                //clean incoming packet
                _peer.Recycle(packet);

                //We connected
                ProcessConnectAccept();

                return;
            }

            //Check peer
            if (_peer == null)
            {
                return;
            }

            //Check endpoint
            if (!_peer.EndPoint.Equals(remoteEndPoint))
            {
                NetUtils.DebugWriteForce(ConsoleColor.DarkCyan, "[NC] Bad EndPoint " + remoteEndPoint);
                return;
            }

            if (packet.Property == PacketProperty.Disconnect)
            {
                NetUtils.DebugWrite(ConsoleColor.Cyan, "[NC] Received disconnection");
                CloseConnection(true);
                var disconnectEvent = CreateEvent(NetEventType.Disconnect);
                disconnectEvent.AdditionalInfo = "Received disconnection from server";
                EnqueueEvent(disconnectEvent);
                return;
            }

            if (packet.Property == PacketProperty.ConnectAccept)
            {
                if (_connected)
                {
                    return;
                }

                //check connection id
                if (BitConverter.ToUInt64(packet.RawData, 1) != _connectId)
                {
                    return;
                }

                //connection things
                ProcessConnectAccept();
                return;
            }

            //Process income packet
            _peer.ProcessPacket(packet);
        }