void C_OnReceivedMessage(byte[] rawMessage) { SharpSerializer ser = SharpSerializer.Create(rawMessage); SharpConnectionMessages incomingType = (SharpConnectionMessages)ser.ReadByte(); if (incomingType == SharpConnectionMessages.ClientConnected) { SharpClient incomingClient = new SharpClient(ref ser); this.room.AssignClient(incomingClient); C_OnClientConnected(incomingClient); } else if (incomingType == SharpConnectionMessages.ClientDisconnected) { short netID = ser.ReadInt16(); C_OnClientDisconnected(this.room.Get(netID)); } else if (incomingType == SharpConnectionMessages.DirectData) { short sender = ser.ReadShort(); int channel = ser.ReadInt32(); int packageSize = ser.ReadInt32(); byte[] rawData = ser.ReadBytes(packageSize); C_OnReceivedData(this.room.Get(sender), channel, rawData); } ser.Post(); }
void S_OnReceivedMessage(SharpClient sender, byte[] data) { SharpSerializer ser = SharpSerializer.Create(data); SharpConnectionMessages type = (SharpConnectionMessages)ser.ReadByte(); if (type == SharpConnectionMessages.MessageType1) { #region Message Type 1 SharpTargets targets = (SharpTargets)ser.ReadByte(); int channel = ser.ReadInt32(); int size = ser.ReadInt32(); byte[] rawData = ser.ReadBytes(size); if (targets == SharpTargets.Server) { S_OnReceivedData(sender, channel, rawData); return; } SharpSerializer package = SharpSerializer.Create(); package.Write((byte)SharpConnectionMessages.DirectData); package.Write(sender.NetworkID); package.Write(channel); package.Write(size); package.Write(rawData); if (targets == SharpTargets.All) { Broadcast(package.DataAndPost(), null); S_OnReceivedData(sender, channel, rawData); } else if (targets == SharpTargets.ClientsOnly) { Broadcast(package.DataAndPost(), null); } else if (targets == SharpTargets.OtherClientsOnly) { Broadcast(package.DataAndPost(), sender); } else if (targets == SharpTargets.Others) { Broadcast(package.DataAndPost(), sender); S_OnReceivedData(sender, channel, rawData); } #endregion } else if (type == SharpConnectionMessages.MessageType2) { #region Message Type 2 short targetID = ser.ReadInt16(); int channel = ser.ReadInt32(); int size = ser.ReadInt32(); byte[] rawData = ser.ReadBytes(size); if (targetID == 0) { S_OnReceivedData(sender, channel, rawData); return; } SharpSerializer package = SharpSerializer.Create(); package.Write((byte)SharpConnectionMessages.DirectData); package.Write(sender.NetworkID); package.Write(channel); package.Write(size); package.Write(rawData); this.room.Get(targetID).SendMessage(package.DataAndPost()); #endregion } else if (type == SharpConnectionMessages.MessageType3) { #region Message Type 3 short targetCount = ser.ReadInt16(); short[] targets = new short[targetCount]; for (int i = 0; i < targetCount; i++) { targets[i] = ser.ReadInt16(); } int channel = ser.ReadInt32(); int size = ser.ReadInt32(); byte[] rawData = ser.ReadBytes(size); SharpSerializer package = SharpSerializer.Create(); package.Write((byte)SharpConnectionMessages.DirectData); package.Write(sender.NetworkID); package.Write(channel); package.Write(size); package.Write(rawData); byte[] goData = package.DataAndPost(); for (int i = 0; i < targets.Length; i++) { if (targets[i] == 0) { S_OnReceivedData(sender, channel, rawData); } else { this.room.Get(targets[i]).SendMessage(goData); } } #endregion } }