public TcpServerWrapper(TcpListener server) { _server = server; Task.Run(() => { while (true) { var client = _server.AcceptTcpClient(); _connected.TryAdd(client, client.GetStream()); OnClientConnected?.Invoke(this, client); } }); Task.Run(() => { while (true) { foreach (var pair in _connected) { if (!pair.Key.Connected) { _connected.TryRemove(pair.Key, out var removed); } if (pair.Value.DataAvailable) { using (var sr = new BinaryReader(pair.Value)) { _messageQueue.Enqueue(new Tuple <TcpClient, byte[]>(pair.Key, sr.ReadBytes(pair.Key.Available))); } } } } }); new Thread(() => { while (true) { if (!_messageQueue.IsEmpty) { _messageQueue.TryDequeue(out var message); var payload = new ByteBufferReader(message.Item2); OnReceivedMessage?.Invoke(this, new TcpPacketEventArgs(message.Item1, payload)); } else { Thread.Sleep(1); } } }) { IsBackground = true }.Start();
public TcpClientWrapper(TcpClient client) { _client = client; Task.Run(() => { while (true) { if (_client.Connected && _client.Available > 0) { using (var stream = _client.GetStream()) { var data = new byte[_client.Available]; stream.Read(data, 0, data.Length); //var msg = Encoding.UTF8.GetString(data); _messageQueue.Enqueue(data); } } } }); new Thread(() => { while (true) { if (!_messageQueue.IsEmpty) { _messageQueue.TryDequeue(out var message); /* * var split = message.Split('|'); * for (int i = 0; i < split.Length; i++) * split[i] = split[i].Replace("{p}", "|"); * * var code = split[0]; * * split = split.Skip(1).ToArray(); */ using (var pr = new ByteBufferReader(message)) { OnReceivedMessage?.Invoke(this, new TcpPacketEventArgs(null, pr)); } } else { Thread.Sleep(1); } } }) { IsBackground = true }.Start();
private void PlayerShot(PlayerDummy player, ByteBufferReader data) { var pos = data.ReadVec2(); var dir = data.ReadVec2(); var hit = new List <Tuple <float, PlayerDummy> >(); foreach (var playerDummy in _players.Values) { if (player == playerDummy || playerDummy.Health <= 0) { continue; } if (RayHelper.Intersects(playerDummy.GetBoundingBox(), pos, dir, out var dist)) { hit.Add(new Tuple <float, PlayerDummy>(dist, playerDummy)); } } var damageToDeal = 35f; foreach (var tuple in hit.OrderBy(el => el.Item1)) { var plr = tuple.Item2; if (plr.Respawning) { continue; } plr.Health -= damageToDeal; if (plr.Health <= 0) { plr.BeginRespawn(); } damageToDeal /= 2; } dir *= float.MaxValue; var payload = new ByteBufferWriter(data.Code); payload.WriteVec2(pos); payload.WriteVec2(dir); SendUdpMessageToAllExcept(player, payload); }
private void LoadMap() { var file = "maps\\map0.ssmap"; if (!File.Exists(file)) { return; } var payload = new ByteBufferReader(File.ReadAllBytes(file)); var count = payload.ReadInt32(); for (int i = 0; i < count; i++) { var min = payload.ReadVec2(); var max = payload.ReadVec2(); _boxes.Add(new AxisAlignedBB(min, max)); } }
public UdpPacketEventArgs(IPEndPoint from, ByteBufferReader byteBuffer) : base(byteBuffer) { From = from; }
private void UpdatePlayer(PlayerDummy player, ByteBufferReader data) { player.SetPos(data.ReadVec2()); player.Rotation = data.ReadVec2(); }
public TcpPacketEventArgs(TcpClient from, ByteBufferReader byteBuffer) : base(byteBuffer) { From = from; }