/* * public static void SetupSockets() * { * int clientCount = 0; * * IPHostEntry ipHostInfo = Dns.GetHostEntry(""); * IPAddress ipAddress = ipHostInfo.AddressList[0]; * IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 1337); * * Socket listener = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); * try * { * listener.Bind(localEndPoint); * listener.Listen(10); * * // Start listening for connections. * while (true) * { * Console.WriteLine("Waiting for a connection..."); * // Program is suspended while waiting for an incoming connection. * Socket handler = listener.Accept(); * Console.WriteLine(" Connection recieved, spinning up ConnectClient thread"); * CheckersGM gameBoard; * CheckersGM.Player playerColor = CheckersGM.Player.NULL; * if (clientCount % 2 == 0) * { * gameBoard = new CheckersGM(); * games.Add(gameBoard); * Console.WriteLine(" Even connected client count, creating new gameboard"); * playerColor = CheckersGM.Player.PLAYER_BLACK; * } * else * { * gameBoard = games.Last(); * Console.WriteLine(" Odd connected client count, matching to last gameboard"); * playerColor = CheckersGM.Player.PLAYER_RED; * } * string clientString = clientCount.ToString() + " " + playerColor; * ConnectedClient client = new ConnectedClient(handler, gameBoard, playerColor, clientString); * * if (clientCount % 2 == 1) * { * var otherPlayerClient = clients[clientCount - 1]; * client.otherPlayer = otherPlayerClient; * otherPlayerClient.otherPlayer = client; * Console.WriteLine(" Two players connected in same game, setting up ConnectedClient's otherPlayers"); * } * * clients.Add(client); * clientCount++; * } * } * catch (Exception e) * { * Console.WriteLine(e.ToString()); * } * * Console.WriteLine("\nServer ending, press enter to close terminal."); * Console.Read(); * } */ public static void SetupSockets() { int clientCount = 0; try { IPAddress ipAd = Array.FindLast(Dns.GetHostEntry(string.Empty).AddressList, a => a.AddressFamily == AddressFamily.InterNetwork); TcpListener tcp = new TcpListener(ipAd, 1337); tcp.Start(); Console.WriteLine("The server is running at port 1337..."); Console.WriteLine("The local End point is: " + tcp.LocalEndpoint); Console.WriteLine("Waiting for a connection..."); while (true) { Socket s = tcp.AcceptSocket(); Console.WriteLine("Connection recieved from {0}, spinning up ConnectClient thread", s.RemoteEndPoint); CheckersGM gameBoard; Player playerColor = Player.NULL; if (clientCount % 2 == 0) { gameBoard = new CheckersGM(); games.Add(gameBoard); Console.WriteLine("Even connected client count. Creating new gameboard"); playerColor = Player.PLAYER_BLACK; } else { gameBoard = games.Last(); Console.WriteLine("Odd connected client count. Matching to last gameboard"); playerColor = Player.PLAYER_RED; } string clientString = clientCount.ToString() + " " + playerColor; ConnectedClient client = new ConnectedClient(s, gameBoard, playerColor, clientString); if (clientCount % 2 == 1) { var otherPlayerClient = clients[clientCount - 1]; client.otherPlayer = otherPlayerClient; otherPlayerClient.otherPlayer = client; Console.WriteLine("Two players connected in same game, setting up ConnectedClient's otherPlayers"); } clients.Add(client); clientCount++; } } catch (Exception e) { Console.WriteLine("Error..... " + e.StackTrace); } }
internal ConnectedClient(Socket inClient, CheckersGM gameBoard, Player playerColor, string clientString) { mySocket = inClient; this.gameBoard = gameBoard; this.playerColor = playerColor; this.clientString = clientString; Thread connection = new Thread(DoConnection); connection.Start(); }
private CheckersGM GetBoardState() { NetworkStream serverStream = new NetworkStream(clientSocket); byte[] messageLengthBytes = new byte[4]; serverStream.Read(messageLengthBytes, 0, messageLengthBytes.Length); Thread.Sleep(100); var messageLength = BitConverter.ToUInt32(messageLengthBytes, 0); var inStream = new byte[messageLength]; serverStream.Read(inStream, 0, inStream.Length); Thread.Sleep(100); var gmBoardState = CheckersGM.FromBytes(inStream); return(gmBoardState); }
private void KeepBoardUpdated() { while (clientSocket != null && clientSocket.Connected) { try { _game = GetBoardState(); Update(_game); if (this.gameOver) { this._game = null; this.gameOver = false; break; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } }
public void Update(CheckersGM newState) { this._game = newState; UpdateText(); Node[,] sparseArray = this._game.SparseArray; if (sparseArray == null) { // TODO: Handle this in a better manner String _err = "Malformed sparse array sent to Form1.update"; MessageBox.Show(String.Format("Error: {0}", _err)); // throw new ArgumentException(_err); } if (_flip) { //Debug.WriteLine(DebugPrintSparseArrayLOCAL(sparseArray)); int rs = sparseArray.GetLength(0) - 1, cs = sparseArray.GetLength(1) - 1; Node[,] temp = new Node[rs + 1, cs + 1]; for (Int32 row = 0; row <= rs; row++) { for (Int32 col = 0; col <= cs; col++) { temp[row, col] = sparseArray[rs - row, cs - col]; } } sparseArray = temp; //Debug.Write("\n--------------------------\n\n"); //Debug.WriteLine(DebugPrintSparseArrayLOCAL(sparseArray)); } for (int row = 0; row < this.board.RowCount; row++) { int sparseCol = 0; for (int col = 0; col < this.board.ColumnCount; col++) { if ((col - (row % 2 == 0 ? 1 : 0)) % 2 != 0) { continue; } Control cell = this.board.GetControlFromPosition(col, row); if (cell == null) { continue; // Wut? } State cellState = (sparseArray[sparseCol, row]).GetState(); Image background = null; switch (cellState) { case State.RED: background = image_pieceRed; break; case State.KING_RED: background = image_kingRed; break; case State.BLACK: background = image_pieceBlack; break; case State.KING_BLACK: background = image_kingBlack; break; default: // Implied: State.EMPTY background = null; break; } cell.BackgroundImage = background; //cell.Invoke((MethodInvoker)(() => { BackgroundImage = background; })); sparseCol++; } } GameState state = _game.GetCurrentGameState(); if (state == GameState.BLACK_WIN || state == GameState.RED_WIN) { String message = "Game over! You {0}!"; if ((state == GameState.BLACK_WIN && _myColor == Player.PLAYER_BLACK) || (state == GameState.RED_WIN && _myColor == Player.PLAYER_RED)) { message = String.Format(message, "won"); } else { message = String.Format(message, "lost"); } MessageBox.Show(message, caption: "Game Over"); this.clientSocket.Disconnect(false); this.clientSocket = null; clearBoard(); this.gameOver = true; } }