public override void Serialize(NetDataWriter writer)
        {
            // Serialize base
            base.Serialize(writer);

            // Serialize class
            writer.PutBytesWithLength(LevelProgress);
            writer.PutBytesWithLength(BoltsExpPoints);
            writer.PutBytesWithLength(Skillpoints);
            writer.PutBytesWithLength(AlphaOmegaMods);
        }
Example #2
0
        public void Serialize(NetDataWriter writer)
        {
            writer.Put(AddonId);
            writer.Put(AddonName);
            writer.Put(AddonAuthor);
            writer.Put(AddonVersion);

            writer.PutBytesWithLength(SendPermissions);
            writer.PutBytesWithLength(ReceivePermissions);

            writer.PutBytesWithLength(RemoteConfig, 0, RemoteConfig.Length);
        }
Example #3
0
 public void Serialize(NetDataWriter writer)
 {
     writer.Put(_packetVersion);
     // length int inserted
     writer.PutBytesWithLength(Encoding.UTF8.GetBytes(PreferredCodec));
     writer.Put((byte)SupportedCodecs.Length);
     for (int i = 0; i < SupportedCodecs.Length; i++)
     {
         // length int inserted
         writer.PutBytesWithLength(Encoding.UTF8.GetBytes(SupportedCodecs[i]));
     }
 }
Example #4
0
 /// <summary>
 /// Write a packet specifically for MessagePack.
 /// </summary>
 /// <typeparam name="T">Packet Type</typeparam>
 /// <param name="type">Packet Type to pack into writer</param>
 /// <param name="packet">Packet object</param>
 /// <returns></returns>
 public NetDataWriter write_packet <T>(PacketType type, T packet) where T : class, new()
 {
     m_CachedWriter.Reset();
     m_CachedWriter.Put((byte)type);
     m_CachedWriter.PutBytesWithLength(MessagePackSerializer.Serialize(packet));
     return(m_CachedWriter);
 }
 public void Serialize(NetDataWriter writer)
 {
     writer.Put(_packetVersion);
     writer.Put(SampleRate);
     writer.Put(Channels);
     writer.PutBytesWithLength(Encoding.UTF8.GetBytes(Codec));
 }
Example #6
0
    private void RegisterPlayer(NetPacketReader reader)
    {
        if (isHost)
        {
            var pinf = new PlayerInfo(MyNetworkId, rand.Next(0, 10000000), isHost, "test", new ColorPacket(myColor.r, myColor.g, myColor.b));
            InitPlayer(pinf);
        }
        else
        {
            NetDataWriter writer      = new NetDataWriter();
            var           networkId   = reader.GetInt();
            var           playerId    = reader.GetInt();
            var           ishost      = reader.GetBool();
            var           playerColor = myColor;
            var           pName       = myName;
            MyNetworkId = networkId;

            var playerInfo = new PlayerInfo(networkId, playerId, ishost, pName, new ColorPacket(playerColor.r, playerColor.g, playerColor.b));

            InitPlayer(playerInfo);

            writer.Put((ushort)1);
            writer.PutBytesWithLength(playerInfo.ToByteArray());

            Send(writer, DeliveryMethod.ReliableOrdered);

            writer.Reset();
        }
    }
        void INetSerializable.Serialize(NetDataWriter netWriter)
        {
            if (State == null)
            {
                byte[] bytes = new byte[0];
                netWriter.PutBytesWithLength(bytes);
                return;
            }

            using (MemoryStream stream = new MemoryStream())
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, State);
                netWriter.PutBytesWithLength(stream.ToArray());
            }
        }
Example #8
0
        // -- INetSerializable -------------------------------------------------

        public void Serialize(NetDataWriter writer)
        {
            writer.Put(Bounds.CentreX); writer.Put(Bounds.CentreY); writer.Put(Bounds.Radius);
            Path.Serialize(writer);
            writer.PutBytesWithLength(BusAddress.GetAddressBytes());
            writer.Put(BusPort);
            writer.Put(ApiUrl);
        }
        public override void Serialize(NetDataWriter writer)
        {
            // Serialize base
            base.Serialize(writer);

            // Serialize class
            writer.PutBytesWithLength(Equipment);
        }
