Hello I’ve decided to open a thread about serialization because I really have problem and I can’t progress my game I would like to know why binary serialization take so much space example I send only a object with size of 12 bytes (2 string for a totally of 12 characters) 1 character is 1 byte and according to unity console the size of sended object is 210 bytes the difference is so high …
210bytes sended.
UnityEngine.Debug:Log(Object)
Client:sendToServer(NMSG, Int32) (at Assets/Scripts/Network/Client.cs:153)
Main:connectAccount() (at Assets/Scripts/Client/Main.cs:220)
UnityEngine.EventSystems.EventSystem:Update()
sended object :
[System.Serializable]
public class NMSG_ConnectAccount : NMSG
{
public string username;
public string password;
public NMSG_ConnectAccount()
{
}
public NMSG_ConnectAccount(string username, string password) : base((byte)PacketHandler.packets.IndexOf(typeof(NMSG_ConnectAccount)))
{
this.username = username;
this.password = password;
}
public override void HandleClient(NMSG msg)
{
}
public override void HandleServer(NMSG msg, int connectionId)
{
NMSG_ConnectAccount cmsg = (NMSG_ConnectAccount)msg;
Server server = Server.getServer();
Mysql mysql = server.mysql;
password = EncryptionUtils.MD5Hash(password);
mysql.openMysqlConnection();
MySqlCommand commandsql = new MySqlCommand("SELECT * FROM users WHERE username = '" + cmsg.username + "'", mysql.con);
MySqlDataReader MyReader = commandsql.ExecuteReader();
string activated = "";
string mpassword = "";
string muser = "";
if (MyReader.Read())
{
activated = MyReader["confirmed"].ToString();
mpassword = MyReader["password"].ToString();
muser = MyReader["username"].ToString();
}
MyReader.Close();
if (mpassword != cmsg.password || cmsg.username != muser)
{
server.sendToPlayer(new NMSG_ConnectionMessage(cmsg.username,false,false), server.reliableChannel, connectionId);
return;
}
if (server.users.ContainsKey(connectionId))
server.users[connectionId].setName(muser);
else
return;
if (activated == "False")
{
server.sendToPlayer(new NMSG_ConnectionMessage(cmsg.username,false,true), server.reliableChannel, connectionId);
return;
}
server.users[connectionId].pData = new PlayerData(cmsg.username);
server.users[connectionId].isAuth = true;
server.sendToPlayer(new NMSG_ConnectionMessage(cmsg.username,true,false), server.reliableChannel, connectionId);
}
}
which is child of NMSG class :
[System.Serializable]
public abstract class NMSG
{
private byte? discriminator = null;
public NMSG()
{
}
public NMSG(byte discriminator)
{
this.discriminator = discriminator;
}
public byte? getPacketId()
{
return this.discriminator;
}
public abstract void HandleServer(NMSG msg, int connectionId);
public abstract void HandleClient(NMSG msg);
}
I serialize like this :
public void sendToServer(NMSG msg, int channelId)
{
if (!PacketHandler.packets.Contains(msg.GetType()))
{
Debug.Log("packet not registered");
return;
}
byte error;
byte[] buffer = new byte[BYTE_SIZE];
MemoryStream stream = new MemoryStream(buffer);
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(stream, msg);
}
catch (SerializationException e)
{
Debug.Log("Serialization Failed : " + e.Message);
}
Debug.Log(stream.Position + "bytes sended.");
stream.Close();
int bufferSize = buffer.Length;
NetworkTransport.Send(hostId, connectionId, channelId, buffer, bufferSize, out error);
}
and I deserialize :
case NetworkEventType.DataEvent: //3
MemoryStream stream = new MemoryStream();
stream.Write(recBuffer, 0, recBuffer.Length);
stream.Seek(0, SeekOrigin.Begin);
BinaryFormatter formatter = new BinaryFormatter();
NMSG msg = null;
try
{
msg = (NMSG)formatter.Deserialize(stream);
}
catch (SerializationException e)
{
Debug.Log("Deserialization Failed : " + e.Message);
}
stream.Close();
onData(connectionId, channelId, recHostId, msg);
break;
the message of connection that I send is not a problem but I have another object with which I create player instance with items and contains 6 int property 2 float property only this is a totally of 1100 bytes and the limit of bytes that I can send fixed in NetworkTransport is 1400 .
[System.Serializable]
public class NMSG_CreatePlayer : NMSG
{
public string playerName;
public float? posX;
public float? posY;
public int? shipSlot;
public int? skinSlot;
public int? particleSlot;
public int? trailSlot1;
public int? trailSlot2;
public int? connectionId;
public NMSG_CreatePlayer(string playerName) : base((byte)PacketHandler.packets.IndexOf(typeof(NMSG_CreatePlayer)))
{
this.playerName = playerName;
}
public NMSG_CreatePlayer(string playerName, int connectionId, float posX, float posY, int shipSlot, int skinSlot, int particleSlot, int trailSlot1, int trailSlot2) : base((byte)PacketHandler.packets.IndexOf(typeof(NMSG_CreatePlayer)))
{
this.playerName = playerName;
this.posX = posX;
this.posY = posY;
this.shipSlot = shipSlot;
this.skinSlot = skinSlot;
this.particleSlot = particleSlot;
this.trailSlot1 = trailSlot1;
this.trailSlot2 = trailSlot2;
this.connectionId = connectionId;
}
//Here packet data send from server
public override void HandleClient(NMSG msg)
{
NMSG_CreatePlayer cmsg = (NMSG_CreatePlayer)msg;
Player p = new Player(cmsg.playerName,0);
Ship ship = (Ship)Item.getItemById((int)cmsg.shipSlot);
Skin skin = (Skin)Item.getItemById((int)cmsg.skinSlot);
Particle particle = (Particle)Item.getItemById((int) this.particleSlot);
Trail trail1 = (Trail)Item.getItemById((int) this.trailSlot1);
Trail trail2 = (Trail)Item.getItemById((int) this.trailSlot2);
GameObject instance = GameObject.Instantiate(trail2 != null ? Main.instance.modelInitializator.playerModel_2Trail : Main.instance.modelInitializator.playerModel_1Trail, GameObject.Find("World").transform);
p.setPosition((float)cmsg.posX, (float)cmsg.posY);
instance.transform.localPosition = new Vector3(p.getPositionX(),p.getPositionY(), 0);
instance.name = playerName;
p.setObject(instance);
p.applyEquipment(ship, skin, particle, trail1, trail2);
p.setName(playerName);
p.setConnectionId((int)connectionId);
instance.transform.localEulerAngles = new Vector3(0, 0, 0);
Client.getClient().players.Add((int)connectionId, p);
if ((int) connectionId == Client.getClient().getConnectionId())
{
instance.AddComponent<PlayerController>();
Main.player = p;
Client.getClient().gm = GameManager.createGameManager(p);
p.isInGame = true;
}
}
//Here packet data send from client
public override void HandleServer(NMSG msg, int connectionId)
{
NMSG_CreatePlayer cmsg = (NMSG_CreatePlayer)msg;
Player p = Server.getPlayer(connectionId);
p.setName(cmsg.playerName);
float posx = World.generateRandomPosX();
float posy = World.generateRandomPosY();
p.setPosition(posx, posy);
GameObject instance = GameObject.Instantiate(Main.instance.modelInitializator.player, GameObject.Find("World").transform);
instance.transform.localPosition = new Vector3(posx, posy, 0);
p.playerGameObject = instance;
int shipSlot = p.pData.shipSlot == null ? 0 : p.pData.shipSlot.getItemId();
int skinSlot = p.pData.skinSlot == null ? 0 : p.pData.skinSlot.getItemId();
int particleSlot = p.pData.particleSlot == null ? 0 : p.pData.particleSlot.getItemId();
int trailSlot1 = p.pData.trailSlot1 == null ? 0 : p.pData.trailSlot1.getItemId();
int trailSlot2 = p.pData.trailSlot2 == null ? 0 : p.pData.trailSlot2.getItemId();
Server.getServer().sendToAll(new NMSG_CreatePlayer(p.getName(), p.getConnectionId(), p.getPositionX(), p.getPositionY(), shipSlot, skinSlot, particleSlot, trailSlot1, trailSlot2),Server.getServer().reliableFragmentedChannel);
p.isInGame = true;
}
}
Is the problem coming from me?
the manipulation for serialize that I use is wrong?
I look forward to your advice, thank you in advance.