private bool CheckForPrevClient(TcpClient prevClient) { TcpHandler.WriteMessage(prevClient, TcpProtocol.CheckForDisconnectSend()); int waitTimeInMillis = 500; bool noResponsePrevClient = false; long startTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; while (prevClient.Available <= 0) { long currentTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; if (currentTime - startTime >= waitTimeInMillis) { noResponsePrevClient = true; break; } } if (noResponsePrevClient) { return(false); } TcpHandler.ReadMessage(prevClient); return(true); }
public void StartGame() { IPAddress ip = IPAddress.Parse("127.0.0.1"); // IPAddress ip = IPAddress.Parse("145.49.59.202"); client = new TcpClient(ip.ToString(), 6963); while (client.Available <= 0) { if (Disconnected) { Console.WriteLine(""); return; } } string received = TcpHandler.ReadMessage(client); JObject jObject = JObject.Parse(received); string command = (string)jObject["command"]; Tuple <int, GameData> tuple; if (command != "disconnect/send") { tuple = Tuple.Create((int)jObject["id"], JsonConvert.DeserializeObject <GameData>((string)jObject["data"])); } else { TcpHandler.WriteMessage(client, new JObject()); tuple = ReadId(); } SendName(); id = tuple.Item1; Disconnected = false; gameData = tuple.Item2; draw.GameData = gameData; }
public void Write(JObject message) { TcpHandler.WriteMessage(client, message); }
private void SendName() { TcpHandler.WriteMessage(client, TcpProtocol.NameSend(form.Name, form.Color)); }
private void SendDirection() { TcpHandler.WriteMessage(client, TcpProtocol.DirectionSend(GetSnake(id).Direction)); }