示例#1
0
        protected bool ReadChunkHeader(DataStreamReader stream, ref DataStreamReader.Context ctx
                                       , out DataStreamReader chunk, out DataStreamReader.Context ctx2, out ushort targetPlayerId, out ushort senderPlayerId)
        {
            chunk          = default;
            ctx2           = default;
            targetPlayerId = 0;
            senderPlayerId = 0;

            int pos = stream.GetBytesRead(ref ctx);

            if (pos >= stream.Length)
            {
                return(false);
            }
            ushort dataLength = stream.ReadUShort(ref ctx);

            if (dataLength == 0)
            {
                return(false);
            }

            chunk          = stream.ReadChunk(ref ctx, dataLength);
            targetPlayerId = chunk.ReadUShort(ref ctx2);
            senderPlayerId = chunk.ReadUShort(ref ctx2);
            return(true);
        }
示例#2
0
        private static void OnRecievePacket(ushort senderPlayerId, byte type, DataStreamReader recievePacket, DataStreamReader.Context ctx)
        {
            //Debug.Log ("OnRecievePacket");
            switch ((BuiltInPacket.Type)type)
            {
            case BuiltInPacket.Type.ReserveNetId:
                RecieveReserveNetId(senderPlayerId, ref recievePacket, ref ctx);
                break;

            case BuiltInPacket.Type.ChangeAuthor:
                int    sceneHash = recievePacket.ReadInt(ref ctx);
                ushort netId     = recievePacket.ReadUShort(ref ctx);
                ushort author    = recievePacket.ReadUShort(ref ctx);
                RecieveChangeAuthor(sceneHash, netId, author);
                break;

            case BuiltInPacket.Type.SyncTransform:
                RecieveSyncTransform(senderPlayerId, ref recievePacket, ref ctx);
                break;

            case BuiltInPacket.Type.BehaviourRpc:
                RecieveBehaviourRpc(senderPlayerId, ref recievePacket, ref ctx);
                break;
            }
        }
示例#3
0
 protected bool ReadQosHeader(DataStreamReader stream, ref DataStreamReader.Context ctx, out QosType qosType, out ushort seqNum, out ushort ackNum)
 {
     if (!stream.IsCreated)
     {
         qosType = QosType.Empty;
         seqNum  = 0;
         ackNum  = 0;
         return(false);
     }
     qosType = (QosType)stream.ReadByte(ref ctx);
     seqNum  = stream.ReadUShort(ref ctx);
     ackNum  = stream.ReadUShort(ref ctx);
     return(true);
 }
    public override void DeserializeObject(ref DataStreamReader reader)
    {
        base.DeserializeObject(ref reader);

        PlayerID = reader.ReadInt();
        newHP    = reader.ReadUShort();
    }
    public static void RoomInfo(DataStreamReader stream)
    {
        stream.ReadUInt();

        Room cRoom = new Room()
        {
            possibleDirections = (Direction)stream.ReadByte(),
            treasureAmount     = stream.ReadUShort(),

            containsMonster = System.Convert.ToBoolean(stream.ReadByte()),
            containsExit    = System.Convert.ToBoolean(stream.ReadByte()),

            numberOfOtherPlayers = stream.ReadByte()
        };

        List <int> otherPlayerIds = new List <int>();

        for (int i = 0; i < cRoom.numberOfOtherPlayers; i++)
        {
            otherPlayerIds.Add(stream.ReadInt());
        }

        cRoom.otherPlayerIds = otherPlayerIds.ToArray();

        GameManager.Instance.StartCoroutine(LateInvokeRoom(cRoom));
    }
        protected override FileTransporter RecieveStart(int hash, DataStreamReader stream, ref DataStreamReader.Context ctx)
        {
            if (string.IsNullOrEmpty(SaveDirectory))
            {
                SaveDirectory = Application.persistentDataPath + "/RecieveFile";
                System.IO.Directory.CreateDirectory(SaveDirectory);
            }

            string fileName = stream.ReadString(ref ctx);
            int    fileSize = stream.ReadInt(ref ctx);
            ushort dataSize = stream.ReadUShort(ref ctx);

            string     path = SaveDirectory + "/" + hash + "_" + fileName;
            FileStream fs   = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);

            var transporter = new FileTransporter(hash, fileName, fs, 8092, 8092);

            unsafe
            {
                fixed(byte *data = transporter.buffer)
                {
                    stream.ReadBytes(ref ctx, data, dataSize);
                    transporter.bufferPos += dataSize;
                }
            }
            return(transporter);
        }
