public void SendRemoteCommand(string commandId) { // To hold the command (all remote commands are exactly 8 bytes long) byte[] command = new byte[8]; // Send commandId to RemoteCommander, which applies the appropriate data and header and returns it as a byte array, ready for sending RemoteCommander commander = new RemoteCommander(); commander.SetCommand(commandId); command = commander.GetCommand(); // Send command byte array with RemoteConnection // Each command is sent over an independent TCP stream RemoteConnection remoteConnection = new RemoteConnection(); remoteConnection.Connect(); remoteConnection.Write(command); remoteConnection.Close(); }
private void MainForm_KeyDown(object sender, KeyEventArgs e) { if (Server != null) { if (e.KeyData == Keys.Up && SelectedMenuIndex > 0) { SelectedMenuIndex--; } if (e.KeyData == Keys.Down && SelectedMenuIndex < 1) { SelectedMenuIndex++; } if (e.KeyData == Keys.Enter) { switch (SelectedMenuIndex) { case 0: // start a remote game this.Event = Event.CreateEventSchedule(this.Server.ConnectedPlayers); this.StartNextGame(); this.Timer.Interval = TurnTickInterval; this.OverrideMessage = "Starting Hosted Game"; this.DrawOverrideMessageScreen(); this.OverrideMessage = null; break; case 1: // abort this.Server.StopListening(); this.Server = null; this.SelectedMenuIndex = 0; this.DrawMainMenu(); break; } } } else // main menu { if (e.KeyData == Keys.Up && SelectedMenuIndex > 0) { SelectedMenuIndex--; } if (e.KeyData == Keys.Down && SelectedMenuIndex < 4) { SelectedMenuIndex++; } if (e.KeyData == Keys.Enter) { if (LocalGameOpponents != null) // if we're selecting opponents { switch (SelectedMenuIndex) { case 0: AddRemoveClassFromLocalOpponents <RandomCaptain>(); break; case 1: AddRemoveClassFromLocalOpponents <SimpleCaptain>(); break; case 2: AddRemoveClassFromLocalOpponents <Nelson>(); break; case 3: this.Server = null; this.OverrideMessage = "Starting Local Game"; this.DrawOverrideMessageScreen(); this.OverrideMessage = null; this.Event = Event.CreateLocalGame(this.LocalGameOpponents); this.StartNextGame(); this.Timer.Interval = TurnTickInterval; this.Timer.Start(); break; case 4: this.LocalGameOpponents = null; this.SelectedMenuIndex = 0; this.RefreshScreen(); break; } } else // otherwise it's the main menu { switch (SelectedMenuIndex) { case 0: // play locally this.LocalGameOpponents = new List <ICaptain>(); this.RefreshScreen(); break; case 1: // connect to server try { var server = "http://localhost:5999"; // the server name should be editable RemoteCommander.RegisterCaptain(server); this.Client = new Client(server); this.Client.PlayGame(); // TODO: we need to disconnect the listener when the game ends or we'll have a problem this.OverrideMessage = "Playing Remote Game"; this.RefreshScreen(); } catch (Exception) { MessageBox.Show("Unable to connect to remote server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } break; case 2: // host server this.Server = new Server(); this.SelectedMenuIndex = 0; this.Timer.Interval = 25; this.DrawServerScreen(); break; default: this.Client?.EndGame(); // last ditch in case we've not shut things down properly this.Server?.StopListening(); this.Close(); break; } } } } }