public void sendMiniInfo(string ip, NetworkCarrierMini cin = null) { NetworkCarrierMini c; if (cin == null) { int[] ints = new int[this.game.players.Length]; for (int i = 0; i < this.game.players.Length; i++) { ints[i] = this.game.players[i].Score; } c = new NetworkCarrierMini(ints, this.game.desk.Desk); } else { c = cin; } newConnection(ip, "MINI"); try { this.formatter.Serialize(this.stream, c); return; } catch (Exception e) { Console.WriteLine(e.Message); } }
/// <summary> /// Update data in this class. Use client! /// </summary> public void networkUpdate(NetworkCarrierMini c) { lock (this.gameLock) { this.newData = true; for (int i = 0; i < this.players.Length; i++) { this.players[i].Score = c.scores[i]; } this.desk.Desk = c.desk; } }
private void Recive() { this.client = listener.AcceptTcpClient(); this.stream = this.client.GetStream(); this.buffer = new byte[4]; this.stream.Read(buffer, 0, buffer.Length); string mes = encoder.GetString(buffer); if (mes.StartsWith("FULL")) { #if DEBUG Console.WriteLine("Přijimam FULL update"); #endif NetworkCarrierFull c = ( NetworkCarrierFull )formatter.Deserialize(this.stream); this.game.networkUpdate(c); } if (mes.StartsWith("MINI")) { #if DEBUG Console.WriteLine("Přijimam MINI update"); #endif NetworkCarrierMini c = ( NetworkCarrierMini )formatter.Deserialize(this.stream); this.game.networkUpdate(c); } if (mes.StartsWith("MOVE")) { NetworkCarrierPlayer c = (NetworkCarrierPlayer)formatter.Deserialize(this.stream); lock (this.game.gameLock) { this.game.yourTurn = true; this.game.ncp = c; } while (true) { try { Thread.Sleep(Timeout.Infinite); } catch (ThreadInterruptedException) {} lock (this.game.gameLock) { if (this.game.turnDone) { this.game.turnDone = false; this.game.yourTurn = false; formatter.Serialize(this.stream, this.game.move); this.stream.Flush(); this.stream.Close(); break; } } } } if (mes.StartsWith("EXIT")) { this.listener.Stop(); try { this.stream.Close(); } catch {} try { this.client.Close(); } catch {} this.end = true; } }