Example #10
0
    public void SendUpdatedData()
    {
        var writer = new NetDataWriter();

        writer.Put((ushort)4);
        writer.PutBytesWithLength(playerInfo.ToByteArray());
        NetworkManager.Instance.Send(writer, DeliveryMethod.ReliableOrdered);
    }
Example #11
0
    public void SendColorUpdate(Color color)
    {
        currentColor = color;
        RefreshColor();
        NetDataWriter writer = new NetDataWriter();

        writer.Put((ushort)1);
        writer.PutBytesWithLength(new ColorPacket(color.r, color.g, color.b).ToByteArray());
        Send(writer, DeliveryMethod.ReliableOrdered);
    }
Example #12
0
 public byte[] Serialize(NetDataWriter writer)
 {
     writer.Put((uint)NetStates.Count);
     for (int i = 0; i < NetStates.Count; i++)
     {
         writer.PutBytesWithLength(NetStates[i]);
         writer.Put(NetIDs[i]);
     }
     return(writer.CopyData());
 }
Example #13
0
        public override Task OutcomingMessage(NList message)
        {
            byte[]        data          = ProtoUtils.Serialize(message);
            NetDataWriter netDataWriter = new NetDataWriter();

            netDataWriter.PutBytesWithLength(data);
            Connection.Send(netDataWriter, DeliveryMethod.ReliableOrdered);

            return(Task.CompletedTask);
        }
Example #14
0
 public void CreateDefaultConnectionData()
 {
     defaultdata = new NetDataWriter();
     defaultdata.Put(plugin.Config.HostConnectionKey);
     defaultdata.Put(plugin.Version.ToString(3));
     defaultdata.Put(Server.Port);
     defaultdata.Put(CustomNetworkManager.slots);
     defaultdata.PutArray(ClientAddons.Select(p => p.Key).ToArray());
     if (File.Exists(tokenPath))
     {
         var bytes = File.ReadAllBytes(tokenPath);
         defaultdata.PutBytesWithLength(bytes, 0, bytes.Length);
     }
     else
     {
         var bytes = Encoding.UTF8.GetBytes(string.Empty);
         defaultdata.PutBytesWithLength(bytes, 0, bytes.Length);
     }
 }
Example #15
0
    public void WriteData(NetDataWriter writer)
    {
        writer.Put((ushort)103);
        writer.Put(objectData.objectId);
        writer.PutBytesWithLength(objectData.positionData.ToByteArray());

        objectData.positionData.lastPacketId++;

        oldPosition = GetPosition();
        oldRotation = GetRotation();
    }
Example #16
0
        public void Serialize(NetDataWriter writer)
        {
            writer.Put(levelId);
            writer.Put(songName);
            writer.Put(songSubName);
            writer.Put(songAuthorName);
            writer.Put(levelAuthorName);
            writer.Put(beatsPerMinute);
            writer.Put(songDuration);

            writer.Put(characteristic);
            writer.PutVarUInt((uint)difficulty);

            if (this.coverImage != null)
            {
                writer.PutBytesWithLength(coverImage);
            }
            else
            {
                Plugin.Log?.Debug($"coverImage is null when serializing '{levelId}'");
                writer.PutBytesWithLength(Array.Empty <byte>());
            }
        }
