public void TestReadWriteTwstat() { var stream = new MemoryStream(); var qid = new Qid((byte)QidType.QtFile, 1, 0x111L); var stat = new Stat( 1, 2, qid, 4, 5, 6, 65535L, "foo", "root", "root", "root"); var message = new Twstat(1, stat) { Tag = 1254 }; var p = new Protocol.Protocol(stream); p.Write(message); stream.Position = 0; var data = new byte[message.Length]; var length = stream.Read(data, 0, (int)message.Length); Assert.That(length, Is.EqualTo(message.Length)); stream.Position = 0; var message2 = (Twstat)p.Read(); Assert.That(message, Is.EqualTo(message2)); }
public void RecieveProtocol() { IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); try { while (true) { #if DEBUG Console.WriteLine("Waiting for protocol"); #endif byte[] bytes = _Listener.Receive(ref remoteIpEndPoint); string data = Encoding.ASCII.GetString(bytes, 0, bytes.Length); #if DEBUG Console.WriteLine($"Data received: {data}"); #endif Protocol.Protocol protocol = JsonConvert.DeserializeObject <Protocol.Protocol>(data); ProtocolReceived?.Invoke(new ProtocolReceivedArguments(protocol, remoteIpEndPoint.Address.ToString(), remoteIpEndPoint.Port)); } } catch (SocketException e) { Console.WriteLine(e); } }
/// <summary> /// Collect data and provides. /// </summary> /// <param name="type"></param> private void Broadcast(Protocol.Type type = Protocol.Type.TYPE_UPDATE) { // Create snapshot. Protocol.Protocol protocol = new Protocol.Protocol { Type = type, Length = _Length }; foreach (ExtendedPlayer player in _Players) { if (player.Jumping > 0) { player.Player.Position.Jumping = true; } else { player.Player.Position.Jumping = false; } protocol.Players.Add(player.Player); } // Provide for broadcasting SnapshotCreated?.Invoke(new SnapshotArguments(protocol)); }
public void TestReadWriteTwrite() { var stream = new MemoryStream(); var utf8 = new UTF8Encoding(); var message = new Twrite( 1, 65535L, (uint)utf8.GetByteCount("test"), utf8.GetBytes("test")) { Tag = 1248 }; var p = new Protocol.Protocol(stream); p.Write(message); stream.Position = 0; var data = new byte[message.Length]; var length = stream.Read(data, 0, (int)message.Length); Assert.That(length, Is.EqualTo(message.Length)); stream.Position = 0; var message2 = (Twrite)p.Read(); Assert.That(message, Is.EqualTo(message2)); }
public void TestReadWriteTcreate() { var stream = new MemoryStream(); var message = new Tcreate(1, "test", 1, 1) { Tag = 1244 }; var p = new Protocol.Protocol(stream); p.Write(message); stream.Position = 0; var data = new byte[message.Length]; var length = stream.Read(data, 0, (int)message.Length); Assert.That(length, Is.EqualTo(message.Length)); stream.Position = 0; var message2 = (Tcreate)p.Read(); Console.WriteLine(message.ToString()); Console.WriteLine(message2.ToString()); Assert.That(message, Is.EqualTo(message2)); }
/// <summary> /// Construct the nexus /// </summary> public Nexus() { _Protocol = new Protocol.Protocol(Configuration.ProtocolPort(), this); _Search = new Search.Search(); _Peers = new PeerList(); _Multicast = new Multicast.Multicast(this, true, true); _UsedPorts.Add(8000); }
/// <summary> /// Process action. /// </summary> /// <param name="protocol"></param> public void ProcessInput(Protocol.Protocol protocol, int playerId) { // Get player ExtendedPlayer player = _Players.FirstOrDefault(p => p.Player.Id == playerId); switch (protocol.Action) { case Protocol.Action.ACT_DOWN: if (player.Direction == Direction.Up) { break; } player.Direction = Direction.Down; break; case Protocol.Action.ACT_JUMP: if (player.Jumping == 0) { player.Jumping = _JumpLength; player.Player.Position.Jumping = true; } break; case Protocol.Action.ACT_LEFT: if (player.Direction == Direction.Right) { break; } player.Direction = Direction.Left; break; case Protocol.Action.ACT_RIGHT: if (player.Direction == Direction.Left) { break; } player.Direction = Direction.Right; break; case Protocol.Action.ACT_UP: if (player.Direction == Direction.Down) { break; } player.Direction = Direction.Up; break; } }
public void TestReadWriteRclunk() { var stream = new MemoryStream(); var message = new Rclunk { Tag = 1251 }; var p = new Protocol.Protocol(stream); p.Write(message); stream.Position = 0; var data = new byte[message.Length]; var length = stream.Read(data, 0, (int)message.Length); Assert.That(length, Is.EqualTo(message.Length)); stream.Position = 0; var message2 = (Rclunk)p.Read(); Assert.That(message, Is.EqualTo(message2)); }
public void TestReadWriteTwalk() { var stream = new MemoryStream(); var message = new Twalk(3, 4, 2, new[] { "hello", "world" }) { Tag = 1240 }; var p = new Protocol.Protocol(stream); p.Write(message); stream.Position = 0; var data = new byte[message.Length]; var length = stream.Read(data, 0, (int)message.Length); Assert.That(length, Is.EqualTo(message.Length)); stream.Position = 0; var message2 = (Twalk)p.Read(); Assert.That(message, Is.EqualTo(message2)); }
public void TestReadWriteTversion() { var stream = new MemoryStream(); var message = new Tversion(16384, "9P2000") { Tag = 0 }; var p = new Protocol.Protocol(stream); p.Write(message); stream.Position = 0; var data = new byte[message.Length]; var length = stream.Read(data, 0, (int)message.Length); Assert.That(length, Is.EqualTo(message.Length)); stream.Position = 0; var message2 = (Tversion)p.Read(); Assert.That(message2, Is.EqualTo(message)); }
public void TestReadWriteTauth() { var stream = new MemoryStream(); var message = new Tauth(Constants.NoFid, "user", "tree") { Tag = 1234 }; var p = new Protocol.Protocol(stream); p.Write(message); stream.Position = 0; var data = new byte[message.Length]; var length = stream.Read(data, 0, (int)message.Length); Assert.That(length, Is.EqualTo(message.Length)); stream.Position = 0; var message2 = (Tauth)p.Read(); Assert.That(message2, Is.EqualTo(message)); }
public void TestReadWriteTattach() { var stream = new MemoryStream(); var message = new Tattach(1, 2, "uname", "aname") { Tag = 1236 }; var p = new Protocol.Protocol(stream); p.Write(message); stream.Position = 0; var data = new byte[message.Length]; var length = stream.Read(data, 0, (int)message.Length); Assert.That(length, Is.EqualTo(message.Length)); stream.Position = 0; var message2 = (Tattach)p.Read(); Assert.That(message, Is.EqualTo(message2)); }
public void TestReadWriteRauth() { var stream = new MemoryStream(); var qid = new Qid((byte)QidType.QtFile, 1, 0x111L); var message = new Rauth(qid) { Tag = 1235 }; var p = new Protocol.Protocol(stream); p.Write(message); stream.Position = 0; var data = new byte[message.Length]; var length = stream.Read(data, 0, (int)message.Length); Assert.That(length, Is.EqualTo(message.Length)); stream.Position = 0; var message2 = (Rauth)p.Read(); Assert.That(qid, Is.EqualTo(message2.Aqid)); }
public void HandleConnection() { if (_Terminate) { return; } TcpClient client = threadListener.AcceptTcpClient(); client.NoDelay = true; NetworkStream ns = client.GetStream(); connections++; Console.WriteLine($"New client accepted: {connections} active connections"); int bytesToRead = 0, nextReadCount = 0, rc = 0; byte[] byteCount = BitConverter.GetBytes(1024); byte[] receiveBuffer = new byte[4096]; try { string data = string.Empty; while (true) { bytesToRead = BitConverter.ToInt32(byteCount, 0); // Receive the data while (bytesToRead > 0) { if (!ns.CanRead) { break; } // Make sure we don't read beyond what the first message indicates // This is important if the client is sending multiple "messages" -- // but in this sample it sends only one if (bytesToRead < receiveBuffer.Length) { nextReadCount = bytesToRead; } else { nextReadCount = receiveBuffer.Length; } // Read some data try { rc = ns.Read(receiveBuffer, 0, nextReadCount); } catch (ObjectDisposedException) { ns.Close(); ConnectionLost?.Invoke(new ConnectionLostArguments(client.Client.RemoteEndPoint.ToString(), this)); _Terminate = true; return; } catch (IOException) { ns.Close(); ConnectionLost?.Invoke(new ConnectionLostArguments(client.Client.RemoteEndPoint.ToString(), this)); _Terminate = true; return; } // Detect if client disconnected if (client.Client.Poll(0, SelectMode.SelectRead)) { try { byte[] buff = new byte[1]; if (client.Client.Receive(buff, SocketFlags.Peek) == 0) { // Client disconnected Console.Error.WriteLineAsync($"Host {client.Client.RemoteEndPoint.ToString()} disconected!"); ConnectionLost?.Invoke(new ConnectionLostArguments(client.Client.RemoteEndPoint.ToString(), this)); break; } } catch (SocketException s) { // Client disconnected Console.Error.WriteLineAsync($"Host {client.Client.RemoteEndPoint.ToString()} disconected!"); #if DEBUG Console.Error.WriteLineAsync($"Message: {s.ToString()}"); #endif ConnectionLost?.Invoke(new ConnectionLostArguments(client.Client.RemoteEndPoint.ToString(), this)); _Terminate = true; return; } } data += Encoding.UTF8.GetString(receiveBuffer, 0, rc); if (data.Contains(Environment.NewLine)) { string[] parts = data.Split(Environment.NewLine); data = parts[0]; if (TronServer.Security.AES.HasClient(client)) { data = data.Replace(Environment.NewLine, ""); byte[] dataCipher = Convert.FromBase64String(data); data = TronServer.Security.AES.Decrypt(dataCipher, client); #if DEBUG Console.WriteLine($"Received from {client.Client.RemoteEndPoint}: {data}"); #endif Protocol.Protocol protocol = null; try { protocol = JsonConvert.DeserializeObject <Protocol.Protocol>(data); } catch (Exception ex) { protocol = null; Console.Error.WriteLineAsync($"JSON deserialize ERROR: '{ex.Message}'."); } if (protocol != null) { ProtocolRecievedArguments protocolRecievedArguments = new ProtocolRecievedArguments(protocol, client); ProtocolRecieved?.Invoke(protocolRecievedArguments); } } else { HandshakeClient(client, data); } data = parts[1]; } bytesToRead -= rc; } if (rc == 0) { break; } } // Client disconnected Console.Error.WriteLineAsync($"Host {client.Client.RemoteEndPoint.ToString()} disconected!"); ConnectionLost?.Invoke(new ConnectionLostArguments(client.Client.RemoteEndPoint.ToString(), this)); } catch (ObjectDisposedException) { Console.WriteLine("Connection canceled!"); ConnectionLost?.Invoke(new ConnectionLostArguments(client.Client.RemoteEndPoint.ToString(), this)); } catch (Exception ex) { Console.Error.WriteLineAsync(ex.ToString()); ConnectionLost?.Invoke(new ConnectionLostArguments(client.Client.RemoteEndPoint.ToString(), this)); } finally { if (ns != null) { ns.Close(); } if (client != null) { client.Close(); } _Terminate = true; connections--; Console.WriteLine($"Client disconnected: {connections} active connections"); } }
public ProtocolReceivedArguments(Protocol.Protocol protocol, string ip, int port) { Protocol = protocol; Ip = ip; Port = port; }