示例#7
0
        private static void RecieveReserveNetId(ushort senderPlayerId, ref DataStreamReader recievePacket, ref DataStreamReader.Context ctx)
        {
            if (GamePacketManager.IsLeader)
            {
                //HostではNetIDの整合性を確認
                ushort reserveNetId = (ushort)Instance.m_identityList.Count;
                Instance.m_identityList.Add(null);

                //Clientに通達する
                using (var packet = new DataStreamWriter(3, Allocator.Temp)) {
                    packet.Write((byte)BuiltInPacket.Type.ReserveNetId);
                    packet.Write(reserveNetId);
                    GamePacketManager.Send(senderPlayerId, packet, QosType.Reliable);
                }
            }
            else
            {
                //確認されたauthorの変更を反映
                ushort reserveNetId = recievePacket.ReadUShort(ref ctx);
                if (uncheckReserveNetIdCallbacks.Count > 0)
                {
                    uncheckReserveNetIdCallbacks[0] (reserveNetId);
                    uncheckReserveNetIdCallbacks.RemoveAt(0);
                }
                else
                {
                    Debug.LogError("uncheckReserveNetIdCallbacks is 0");
                }
            }
        }
    public static void ObtainTreasure(DataStreamReader stream)
    {
        stream.ReadUInt();

        ushort amountGained = stream.ReadUShort();

        ObtainTreasureReceived?.Invoke(amountGained);
    }
    public static void PlayerDefends(DataStreamReader stream)
    {
        stream.ReadUInt();

        int    playerId     = stream.ReadInt();
        ushort newCurrentHp = stream.ReadUShort();

        PlayerDefendsReceived?.Invoke(playerId, newCurrentHp);
    }
    public static void HitMonster(DataStreamReader stream)
    {
        stream.ReadUInt();

        int    playerId    = stream.ReadInt();
        ushort damageDealt = stream.ReadUShort();

        HitMonsterReceived?.Invoke(playerId, damageDealt);
    }
    public static void HitByMonster(DataStreamReader stream)
    {
        stream.ReadUInt();

        int    playerId     = stream.ReadInt();
        ushort newCurrentHp = stream.ReadUShort();

        HitByMonsterReceived?.Invoke(playerId, newCurrentHp);
    }
示例#12
0
    public void Deserialize(ref DataStreamReader reader)
    {
        numberOfScores = reader.ReadByte();

        highScores = new KeyValuePair <int, ushort> [numberOfScores];
        for (int i = 0; i < numberOfScores; i++)
        {
            highScores[i] = new KeyValuePair <int, ushort>(reader.ReadInt(), reader.ReadUShort());
        }
    }
示例#13
0
 public override void DeserializeObject(ref DataStreamReader reader)
 {
     base.DeserializeObject(ref reader);
     NumberOfScores = reader.ReadByte();
     for (int i = 0; i < NumberOfScores; i++)
     {
         PlayerID.Add(reader.ReadInt());
         HighScores.Add(reader.ReadUShort());
     }
 }
示例#14
0
        protected virtual void ProcessData(DataStreamReader reader, int connectionID)
        {
            DataStreamReader.Context readerCtx = default(DataStreamReader.Context);
            var packetID = (PacketsEnum)reader.ReadUShort(ref readerCtx);

            Debug.Log(packetID + " " + connectionID);
            var packet = Options.Packets[packetID];

            packet.FromStream(reader, ref readerCtx);
            InvokeHandler(packetID, packet, connectionID);
        }
    public override void DeserializeObject(ref DataStreamReader reader)
    {
        base.DeserializeObject(ref reader);

        NumberOfScores         = reader.ReadByte();
        PlayerIDHighscorePairs = new HighScorePair[NumberOfScores];
        for (int i = 0; i < NumberOfScores; i++)
        {
            PlayerIDHighscorePairs[i].playerID = reader.ReadInt();
            PlayerIDHighscorePairs[i].score    = reader.ReadUShort();
        }
    }