Example #17
0
    public GameObject InstantiateNetworkObject(string prefabName, int playerId, Vector3 pos, Quaternion rot)
    {
        ushort     i   = 0;
        GameObject obj = null;

        if (Thread.CurrentThread == mainThread)
        {
            obj = Instantiate(NetworkInfo.GetNetworkPrefabByName(prefabName), pos, rot);
            //obj.SetActive(false);
            while (ObjectsInWaiting.ContainsKey(i))
            {
                i++;
            }

            ObjectsInWaiting.Add(i, obj);
        }
        else
        {
            UnityThread.executeInUpdate(
                () =>
            {
                print("search for " + prefabName);
                foreach (var p in NetworkInfo.networkPrefabs)
                {
                    print(p.name);
                }
                print(NetworkInfo.GetNetworkPrefabByName(prefabName) + " " + NetworkInfo.networkPrefabs.Length);
                obj = Instantiate(NetworkInfo.GetNetworkPrefabByName(prefabName), pos, rot);
                //obj.SetActive(false);
                while (ObjectsInWaiting.ContainsKey(i))
                {
                    i++;
                }

                ObjectsInWaiting.Add(i, obj);
            });
        }

        ObjectData    data   = new ObjectData(playerId, -1, NetworkInfo.GetNetworkPrefabId(prefabName), new Position(pos.x, pos.y, pos.z), new Rotation(rot.x, rot.y, rot.z, rot.w));
        NetDataWriter writer = new NetDataWriter();

        writer.Put((ushort)101);
        writer.Put(i);
        writer.PutBytesWithLength(data.ToByteArray());
        Send(writer, DeliveryMethod.ReliableOrdered);

        return(obj);
    }
Example #18
0
 public void SendObjectData(NetPeer peer, NetDataWriter writer)
 {
     writer.Put((ushort)102);
     writer.Put(peer.Id);
     writer.PutBytesWithLength(objectData.ToByteArray());
 }
