private void periodicMessageCheck(object obj, EventArgs evt) { //Recieve messages from server msg = tcp.receiveMessage(); switch (msg.msgID) { case 0: //0 byte messages break; case 2: //re-instantiate array of conected clients decoder.resetConnectedClients(msg); Console.WriteLine("Waiting for players"); break; case 3: //wait for incoming list of connected clients decoder.getPlayers(msg); updateAvailableClients(); Console.WriteLine("Player recieved: " + ConnectedClients.Last().Alias); break; case 5: //display message accept/decline invitation Console.WriteLine("Recieved invitation"); decoder.invitationAcceptReject(msg); break; case 7: //start game on client decoder.startGameSession(msg); break; } }
public Messages.MESSAGE DeSerialize(byte[] received) { IntPtr ip = Marshal.AllocHGlobal(received.Length); //allocate unmanaged memory Marshal.Copy(received, 0, ip, received.Length); //copy the byte[] to the unmanaged memory Messages.MESSAGE structure = (Messages.MESSAGE)Marshal.PtrToStructure(ip, typeof(Messages.MESSAGE)); //Marshal the unmanaged memory contents to the Message_PDU structure Marshal.FreeHGlobal(ip); //free unmanaged memory return(structure); }
//check for recieved messages private void recMessages(object obj, EventArgs evt) { msg = TCP.receiveMessage(); switch (msg.msgID) { case 8: panel_tableLayout.Enabled = turn; turnTimer.Start(); break; } }
public MainScreen(Client client) { InitializeComponent(); this.myDetails = client; lb_welcome.Text = "Welcome " + client.Alias; msg = new Messages.MESSAGE(); Network._IP = "192.168.56.1"; Network._ConnPort = client.ConnectionPort; tcp = Network.getNetwork(); ConnectedClients = new List <Client>(); decoder = new Messages(ConnectedClients); decoder.MainScreen = this; tcp.connectToServer(); decoder.initialiseClient(client); //timer set up timer = new Timer(); timer.Tick += new EventHandler(periodicMessageCheck); timer.Interval = 100; timer.Enabled = true; this.FormClosed += new FormClosedEventHandler(MainClosed); }
public Messages.MESSAGE receiveMessage() { Messages.MESSAGE msg = new Messages.MESSAGE(); try { byte[] recBuffer = new byte[1024]; //Initialise Buffer for recived messages int recByteCount; recByteCount = tcp_Socket.Receive(recBuffer, SocketFlags.None); byte[] data = new byte[recByteCount]; Array.Copy(recBuffer, data, recByteCount); if (recByteCount > 0) { return(msg = DeSerialize(data)); } else { msg.msgID = 0; } } catch (SocketException se) // Handle socket-related exception { // If an exception occurs, display an error message if (10053 == se.ErrorCode || 10054 == se.ErrorCode) // Remote end closed the connection { closeConnection(); } else if (10035 != se.ErrorCode) { // Ignore error messages relating to normal behaviour of non-blocking sockets Console.Write(se.Message); } } catch // Silently handle any other exception { Console.WriteLine("Server Lost"); } return(msg); }
private void MainClosed(object obj, EventArgs evt) { Messages.MESSAGE m = new Messages.MESSAGE(); m.msgID = -1; tcp.sendMessage(msg); }
public void sendMessage(Messages.MESSAGE msg) { byte[] byteData = Serialize(msg); tcp_Socket.Send(byteData, SocketFlags.None); }