Esempio n. 1
0
 private void OnMessageReceived(NeutronStream.IReader reader, bool isServer, bool isMine, NeutronPlayer player, Neutron instance)
 {
     if (!isServer)
     {
         ProcessMessage(player, reader.ReadString());
     }
 }
Esempio n. 2
0
 private bool OnCreatePlayer(NeutronStream.IReader reader, bool isServer, bool isMine, NeutronPlayer player, Neutron instance)
 {
     if (instance.EndPlayer(reader, out var pos, out var rot))
     {
         NeutronSchedule.ScheduleTask(() =>
         {
             Neutron.NetworkSpawn(isServer, false, player, _player, pos, rot, instance);
         });
     }
Esempio n. 3
0
        public void SyncSnapshot(NeutronStream.IReader reader, NeutronPlayer player)
        {
            var position  = _syncPosition ? reader.ReadVector3() : Vector3.zero;
            var rotation  = _syncRotation ? _compressQuaternion ? reader.ReadCompressedQuaternion(_floatMultiplicationPrecision) : reader.ReadQuaternion() : Quaternion.identity;
            var scale     = _syncScale ? reader.ReadVector3() : Vector3.zero;
            var timestamp = reader.ReadDouble();

            if (_syncPosition)
            {
                OnTeleport(position);
            }

            NetworkTransformSnapshot snapshot = new NetworkTransformSnapshot(
                timestamp,
                LocalTime,
                position, rotation, scale
                );

            lock (_bufferLock)
                SnapshotInterpolation.InsertIfNewEnough(snapshot, _buffer);
        }
Esempio n. 4
0
        public void SyncRigidbody(NeutronStream.IReader reader, NeutronPlayer player)
        {
            if (DoNotPerformTheOperationOnTheServer)
            {
                if (!_sync)
                {
                    _sync = true;
                }

                var position        = reader.ReadVector3();
                var velocity        = reader.ReadVector3();
                var angularVelocity = reader.ReadVector3();
                var localTime       = reader.ReadDouble();

                if (localTime > _lastTime)
                {
                    _position        = position;
                    _velocity        = velocity;
                    _angularVelocity = angularVelocity;
                }

                _lastTime = localTime;
            }
        }
Esempio n. 5
0
 public bool RpcStress(NeutronStream.IReader reader, NeutronPlayer player)
 {
     Debug.LogError($"{IsServer}: {reader.ReadInt()}");
     return(true);
 }
Esempio n. 6
0
 private void RpcTest(NeutronStream.IReader reader, NeutronPlayer player)
 {
     LogHelper.Error("??????");
 }
Esempio n. 7
0
 private void Mov(NeutronStream.IReader reader, NeutronPlayer player)
 {
 }
Esempio n. 8
0
        private void RunPacket(NeutronPacket neutronPacket)
        {
            byte[]        pBuffer  = neutronPacket.Buffer;       //* Get the packet buffer.
            bool          isServer = neutronPacket.IsServerSide; //* Get the packet is server side.
            Protocol      protocol = neutronPacket.Protocol;     //* Get the packet protocol.
            NeutronPlayer owner    = neutronPacket.Owner;        //* Get the packet owner.
            NeutronPlayer sender   = neutronPacket.Sender;       //* Get the packet sender.

            using (NeutronStream stream = Neutron.PooledNetworkStreams.Pull())
            {
                NeutronStream.IReader reader = stream.Reader;
                reader.SetBuffer(pBuffer);                      //* Set the buffer.
                Packet outPacket = (Packet)reader.ReadPacket(); //* Read the packet.
                if (OnReceivePacket(outPacket) || isServer)
                {
                    switch (outPacket)
                    {
                    case Packet.TcpKeepAlive:
                    {
                        using (NeutronStream tcpStream = Neutron.PooledNetworkStreams.Pull())
                        {
                            NeutronStream.IWriter writer = tcpStream.Writer;
                            writer.WritePacket((byte)Packet.TcpKeepAlive);
                            owner.Write(writer);
                        }
                    }
                    break;

                    case Packet.Handshake:
                    {
                        var appId          = reader.ReadString();                       //* Read the app id.
                        var time           = reader.ReadDouble();                       //* Read the time.
                        var authentication = reader.ReadWithInteger <Authentication>(); //* Read the authentication.
                        if (appId.Decrypt(out appId))
                        {
                            //* Decrypt the app id.
                            if (Helper.GetSettings().GlobalSettings.AppId == appId)
                            {
                                HandshakeHandler(owner, time, authentication);         //* Handshake the player.
                            }
                            else
                            {
                                owner.Error(Packet.Handshake, "Update your game version, it does not match the current server version.");
                                Task.Run(async() =>
                                    {
                                        await Task.Delay(150);     //* Submit the disconnect after receiving the error message.
                                        DisconnectHandler(owner);  //* Disconnect the player.
                                    });
                            }
                        }
                        else if (!LogHelper.Error("Failed to verify handshake!"))
                        {
                            DisconnectHandler(owner);         //* Disconnect the player.
                        }
                    }
                    break;

                    case Packet.Nickname:
                    {
                        var nickname = reader.ReadString();         //* Read the nickname.
                        SetNicknameHandler(owner, nickname);
                    }
                    break;

                    case Packet.Chat:
                    {
                        var matchmakingTo = default(MatchmakingTo);        //* Create the matchmaking to.
                        var viewId        = default(int);                  //* Read the view id.
                        var chatPacket    = (ChatMode)reader.ReadPacket(); //* Read the chat mode.
                        switch (chatPacket)
                        {
                        case ChatMode.Global:
                            matchmakingTo = (MatchmakingTo)reader.ReadPacket();             //* Read the matchmaking to.
                            break;

                        case ChatMode.Private:
                            viewId = reader.ReadInt();             //* Read the view id.
                            break;
                        }
                        string message = reader.ReadString();
                        ChatHandler(owner, chatPacket, matchmakingTo, viewId, message);
                    }
                    break;

                    case Packet.iRPC:
                    {
                        var   registerType = (RegisterMode)reader.ReadPacket();                                                 //* Read the register mode.
                        var   targetTo     = (TargetTo)reader.ReadPacket();                                                     //* Read the target to.
                        var   cache        = (CacheMode)reader.ReadPacket();                                                    //* Read the cache mode.
                        short viewId       = reader.ReadShort();                                                                //* Read the view id.
                        var   rpcId        = reader.ReadByte();                                                                 //* Read the rpc id.
                        var   instanceId   = reader.ReadByte();                                                                 //* Read the instance id.
                        var   buffer       = reader.ReadNext();                                                                 //* Read the buffer.
                        iRPCHandler(owner, sender, viewId, rpcId, instanceId, buffer, registerType, targetTo, cache, protocol); //* Handle the iRPC.
                    }
                    break;

                    case Packet.gRPC:
                    {
                        var id     = reader.ReadByte();                   //* Read the id.
                        var buffer = reader.ReadNext();                   //* Read the buffer.
                        gRPCHandler(owner, sender, id, buffer, protocol); //* Handle the gRPC.
                    }
                    break;

                    case Packet.GetChannels:
                    {
                        GetChannelsHandler(owner);         //* Handle the get channels.
                    }
                    break;

                    case Packet.JoinChannel:
                    {
                        var channelId = reader.ReadInt();         //* Read the channel id.
                        JoinChannelHandler(owner, channelId);     //* Handle the join channel.
                    }
                    break;

                    case Packet.GetCache:
                    {
                        var cachedPacket = (CachedPacket)reader.ReadPacket(); //* Read the cached packet.
                        var Id           = reader.ReadByte();                 //* Read the id.
                        var includeMe    = reader.ReadBool();                 //* Send packets to me?
                        GetCacheHandler(owner, cachedPacket, Id, includeMe);  //* Handle the get cache.
                    }
                    break;

                    case Packet.CreateRoom:
                    {
                        var password = reader.ReadString();                    //* Read the password.
                        var room     = reader.ReadWithInteger <NeutronRoom>(); //* Read the room.
                        CreateRoomHandler(owner, room, password);              //* Handle the create room.
                    }
                    break;

                    case Packet.GetRooms:
                    {
                        GetRoomsHandler(owner);         //* Handle the get rooms.
                    }
                    break;

                    case Packet.JoinRoom:
                    {
                        var roomId   = reader.ReadInt();          //* Read the room id.
                        var password = reader.ReadString();       //* Read the password.
                        JoinRoomHandler(owner, roomId, password); //* Handle the join room.
                    }
                    break;

                    case Packet.Leave:
                    {
                        var packet = (MatchmakingMode)reader.ReadPacket();         //* Read the matchmaking mode.
                        if (packet == MatchmakingMode.Room)
                        {
                            LeaveRoomHandler(owner);         //* Handle the leave room.
                        }
                        else if (packet == MatchmakingMode.Channel)
                        {
                            LeaveChannelHandler(owner);         //* Handle the leave channel.
                        }
                    }
                    break;

                    case Packet.Destroy:
                    {
                        DestroyPlayerHandler(owner);         //* Handle the destroy player.
                    }
                    break;

                    case Packet.SetPlayerProperties:
                    {
                        var properties = reader.ReadString();          //* Read the properties.
                        SetPlayerPropertiesHandler(owner, properties); //* Handle the set player properties.
                    }
                    break;

                    case Packet.SetRoomProperties:
                    {
                        var properties = reader.ReadString();         //* Read the properties.
                        SetRoomPropertiesHandler(owner, properties);  //* Handle the set room properties.
                    }
                    break;

                    case Packet.UdpKeepAlive:
                    {
                        var time = reader.ReadDouble();   //* Read the time.
                        PingHandler(owner, time);         //* Handle the ping.
                    }
                    break;

                    case Packet.CustomPacket:
                    {
                        // var isMine;
                        // TargetTo targetTo = default(TargetTo);
                        // MatchmakingTo matchmakingTo = default(MatchmakingTo);
                        // int viewId = reader.ReadInt();
                        // byte packet = reader.ReadPacket();
                        // if ((isMine = PlayerHelper.IsMine(owner, viewId)))
                        // {
                        //     targetTo = (TargetTo)reader.ReadPacket();
                        //     matchmakingTo = (MatchmakingTo)reader.ReadPacket();
                        // }
                        // byte[] buffer = reader.ReadWithInteger();
                        // CustomPacketHandler(owner, isMine, viewId, buffer, packet, targetTo, matchmakingTo, protocol);
                    }
                    break;

                    case Packet.AutoSync:
                    {
                        var registerMode = (RegisterMode)reader.ReadPacket();                           //* Read the register mode.
                        var viewId       = reader.ReadShort();                                          //* Read the view id.
                        var instanceId   = reader.ReadByte();                                           //* Read the instance id.
                        var parameters   = reader.ReadNext();                                           //* Read the parameters.
                        OnAutoSyncHandler(neutronPacket, viewId, instanceId, parameters, registerMode); //* Handle the auto sync.
                    }
                    break;

                    case Packet.Synchronize:
                    {
                        SynchronizeHandler(owner, protocol);         //* Handle the synchronize.
                    }
                    break;
                    }
                }
                else
                {
                    LogHelper.Error("Client is not allowed to run this packet.");
                }
            }
        }