Example #19
0
        public void NetworkReceived(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod)
        {
            try
            {
                if (connectionActive(peer))
                {
                    string  uuid        = clientIDs.Forward[peer.Id];
                    string  partnerUuid = activeConnections[uuid];
                    long    partnerID   = clientIDs.Reverse[partnerUuid];
                    NetPeer partnerPeer = peers[partnerID];

                    NetDataWriter writer = new NetDataWriter();
                    writer.Put(reader.GetRemainingBytes());
                    partnerPeer.Send(writer, DeliveryMethod.ReliableOrdered);
                }
                else
                {
                    int    packetId   = reader.GetInt();
                    string uuid       = reader.GetString();
                    byte[] data       = reader.GetBytesWithLength();
                    int    activity   = reader.GetInt();
                    string toUuid     = reader.GetString();
                    int    toActivity = reader.GetInt();

                    //stop the connection if it's not CLIENT_INFO
                    if (packetId != CLIENT_INFO)
                    {
                        peer.Disconnect();//bad packet!
                    }
                    else if (clientIDs.Reverse.Contains(uuid))
                    {
                        peer.Disconnect(createDCMsg(DC_CODE_ALREADY_CONNECTED));
                    }
                    else
                    {
                        clientIDs.Add(peer.Id, uuid);
                        info.Add(uuid, new ClientInfo(data, activity, toActivity));
                        if (searchingConnections.ContainsKey(toUuid))
                        {
                            searchingConnections.Remove(toUuid);
                            activeConnections.Add(uuid, toUuid);
                            activeConnections.Add(toUuid, uuid);

                            ClientInfo selfInfo    = info[uuid];
                            ClientInfo partnerInfo = info[toUuid];

                            long    partnerID   = clientIDs.Reverse[toUuid];
                            NetPeer partnerPeer = peers[partnerID];

                            if (selfInfo.Activity != partnerInfo.ToActivity ||
                                selfInfo.ToActivity != partnerInfo.Activity)
                            {
                                peer.Disconnect(createDCMsg(DC_CODE_DIFFERENT_ACTIVITY));
                                partnerPeer.Disconnect(createDCMsg(DC_CODE_DIFFERENT_ACTIVITY));
                            }
                            else
                            {
                                {
                                    NetDataWriter writer = new NetDataWriter();
                                    writer.Put(SERVER_CONNECTED);
                                    writer.PutBytesWithLength(partnerInfo.Data);
                                    peer.Send(writer, DeliveryMethod.ReliableOrdered);
                                }
                                {
                                    NetDataWriter writer = new NetDataWriter();
                                    writer.Put(SERVER_CONNECTED);
                                    writer.PutBytesWithLength(selfInfo.Data);
                                    partnerPeer.Send(writer, DeliveryMethod.ReliableOrdered);
                                }
                            }
                        }
                        else
                        {
                            searchingConnections.Add(uuid, toUuid);
                        }
                    }
                }

                reader.Recycle();
            }
            catch (Exception ex)
            {
                DiagManager.Instance.LogError(ex);
            }
        }
        private void OnNetworkReceived(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod)
        {
            //rely on current connection state to determine what packets are coming in.
            if (Status == OnlineStatus.Connecting)
            {
                int packetId = reader.GetInt();
                //TODO: stop the connection if it's not SERVER_INTRO?
                string serverName = reader.GetString();
                //if (!P2P)
                Activity.Server.ServerName = serverName;
                //else
                //    Activity.TargetInfo.Data.TeamName = serverName;

                DiagManager.Instance.SaveSettings(DiagManager.Instance.CurSettings);
                NetDataWriter writer = new NetDataWriter();
                writer.Put(CLIENT_INFO);
                writer.Put(Activity.SelfInfo.UUID);
                writer.PutBytesWithLength(Activity.SelfInfo.SerializeData());
                writer.Put((int)Activity.Activity);
                writer.Put(Activity.TargetInfo.UUID);
                writer.Put((int)Activity.CompatibleActivity);
                peer.Send(writer, DeliveryMethod.ReliableOrdered);
                Status = OnlineStatus.FindingPartner;
            }
            else if (Status == OnlineStatus.FindingPartner)
            {
                int packetId = reader.GetInt();
                //TODO: stop the connection if it's not SERVER_CONNECTED?
                byte[] partnerData = reader.GetBytesWithLength();
                Activity.TargetInfo.DeserializeData(partnerData);
                Activity.TargetInfo.LastContact = String.Format("{0:yyyy-MM-dd}", DateTime.Now);
                DiagManager.Instance.SaveSettings(DiagManager.Instance.CurSettings);
                Activity.SetPeer(peer);
                Status = OnlineStatus.Connected;
            }
            else if (Status == OnlineStatus.ReceivingPartner)
            {
                int packetId = reader.GetInt();
                //TODO: stop the connection if it's not CLIENT_INFO?
                string partnerUuid = reader.GetString();
                if (partnerUuid != Activity.TargetInfo.UUID)
                {
                    peerDisconnect(peer, NetCloseReason.PeerIDMismatch);
                }
                else
                {
                    byte[] partnerData = reader.GetBytesWithLength();
                    Activity.TargetInfo.DeserializeData(partnerData);
                    Activity.TargetInfo.LastContact = String.Format("{0:yyyy-MM-dd}", DateTime.Now);
                    DiagManager.Instance.SaveSettings(DiagManager.Instance.CurSettings);

                    //compare activity for validity
                    ActivityType theirActivity = (ActivityType)reader.GetInt();
                    string       yourUuid      = reader.GetString();
                    if (yourUuid != Activity.SelfInfo.UUID)
                    {
                        peerDisconnect(peer, NetCloseReason.PeerIDMismatch);
                    }
                    else
                    {
                        ActivityType yourActivity = (ActivityType)reader.GetInt();
                        if (yourActivity != Activity.Activity || theirActivity != Activity.CompatibleActivity)
                        {
                            peerDisconnect(peer, NetCloseReason.DifferentActivity);
                        }
                        else
                        {
                            //Send your own info
                            NetDataWriter writer = new NetDataWriter();
                            writer.Put(SERVER_CONNECTED);
                            writer.PutBytesWithLength(Activity.SelfInfo.SerializeData());
                            peer.Send(writer, DeliveryMethod.ReliableOrdered);

                            Activity.SetPeer(peer);
                            Status = OnlineStatus.Connected;
                        }
                    }
                }
            }
            else if (Status == OnlineStatus.Connected)
            {
                Activity.NetworkReceived(peer, reader, deliveryMethod);
            }

            reader.Recycle();
        }
 public void PutBytesWithLength(byte[] data)
 {
     _networkWriterImplementation.PutBytesWithLength(data);
 }