示例#16
0
    public void Deserialize(DataStreamReader reader, ref DataStreamReader.Context ctx)
    {
        var nameLength = reader.ReadUShort(ref ctx);

        GameDebug.Assert(nameLength <= NativeString64.MaxLength);
        MapName.LengthInBytes = nameLength;
        unsafe
        {
            fixed(byte *b = &MapName.buffer.byte0000)
            {
                reader.ReadBytes(ref ctx, b, MapName.LengthInBytes);
            }
        }
    }
示例#17
0
    public void Deserialize(ref DataStreamReader reader)
    {
        moveDirections       = (Direction)reader.ReadByte();
        treasureInRoom       = reader.ReadUShort();
        containsMonster      = Convert.ToBoolean(reader.ReadByte());
        containsExit         = Convert.ToBoolean(reader.ReadByte());
        numberOfOtherPlayers = reader.ReadByte();

        otherPlayerIDs = new int[numberOfOtherPlayers];
        for (int i = 0; i < numberOfOtherPlayers; i++)
        {
            otherPlayerIDs[i] = reader.ReadInt();
        }
    }
示例#18
0
    public void Deserialize(DataStreamReader reader, ref DataStreamReader.Context ctx)
    {
        var msgLength = reader.ReadUShort(ref ctx);

        GameDebug.Assert(msgLength <= NativeString64.MaxLength);
        Command.LengthInBytes = msgLength;
        unsafe
        {
            fixed(byte *b = &Command.buffer.byte0000)
            {
                reader.ReadBytes(ref ctx, b, Command.LengthInBytes);
            }
        }
    }
示例#19
0
        public override void DeserializeObject(ref DataStreamReader reader)
        {
            base.DeserializeObject(ref reader);
            MoveDirections       = reader.ReadByte();
            TreasureInRoom       = reader.ReadUShort();
            ContainsMonster      = reader.ReadByte();
            ContainsExit         = reader.ReadByte();
            NumberOfOtherPlayers = reader.ReadByte();
            OtherPlayerIDs.Clear();

            for (int i = 0; i < NumberOfOtherPlayers; i++)
            {
                OtherPlayerIDs.Add(reader.ReadInt());
            }
        }
    public static void EndGame(DataStreamReader stream)
    {
        stream.ReadUInt();

        byte numberOfScores = stream.ReadByte();

        List <System.Tuple <int, ushort> > idScorePairs = new List <System.Tuple <int, ushort> >();

        for (int i = 0; i < numberOfScores; i++)
        {
            var temp = new System.Tuple <int, ushort>(stream.ReadInt(), stream.ReadUShort());
        }

        EndGameReceived?.Invoke(idScorePairs);
    }
        public override void DeserializeObject(ref DataStreamReader reader)
        {
            base.DeserializeObject(ref reader);

            directions           = (Server.Game.Directions)reader.ReadByte();
            TreasureInRoom       = reader.ReadUShort();
            ContainsMonster      = reader.ReadByte() > 0;
            ContainsExit         = reader.ReadByte() > 0;
            NumberofOtherPlayers = reader.ReadByte();
            OtherPlayerIDs.Clear(); // to be sure :)
            for (int i = 0; i < NumberofOtherPlayers; i++)
            {
                OtherPlayerIDs.Add(reader.ReadInt());
            }
        }
示例#22
0
        public override void DeserializeObject(ref DataStreamReader reader)
        {
            base.DeserializeObject(ref reader);

            NumberOfScores = reader.ReadByte();
            scoreData.Clear();//to be sure
            for (int i = 0; i < NumberOfScores; i++)
            {
                scoreData.Add(new ScoreData {
                    playerID = reader.ReadInt(),
                    gold     = reader.ReadUShort()
                }
                              );
            }
        }
示例#23
0
        public override void DeserializeObject(ref DataStreamReader reader)
        {
            base.DeserializeObject(ref reader);
            MoveDirections       = reader.ReadByte();
            TreasureInRoom       = reader.ReadUShort();
            ContainsMonster      = reader.ReadByte();
            ContainsExit         = reader.ReadByte();
            NumberOfOtherPlayers = reader.ReadByte();

            OtherPlayerIDs = new int[NumberOfOtherPlayers];
            for (int i = 0; i < NumberOfOtherPlayers; i++)
            {
                OtherPlayerIDs[i] = reader.ReadInt();
            }
            //Debug.Log(PlayerName);
        }
