public ChannelHandler(uint myChannel, Type Serializable, bool loggedInOnly) { LoggedInOnly = loggedInOnly; ChannelID = myChannel; if (Serializable != typeof(void)) { processor = SerialInterface.Build(Serializable); } }
public ChannelSender(Type t) { processor = SerialInterface.Build(t); }
protected override void listen() { bool readPayload = false; int channel = 0; int size = 0; byte[] payload; int received = 0; byte[] headerBuffer = new byte[8]; while (doListen || connected) { try { //Wait for 8 bytes if (!readPayload) { received = 0; while (received < 8) { received += clientStream.Read(headerBuffer, received, 8 - received); } channel = BitConverter.ToInt32(headerBuffer, 0); size = BitConverter.ToInt32(headerBuffer, 4); readPayload = true; } else if (readPayload) { received = 0; payload = new byte[size]; while (received < size) { received += clientStream.Read(payload, received, size - received); } byte[] data = new byte[8 + size]; BitConverter.GetBytes(channel).CopyTo(data, 0); BitConverter.GetBytes(size).CopyTo(data, 4); payload.CopyTo(data, 8); if (initializing) { if (channel == (int)ChannelID.Hello) { SerialInterface proc = SerialInterface.Build(typeof(Result)); Result r = (Result)proc.Deserialize(payload, size); if (r.success == false) { disconnect(); return; } joinAsSpectator(); } else if (channel == (int)ChannelID.ClientFaction) { SerialInterface proc = SerialInterface.Build(typeof(ClientFactionResponse)); ClientFactionResponse r = (ClientFactionResponse)proc.Deserialize(payload, size); host.planetConfig = r.planetConfig; initializing = false; } else if (channel == (int)ChannelID.PhaseChange) { host.phase = data; } } else { if (channel == (int)ChannelID.PhaseChange) { host.phase = data; } if (channel != 2) { buffer.add(data); //server.sendToClients(data, data.Length); //TODO Redirect to analytics } } readPayload = false; } Thread.Sleep(1); } catch (Exception e) { Log.error("Error while reading data from client.", this); Log.error(e.Message, this); this.disconnect(); } } }