public object Deserialize(byte[] serializedData) { if (serializedData.Length == 0) { return(null); } return(ObjectSerializer.GetObjects(0, 0, serializedData, GetValueType())[0]); }
internal override void Receive(Identity id, byte[] packet, int size, int channel) { base.Receive(id, packet, size, channel); EPacket parsedPacket = (EPacket)packet[0]; StripPacketByte(ref packet, ref size); if (parsedPacket.IsUpdate()) { foreach (Channel ch in Receivers.Where(ch => ch.ID == channel)) { ch.Receive(id, packet, 0, size); } return; } if (id != ServerID) { return; } switch (parsedPacket) { case EPacket.WORKSHOP: //todo break; case EPacket.TICK: { var data = ObjectSerializer.GetBytes(0, Time.realtimeSinceStartup); Send(ServerID, EPacket.TIME, data, 0); break; } case EPacket.TIME: { object[] args = ObjectSerializer.GetObjects(0, 0, packet, typeof(float)); LastNet = Time.realtimeSinceStartup; OffsetNet = ((float)args[0]) + ((Time.realtimeSinceStartup - LastPing) / 2f); Lag(Time.realtimeSinceStartup - LastPing); break; } case EPacket.SHUTDOWN: Disconnect(); break; case EPacket.CONNECTED: { { Type[] argTypes = { //[0] id, [1] name, [2] group, [3] position, [4], angle, [5] channel typeof(Identity), typeof(string), typeof(ulong), typeof(Vector3), typeof(Vector3), typeof(int), typeof(bool) }; object[] args = ObjectSerializer.GetObjects(0, 0, packet, argTypes); if (IsSinglePlayer) { return; } if (World.Loaded) { AddPlayer(Provider.Deserialilze((Identity)args[0]), (string)args[1], (ulong)args[2], (Vector3)args[3], (Vector3)args[4], (int)args[5], (bool)args[6]); } else { QueuePlayer(Provider.Deserialilze((Identity)args[0]), (string)args[1], (ulong)args[2], (Vector3)args[3], (Vector3)args[4], (int)args[5], (bool)args[6]); } break; } } case EPacket.VERIFY: LogUtils.Debug("Opening ticket"); byte[] ticket = ((ClientMultiplayerProvider)Provider).OpenTicket(); if (ticket == null) { LogUtils.Debug("ticket equals null"); Disconnect(); break; } Send(ServerID, EPacket.AUTHENTICATE, ticket, ticket.Length, 0); break; case EPacket.DISCONNECTED: { //If singleplayer (local server) we will already do this at ServerConnection if (IsSinglePlayer) { return; } object[] args = ObjectSerializer.GetObjects(0, 0, packet, typeof(byte)); var index = (byte)args[0]; var user = GetUser(index); PlayerQuitEvent @event = new PlayerQuitEvent(user.Player); EventManager.Instance.CallEvent(@event); RemovePlayer(index); break; } case EPacket.REJECTED: case EPacket.KICKED: Disconnect(); break; case EPacket.ACCEPTED: { object[] args = ObjectSerializer.GetObjects(0, 0, packet, typeof(ulong), typeof(int)); LogUtils.Debug("Setting MainPlayer channel to: " + (int)args[1]); ((ClientMultiplayerProvider)Provider).SetIdentity((ulong)args[0]); ((ClientMultiplayerProvider)Provider).AdvertiseGame(ServerID, _currentIp, _currentPort); ((ClientMultiplayerProvider)Provider).SetConnectInfo(_currentIp, _currentPort); IsFavoritedServer = ((ClientMultiplayerProvider)Provider).IsFavoritedServer(_currentIp, _currentPort); ((ClientMultiplayerProvider)Provider).FavoriteServer(_currentIp, _currentPort); break; } case EPacket.UPDATE_CHANNELS: { object[] args = ObjectSerializer.GetObjects(0, 0, packet, ChannelCount.GetType()); ChannelCount = (int)args[0]; break; } default: LogUtils.LogWarning("Couldn't handle packet: " + parsedPacket); break; } }
internal override void Receive(Identity source, byte[] packet, int size, int channel) { base.Receive(source, packet, size, channel); var net = ((OffsetNet + Time.realtimeSinceStartup) - LastNet); EPacket parsedPacket = (EPacket)packet[0]; StripPacketByte(ref packet, ref size); if (parsedPacket.IsUpdate()) { if (source == ServerID) { foreach (Channel ch in Receivers) { ch.Receive(source, packet, 0, size); } } else { if (Clients.All(client => client.Identity != source)) { return; } foreach (Channel ch in Receivers.Where(ch => ch.ID == channel)) { ch.Receive(source, packet, 0, size); } } return; } PendingUser currentPending; switch (parsedPacket) { case EPacket.WORKSHOP: { //workshop list {none for now} List <ulong> workshoplist = new List <ulong>(); byte[] args = new byte[1 + (workshoplist.Count * 8)]; args[0] = (byte)workshoplist.Count; for (byte i = 0; i < workshoplist.Count; i = (byte)(i + 1)) { BitConverter.GetBytes(workshoplist[i]).CopyTo(args, (1 + (i * 8))); } Send(source, EPacket.WORKSHOP, args, args.Length, 0); return; } case EPacket.TICK: { object[] objects = { net }; byte[] buffer2 = ObjectSerializer.GetBytes(0, objects); Send(source, EPacket.TIME, buffer2, 0); return; } case EPacket.TIME: foreach (User c in Clients.Where(c => c.Identity == source)) { if (!(c.LastPing > 0f)) { return; } c.LastNet = Time.realtimeSinceStartup; c.Lag(Time.realtimeSinceStartup - c.LastPing); c.LastPing = -1f; return; } return; case EPacket.CONNECT: { if (_pendingPlayers.Any(p => p.Identity == source)) { Reject(source, ERejectionReason.ALREADY_PENDING); return; } if (Clients.Any(c => c.Identity == source)) { Reject(source, ERejectionReason.ALREADY_CONNECTED); return; } Type[] argTypes = { // [0] name, [1] group, [2] version, [3] ping typeof(string), typeof(ulong), typeof(string), typeof(float) }; var args = ObjectSerializer.GetObjects(0, 0, packet, argTypes); var playerName = (string)args[0]; var group = (ulong)args[1]; var version = (string)args[2]; var ping = (float)args[3]; LogUtils.Log("Player connecting: " + playerName); if (version != GameInfo.VERSION) { Reject(source, ERejectionReason.WRONG_VERSION); return; } if ((Clients.Count + 1) > MultiplayerProvider.MultiplayerProvider.MAX_PLAYERS) { Reject(source, ERejectionReason.SERVER_FULL); return; } var pendingPlayer = new PendingUser(source, playerName, group, ping); _pendingPlayers.Add(pendingPlayer); if (Provider.SupportsAuthentification) { Send(source, EPacket.VERIFY, new byte[] { }, 0, 0); return; } pendingPlayer.HasAuthentication = true; Accept(pendingPlayer); return; } default: if (parsedPacket != EPacket.AUTHENTICATE) { LogUtils.LogError("Failed to handle message: " + parsedPacket); return; } currentPending = _pendingPlayers.FirstOrDefault(p => p.Identity == source); break; } if (currentPending == null) { Reject(source, ERejectionReason.NOT_PENDING); } else if ((Clients.Count + 1) > MultiplayerProvider.MultiplayerProvider.MAX_PLAYERS) { Reject(source, ERejectionReason.SERVER_FULL); } else { object[] args = ObjectSerializer.GetObjects(0, 0, packet, typeof(byte[])); if (!((ServerMultiplayerProvider)Provider).VerifyTicket(source, (byte[])args[0])) { Reject(source, ERejectionReason.AUTH_VERIFICATION); } } }
public void Receive(Identity ident, byte[] packet, int start, int end) { LogUtils.LogNetwork(nameof(Receive) + "; ident: " + ident); if (end < sizeof(byte) * 2) { return; // we need at least 2 bytes } if (ident.GetUser() == null) { ident = Connection.Provider.GetServerIdent(); } int index = packet[start + 1]; if ((index < 0) || (index >= Calls.Count)) { return; } EPacket packet2 = (EPacket)packet[start]; var call = Calls[index]; var networkCall = (NetworkCallAttribute)call.Method.GetCustomAttributes(typeof(NetworkCallAttribute), true)[0]; if (networkCall.ValidateOwner && networkCall.ValidateServer) { if (!ValidateOwner(ident, false) && !ValidateServer(ident, false)) { throw new Exception("Couldn't validate owner/server"); } } else if (networkCall.ValidateOwner) { ValidateOwner(ident); } else if (networkCall.ValidateServer) { ValidateServer(ident); } if (networkCall.ConnectionEnd != ConnectionEnd.BOTH) { switch (networkCall.ConnectionEnd) { case ConnectionEnd.CLIENT: if (!Connection.IsClient()) { throw new Exception(Calls[index].Method.Name + " is not supposed to run on this connection end"); } break; case ConnectionEnd.SERVER: if (!Connection.IsServer()) { throw new Exception(Calls[index].Method.Name + " is not supposed to run on this connection end"); } break; } } if ((packet2 == EPacket.UPDATE_VOICE) && (end < 4)) { return; } if ((packet2 == EPacket.UPDATE_UNRELIABLE_CHUNK_BUFFER) || (packet2 == EPacket.UPDATE_RELIABLE_CHUNK_BUFFER)) { Buffer.Reset(start + 2, packet); object[] parameters = { ident }; Calls[index].Method.Invoke(Calls[index].Component, parameters); } else if (Calls[index].Types.Length > 0) { if (packet2 == EPacket.UPDATE_VOICE) { Voice[0] = ident; Voice[1] = packet; Voice[2] = BitConverter.ToUInt16(packet, start + 2); Calls[index].Method.Invoke(Calls[index].Component, Voice); return; } object[] objArray = ObjectSerializer.GetObjects(ident, start, 2, packet, true, Calls[index].Types); if (objArray != null) { Calls[index].Method.Invoke(Calls[index].Component, objArray); } return; } Calls[index].Method.Invoke(Calls[index].Component, null); }