示例#24
0
    public void Deserialize(DataStreamReader reader, ref DataStreamReader.Context ctx)
    {
        TeamId        = reader.ReadShort(ref ctx);
        CharacterType = reader.ReadInt(ref ctx);
        var nameLength = reader.ReadUShort(ref ctx);

        GameDebug.Assert(nameLength <= NativeString64.MaxLength);
        PlayerName.LengthInBytes = nameLength;
        unsafe
        {
            fixed(byte *b = &PlayerName.buffer.byte0000)
            {
                reader.ReadBytes(ref ctx, b, PlayerName.LengthInBytes);
            }
        }
    }
示例#25
0
        protected override void HandleData(ref DataStreamReader reader, int id)
        {
            var msgTypeId = reader.ReadUShort();
            var msgType   = MessageType.Unresolved;

            try {
                msgType = (MessageType)msgTypeId;
            }
            catch (InvalidCastException) {
                LogError($"Unresolved message type: {msgTypeId}");
            }

            switch (msgType)
            {
            case MessageType.None:
                KeepAlive(id);
                break;

            case MessageType.RingCreated:
                EnqueueReceived(Message.Read <RingCreatedMessage>(ref reader), id);
                break;

            case MessageType.RingReleased:
                EnqueueReceived(Message.Read <RingReleasedMessage>(ref reader), id);
                break;

            case MessageType.GameJoined:
            case MessageType.PlayerLeft:
            case MessageType.JoinRefused:
            case MessageType.StartGame:
            case MessageType.GameLost:
            case MessageType.GameWon:
            case MessageType.TargetSpawned:
                // Client messages, should not be received by server
                LogWarning($"Server received message intended for client. Message was: {msgType.ToString()}");
                break;

            case MessageType.Unresolved:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
示例#26
0
        private static void RecieveBehaviourRpc(ushort senderPlayerId, ref DataStreamReader recievePacket, ref DataStreamReader.Context ctx)
        {
            int    sceneHash = recievePacket.ReadInt(ref ctx);
            ushort netId     = recievePacket.ReadUShort(ref ctx);

            if (sceneHash == 0)
            {
                Instance.RecieveBehaviourRpcInGroup(senderPlayerId, netId, ref recievePacket, ref ctx);
            }
            else
            {
                RecordableSceneIdentity sceneIdentity;
                if (m_recordableSceneIdentitTable.TryGetValue(sceneHash, out sceneIdentity))
                {
                    sceneIdentity.RecieveBehaviourRpcInGroup(senderPlayerId, netId, ref recievePacket, ref ctx);
                }
                else
                {
                    Debug.LogError($"{sceneHash} is not found in recordableSceneIdentitTable");
                }
            }
        }
示例#27
0
 public override void DeserializeObject(ref DataStreamReader reader)
 {
     base.DeserializeObject(ref reader);
     StartHP = reader.ReadUShort();
 }
示例#28
0
        private void ReadDataEvent(NetworkConnection connection, DataStreamReader streamReader)
        {
            try
            {
                int senderActorNumber = connection.InternalId;
                int command           = streamReader.ReadInt();
                switch (command)
                {
                case Commands.AcknowledgeActorNumber:
                {
                    if (_acceptAllPlayers)
                    {
                        AcceptPlayer(senderActorNumber);
                    }
                    break;
                }

                case Commands.PlayerData:
                {
                    string playerData = streamReader.ReadManagedString();
                    PlayerDataReceived?.Invoke(senderActorNumber, playerData);
                    break;
                }

                case Commands.Ping:
                    _lastPingTimes[connection] = Time;
                    float            sendLocalTime = streamReader.ReadFloat();
                    DataStreamWriter writer        = _serverDriver.BeginSend(_unreliablePipeline, connection);
                    writer.WriteInt(Commands.Ping);
                    writer.WriteFloat(sendLocalTime);
                    writer.WriteFloat(Time);
                    writer.WriteFloat(UnityEngine.Time.deltaTime);
                    _serverDriver.EndSend(writer);
                    break;

                case Commands.RequestSpawnMessage:
                {
                    var connections = new NativeList <NetworkConnection>(1, Allocator.Temp)
                    {
                        connection
                    };
                    SendSpawnMessage(NetObjectManager.Instance.NetObjects, connections);
                    connections.Dispose();
                    break;
                }

                case Commands.UpdateNetObjects:
                {
                    int objectsInMessage = streamReader.ReadInt();
                    for (var obj = 0; obj < objectsInMessage; obj++)
                    {
                        int       netObjID  = streamReader.ReadInt();
                        int       size      = streamReader.ReadInt();
                        NetObject netObject = NetObjectManager.Instance.Exists(netObjID)
                                ? NetObjectManager.Instance.Get(netObjID)
                                : null;

                        // ignore illegal updates and those from local host client
                        if (netObject == null ||
                            netObject.OwnerActorNumber != senderActorNumber ||     // cheater?
                            Client.IsHost && Client.Instance.ActorNumber == senderActorNumber)
                        {
                            streamReader.DiscardBytes(size);
                        }
                        else
                        {
                            int bytesRead = streamReader.GetBytesRead();
                            try
                            {
                                netObject.Deserialize(ref streamReader, b => b.ClientAuthoritative);
                            }
                            catch (Exception e)
                            {
                                Debug.LogException(e);
                                int remainingBytes = size + bytesRead - streamReader.GetBytesRead();
                                streamReader.DiscardBytes(remainingBytes);
                            }
                        }
                    }

                    break;
                }

                case Commands.GameAction:
                {
                    int   gameActionID = streamReader.ReadInt();
                    int   actorNumber  = streamReader.ReadInt();
                    float triggerTime  = streamReader.ReadFloat();
                    try
                    {
                        GameAction             gameAction = GameActionManager.Instance.Get(gameActionID);
                        GameAction.IParameters parameters =
                            gameAction.DeserializeParameters(ref streamReader);
                        gameAction.ReceiveOnServer(parameters, actorNumber, senderActorNumber,
                                                   triggerTime);
                        break;
                    }
                    catch (Exception e)
                    {
                        Debug.LogException(e);
                        break;
                    }
                }

                case Commands.NetAssetRPC:
                {
                    float        sentServerTime = streamReader.ReadFloat();
                    int          netAssetID     = streamReader.ReadInt();
                    NetAsset     netAsset       = NetAssetManager.Instance.Get(netAssetID);
                    NetAsset.RPC rpc            = netAsset.DeserializeRPC(ref streamReader);
                    var          messageInfo    = new MessageInfo
                    {
                        SentServerTime = sentServerTime, SenderActorNumber = senderActorNumber
                    };
                    rpc.Invoke(messageInfo);
                    break;
                }

                case Commands.NetObjectRPC:
                {
                    float sentServerTime = streamReader.ReadFloat();
                    int   netObjectID    = streamReader.ReadInt();
                    if (!NetObjectManager.Instance.Exists(netObjectID))
                    {
                        Debug.LogWarning("Ignoring received RPC, because NetObject was not found.");
                        break;
                    }

                    NetObject            netObject      = NetObjectManager.Instance.Get(netObjectID);
                    ushort               netBehaviourID = streamReader.ReadUShort();
                    NetBehaviour         netBehaviour   = netObject.Get(netBehaviourID);
                    NetObjectManager.RPC rpc            =
                        NetObjectManager.Instance.DeserializeRPC(ref streamReader, netBehaviour);
                    var messageInfo = new MessageInfo
                    {
                        SentServerTime = sentServerTime, SenderActorNumber = senderActorNumber
                    };
                    rpc.Invoke(messageInfo);
                    break;
                }

                default:
                    Debug.LogException(new NetException($"Unknown command {command}"));
                    break;
                }
            }
            catch (Exception e)
            {
                Debug.LogException(new NetException("Failed to handle data event", e));
            }
        }
示例#29
0
 public void Deserialize(ref DataStreamReader reader)
 {
     playerID = reader.ReadInt();
     value    = reader.ReadUShort();
 }
 public void Deserialize(ref DataStreamReader reader)
 {
     startHP = reader.ReadUShort();
 }