public void Write(NetworkPacket packet) { if (_mClients != null) { byte[] data = ByteMethods.SerializeToBytes(packet); byte[] header = new byte[6]; header[0] = (byte)'$'; header[1] = (byte) '#'; Array.Copy(BitConverter.GetBytes(data.Length), 0, header, 2, 4); foreach (var sc in _mClients) { Traffic += data.Length+6; sc.PushGameData(header); sc.PushGameData(data); sc.FlushGameData(); } } }
public void AcceptPackets() { List <byte> RxBuffer = new List <byte>(); while (_mClient.Connected) { try { // TODO: This is really really messy. byte[] rxbuf = new byte[256 * 1024]; int available = _mStream.Read(rxbuf, 0, 256 * 1024); byte[] rxbuf2 = new byte[available]; Array.Copy(rxbuf, rxbuf2, available); RxBuffer.AddRange(rxbuf2); while (RxBuffer.Count >= 6) { if (RxBuffer.Count > 2 * 1024 * 1024) // Cap buffer at 2MiB { RxBuffer.RemoveRange(0, RxBuffer.Count - 2 * 1024 * 1024); // remove x bytes so 2 MiB remains. } try { if (RxBuffer[0] == (byte)'$' && RxBuffer[1] == (byte)'#') /// 0x24 + 0x23 { int size = BitConverter.ToInt32(RxBuffer.ToArray(), 2); if (RxBuffer.Count > size) { RxBuffer.RemoveRange(0, 6); NetworkPacket pack = (NetworkPacket)ByteMethods.DeserializeFromBytes(RxBuffer.ToArray()); if (Packet != null) { Packet(pack); } RxBuffer.RemoveRange(0, size); } else { break; } } } catch (Exception ex) { } int next = RxBuffer.IndexOf((byte)'$'); if (next >= 2) { RxBuffer.RemoveRange(0, next - 1); } } } catch (Exception ex) { } Thread.Sleep(5); } }
private void DataCreator() { int SecTimer = 0; while (_mServer.Running) { SecTimer++; try { NetworkPacket packet = new NetworkPacket(); packet.Type = NetworkTypes.SIMULATOR; if (!Telemetry.m.Active_Sim) { List <string> Sims = new List <string>(); Telemetry.m.Sims.Sims.ForEach(x => Sims.Add(x.ProcessName)); NetworkStateReport report = new NetworkStateReport(NetworkAppState.WAITING_SIM, Sims); packet.Data = ByteMethods.SerializeToBytes(report); _mServer.Write(packet); } else if (!Telemetry.m.Active_Session) { NetworkStateReport report = new NetworkStateReport(NetworkAppState.WAITING_SESSION, Telemetry.m.Sim.Name, Telemetry.m.Sim.ProcessName); packet.Data = ByteMethods.SerializeToBytes(report); _mServer.Write(packet); } // TODO: Add packet for game. if (Telemetry.m.Active_Sim && Telemetry.m.Active_Session) { NetworkStateReport report = new NetworkStateReport(NetworkAppState.RUNNING, Telemetry.m.Sim.Name, Telemetry.m.Sim.ProcessName); packet.Data = ByteMethods.SerializeToBytes(report); _mServer.Write(packet); packet.Type = NetworkTypes.SESSION; packet.Data = ByteMethods.SerializeToBytes(NetworkSession.Create(Telemetry.m.Sim.Session)); _mServer.Write(packet); packet.Type = NetworkTypes.DRIVER; packet.Data = ByteMethods.SerializeToBytes(NetworkDrivers.Create(Telemetry.m.Sim.Drivers)); _mServer.Write(packet); packet.Type = NetworkTypes.PLAYER; packet.Data = ByteMethods.SerializeToBytes(NetworkDriverPlayer.Create(Telemetry.m.Sim.Player)); _mServer.Write(packet); if (SecTimer % (5 * Bandwidth) == 0 && Telemetry.m.Track.Route != null) // every 5 seconds { // Send complete track route packet.Type = NetworkTypes.TRACKMAP; packet.Data = ByteMethods.SerializeToBytes(Telemetry.m.Track.Route); _mServer.Write(packet); packet.Type = NetworkTypes.TRACK; NetworkTrackInformation trackInfo = new NetworkTrackInformation(); trackInfo.Type = Telemetry.m.Track.Type; trackInfo.Location = Telemetry.m.Track.Location; trackInfo.Name = Telemetry.m.Track.Name; packet.Data = ByteMethods.SerializeToBytes(trackInfo); _mServer.Write(packet); } } } catch (Exception ex) { } Thread.Sleep(1000 / Bandwidth); } }