/// <summary> /// Sends reports about the given line states to all connected clients and to the listener of this server. /// </summary> private void SendLineStateReports(LobbyLineState[] lineStates) { for (int i = 0; i < this.connections.Length; i++) { if (this.connections[i].ConnectionState == LobbyConnectionState.Connected) { RCPackage lineStateReport = RCPackage.CreateNetworkControlPackage(Network.FORMAT_LOBBY_LINE_STATE_REPORT); lineStateReport.WriteShort(0, (short)(i + 1)); /// The ID of the client that receives the report. byte[] lineStatesBytes = new byte[lineStates.Length]; for (int j = 0; j < lineStatesBytes.Length; j++) { lineStatesBytes[j] = (byte)lineStates[j]; } lineStateReport.WriteByteArray(1, lineStatesBytes); /// The list of the line states on this server. /// Send the package if (!this.connections[i].SendPackage(lineStateReport)) { /// In case of error, shutdown the connection and notify the other clients again. this.connections[i].Shutdown(); this.connections[i].LineState = LobbyLineState.Opened; /// Keep it opened for other clients. SendLineStateReports(); } } } this.listener.LineStateReport(0, lineStates); }
/// <summary> /// Sends a DSS_LEAVE message to the other side of this session. /// </summary> /// <param name="reason">The reason of leaving the DSS.</param> /// <param name="customData">Custom data about leaving the DSS.</param> public void SendLeaveMsg(string reason, byte[] customData) { if (this.sm.CurrentState == this.SendingSetupStepRQ) { RCPackage leaveMsg = RCPackage.CreateNetworkControlPackage(DssRoot.DSS_LEAVE); leaveMsg.WriteString(0, reason); leaveMsg.WriteByteArray(1, customData); this.manager.HostRoot.Lobby.SendControlPackage(leaveMsg, this.channel.Index + 1); } }
/// <summary> /// Call this function to initialize the UI for the setup stage and send reset messages to the connected guests. /// </summary> private void FirstSetupStep(IDssGuestChannel[] channelsToGuests) { /// Reset the UI this.simulator.GetPlayer(0).Reset(); this.simulator.GetPlayer(0).Activate(); this.uiCallMarshal.SetMainControlStatus(FormStatus.HostSide); this.uiCallMarshal.SelectNewHostColor(this.simulator.GetPlayer(0).Color); this.uiCallMarshal.SetHostControlStatus(HostControlStatus.AccessGranted); this.previousChannelStates = new DssChannelState[channelsToGuests.Length]; for (int i = 0; i < this.previousChannelStates.Length; i++) { this.simulator.GetPlayer(i + 1).Reset(); this.previousChannelStates[i] = channelsToGuests[i].ChannelState; if (channelsToGuests[i].ChannelState == DssChannelState.CHANNEL_OPENED) { this.uiCallMarshal.SetGuestControlStatus(i, GuestControlStatus.HostSideOpened); } else if (channelsToGuests[i].ChannelState == DssChannelState.CHANNEL_CLOSED) { this.uiCallMarshal.SetGuestControlStatus(i, GuestControlStatus.HostSideClosed); } else if (channelsToGuests[i].ChannelState == DssChannelState.GUEST_CONNECTED) { this.simulator.GetPlayer(i + 1).Activate(); this.uiCallMarshal.SelectNewGuestColor(i, this.simulator.GetPlayer(i + 1).Color); this.uiCallMarshal.SetGuestControlStatus(i, GuestControlStatus.HostSideEngaged); } } /// Send reset messages for (int i = 0; i < channelsToGuests.Length; i++) { if (channelsToGuests[i].ChannelState == DssChannelState.GUEST_CONNECTED) { byte[] colors = new byte[this.simulator.MaxNumOfPlayers]; int[] xCoords = new int[this.simulator.MaxNumOfPlayers]; int[] yCoords = new int[this.simulator.MaxNumOfPlayers]; for (int j = 0; j < colors.Length; j++) { colors[j] = (byte)this.simulator.GetPlayer(j).Color; xCoords[j] = this.simulator.GetPlayer(j).Position.X; yCoords[j] = this.simulator.GetPlayer(j).Position.Y; } RCPackage reset = RCPackage.CreateNetworkControlPackage(TestClientMessages.RESET); reset.WriteByteArray(0, colors); reset.WriteIntArray(1, xCoords); reset.WriteIntArray(2, yCoords); this.rqs[i].Add(reset); } } }
/// <summary> /// Leaves the DSS. /// </summary> private void LeaveDss() { /// Send the DSS_LEAVE message to the host. RCPackage leavePackage = RCPackage.CreateNetworkControlPackage(DssRoot.DSS_LEAVE); leavePackage.WriteString(0, "Guest left DSS!"); leavePackage.WriteByteArray(1, new byte[0] { }); this.guestRoot.Lobby.SendControlPackage(leavePackage); /// Stop the timeout clocks and exit from the event-loop. StopTimeouts(); this.guestRoot.EventQueue.ExitEventLoop(); }
public bool ExecuteNextStep(IDssHostChannel channelToHost) { TraceManager.WriteAllTrace("SETUP_CALL:", TestConsoleTraceFilters.TEST_INFO); TraceManager.WriteAllTrace(string.Format("Guest index: {0}", channelToHost.GuestIndex), TestConsoleTraceFilters.TEST_INFO); TraceManager.WriteAllTrace("Request from host:", TestConsoleTraceFilters.TEST_INFO); RCPackage[] request = channelToHost.RequestFromHost; for (int i = 0; i < request.Length; i++) { TraceManager.WriteAllTrace(request[i], TestConsoleTraceFilters.TEST_INFO); } TraceManager.WriteAllTrace("Channel states:", TestConsoleTraceFilters.TEST_INFO); for (int i = 0; i < channelToHost.ChannelStates.Length; i++) { if (channelToHost.ChannelStates[i] == DssChannelState.GUEST_CONNECTED) { TraceManager.WriteAllTrace(string.Format("Channel-{0}: ENGAGED", i), TestConsoleTraceFilters.TEST_INFO); } else if (channelToHost.ChannelStates[i] == DssChannelState.CHANNEL_OPENED) { TraceManager.WriteAllTrace(string.Format("Channel-{0}: OPENED", i), TestConsoleTraceFilters.TEST_INFO); } else if (channelToHost.ChannelStates[i] == DssChannelState.CHANNEL_CLOSED) { TraceManager.WriteAllTrace(string.Format("Channel-{0}: CLOSED", i), TestConsoleTraceFilters.TEST_INFO); } } List <RCPackage> answer = new List <RCPackage>(); while (true) { string input = Console.ReadLine(); if (input.CompareTo("Continue") == 0) { channelToHost.AnswerToHost = answer.ToArray(); return(true); } else if (input.CompareTo("Leave") == 0) { return(false); } else { RCPackage package = RCPackage.CreateNetworkControlPackage(Program.MY_FORMAT); package.WriteString(0, input); answer.Add(package); } } }
/// <summary> /// Called when we have successfully connected to the lobby. /// </summary> public void LobbyIsRunning() { /// We send a DSS_CTRL_CONN_REQUEST package. RCPackage reqPackage = RCPackage.CreateNetworkControlPackage(DssRoot.DSS_CTRL_CONN_REQUEST); reqPackage.WriteInt(0, DssRoot.APPLICATION_VERSION.Major); reqPackage.WriteInt(1, DssRoot.APPLICATION_VERSION.Minor); reqPackage.WriteInt(2, DssRoot.APPLICATION_VERSION.Build); reqPackage.WriteInt(3, DssRoot.APPLICATION_VERSION.Revision); this.guestRoot.Lobby.SendControlPackage(reqPackage); /// Then we fire the corresponding trigger at the session state machine. this.Start_WaitingConnectionACK.Fire(); this.controller.ExecuteFirings(); }
/// <summary> /// Creates an answer to the current setup step at guest side. /// </summary> /// <param name="outgoingPackages">The outgoing packages sent by the client module.</param> /// <returns>List of the packages of the outgoing setup step answer.</returns> /// <remarks>This function must be called at guest side.</remarks> public RCPackage[] CreateAnswer(RCPackage[] outgoingPackages) { if (this.mode != DssMode.GUEST_SIDE) { throw new DssException("This call is only allowed at guest side!"); } if (this.state != SetupStepState.READY) { throw new DssException("You can only create answer in READY state!"); } if (outgoingPackages == null) { throw new ArgumentNullException("outgoingPackages"); } int numPackages = outgoingPackages.Length + 2; /// BEGIN, END RCPackage[] retList = new RCPackage[numPackages]; for (int i = 0; i < numPackages; i++) { if (i == 0) /// BEGIN { retList[i] = RCPackage.CreateNetworkControlPackage(DssRoot.DSS_CTRL_SETUP_STEP_AW_BEGIN); retList[i].WriteInt(0, this.stepID); } else if (i == numPackages - 1) /// END { retList[i] = RCPackage.CreateNetworkControlPackage(DssRoot.DSS_CTRL_SETUP_STEP_MSG_END); retList[i].WriteInt(0, this.stepID); } else /// Package from the client module { if (outgoingPackages[i - 1] != null && outgoingPackages[i - 1].IsCommitted && outgoingPackages[i - 1].PackageType == RCPackageType.NETWORK_CONTROL_PACKAGE && !DssRoot.IsInternalFormat(outgoingPackages[i - 1].PackageFormat)) { retList[i] = outgoingPackages[i - 1]; } else { throw new DssException("Outgoing setup package format error!"); } } } /// end-for return(retList); }
/// <summary> /// Internal function to send a DSS_CTRL_DROP_GUEST message to the other side of this session. /// </summary> public void DropGuest() { if (this.sm.CurrentState == this.SendingSetupStepRQ) { RCPackage dropGuestMsg = RCPackage.CreateNetworkControlPackage(DssRoot.DSS_CTRL_DROP_GUEST); dropGuestMsg.WriteString(0, "Dropped from the DSS by the host."); dropGuestMsg.WriteByteArray(1, new byte[0] { }); /// Send the DSS_CTRL_DROP_GUEST message to the guest and close the line immediately this.manager.HostRoot.Lobby.SendControlPackage(dropGuestMsg, this.channel.Index + 1); this.manager.HostRoot.Lobby.CloseLine(this.channel.Index + 1); /// Move the session back to inactive state. this.SendingSetupStepRQ_Inactive.Fire(); } else { throw new DssException("Illegal call to DssHostSessionSM.DropGuest"); } }
/// <summary> /// Processes the actions arrived from the UI. /// </summary> private bool ProcessUiActions(IDssHostChannel channelToHost) { UiActionType[] uiActions = null; int[] firstParams = null; int[] secondParams = null; this.ActionQueue.GetAllActions(out uiActions, out firstParams, out secondParams); for (int i = 0; i < uiActions.Length; i++) { if (uiActions[i] == UiActionType.NewColorSelected) { /// Send the request to the host and register that a color selection request is in progress. int opID = firstParams[i]; if (!this.newColorSelected && opID - 1 == channelToHost.GuestIndex) { /// Send the request to the host. PlayerColor newColor = (PlayerColor)secondParams[i]; RCPackage colorChgRq = RCPackage.CreateNetworkControlPackage(TestClientMessages.COLOR_CHANGE_REQUEST); colorChgRq.WriteByte(0, (byte)newColor); this.aw.Add(colorChgRq); /// Register that a color selection request is in progress. this.newColor = newColor; this.newColorSelected = true; } } else if (uiActions[i] == UiActionType.LeaveBtnPressed) { /// TODO: notify the UI return(false); } else { /// Otherwise ignore the action } } return(true); }
private void btnTest_Click(object sender, EventArgs e) { int index = this.testButtonMap[(Button)sender]; if (this.joinedLobby == null && this.createdLobby != null && index < this.lastKnownLineStates.Length && index != this.lastKnownIdOfThisPeer && !this.connectingToLobby && this.lastKnownLineStates[index] == LobbyLineState.Engaged) { /// Create the package RCPackage package = RCPackage.CreateNetworkControlPackage(MY_FORMAT); package.WriteString(0, "INTERNAL MESSAGE: " + this.internalCount); this.createdLobby.SendControlPackage(package, index); } else if (this.joinedLobby != null && this.createdLobby == null && index == 0 && !this.connectingToLobby) { /// Create the package RCPackage package = RCPackage.CreateNetworkControlPackage(MY_FORMAT); package.WriteString(0, "INTERNAL MESSAGE: " + this.internalCount); this.joinedLobby.SendControlPackage(package); } this.internalCount++; }
/// <summary> /// Starts the disconnection procedure of this connection. /// </summary> /// <returns>True if the disconnection procedure has been successfully started, false otherwise.</returns> /// <remarks> /// If this function returns false that means that an underlying network error occured and the connection /// has been shutdown immediately. /// </remarks> public bool BeginDisconnect() { if (this.connectionState == LobbyConnectionState.Connected) { /// Send a disconnect indicator message to the other side. RCPackage disconnectIndicator = RCPackage.CreateNetworkControlPackage(Network.FORMAT_DISCONNECT_INDICATOR); disconnectIndicator.WriteString(0, string.Empty); disconnectIndicator.WriteByteArray(1, new byte[0] { }); if (!SendPackage(disconnectIndicator)) { /// Shutdown the connection immediately in case of any error. Shutdown(); return(false); } this.connectionState = LobbyConnectionState.Disconnecting; this.disconnectAckMsgTimer.Start(); return(true); } else { throw new NetworkingSystemException("Unexpected call to LobbyConnection.BeginDisconnect()!"); } }
/// <summary> /// Process all incoming messages arrived from all connections. /// </summary> private void ProcessIncomingMessages() { for (int i = 0; i < this.connections.Length; i++) { if (this.connections[i].ConnectionState == LobbyConnectionState.Connected) { /// The connection is in connected state --> it's incoming messages will be processed by the server List <RCPackage> incomingPackages = new List <RCPackage>(); if (this.connections[i].ReceiveIncomingPackages(ref incomingPackages)) { /// Process the incoming messages foreach (RCPackage package in incomingPackages) { if (package.PackageType == RCPackageType.NETWORK_CUSTOM_PACKAGE) { /// This is a custom message, forward it to every other clients package.Sender = i + 1; for (int j = 0; j < this.connections.Length; j++) { if (i != j && this.connections[j].ConnectionState == LobbyConnectionState.Connected) { if (!this.connections[j].SendPackage(package)) { /// Unable to forward the message to a client --> Shutdown the connection this.connections[j].Shutdown(); this.connections[j].LineState = LobbyLineState.Opened; /// Keep it opened for other clients. SendLineStateReports(); } } } /// Notify the listener object about the arrived package this.listener.PackageArrived(package, i + 1); } else if (package.PackageType == RCPackageType.NETWORK_CONTROL_PACKAGE && package.PackageFormat.ID == Network.FORMAT_DEDICATED_MESSAGE) { /// This is a dedicated message, forward only to the targets byte[] targets = package.ReadByteArray(0); /// List of the targets byte[] theMessageBytes = package.ReadByteArray(1); /// The embedded message int parsedBytes = 0; RCPackage theMessage = RCPackage.Parse(theMessageBytes, 0, theMessageBytes.Length, out parsedBytes); if (theMessage != null && theMessage.IsCommitted && theMessage.PackageType == RCPackageType.NETWORK_CUSTOM_PACKAGE) { /// The embedded message is OK --> forward it to the dedicated targets theMessage.Sender = i + 1; for (int j = 0; j < targets.Length; j++) { int target = targets[j]; if (target == 0) { /// This server is the target this.listener.PackageArrived(theMessage, i + 1); } else if (target - 1 >= 0 && target - 1 < this.connections.Length && target - 1 != i) { /// Another client is the target --> forward the message to it LobbyConnection targetConn = this.connections[target - 1]; if (targetConn.ConnectionState == LobbyConnectionState.Connected) { if (!targetConn.SendPackage(theMessage)) { /// Unable to forward the message to a target --> Shutdown the connection targetConn.Shutdown(); targetConn.LineState = LobbyLineState.Opened; /// Keep it opened for other clients. SendLineStateReports(); } } } } } else { /// The embedded message has unexpected format --> Shutdown the connection this.connections[i].Shutdown(); this.connections[i].LineState = LobbyLineState.Opened; /// Keep it opened for other clients. SendLineStateReports(); break; /// Stop processing the messages of the closed connection } } else if (this.connections[i].ConnectionState == LobbyConnectionState.Connected && package.PackageType == RCPackageType.NETWORK_CONTROL_PACKAGE && package.PackageFormat.ID == Network.FORMAT_DISCONNECT_INDICATOR) { /// The client at the other side of the connection wants to disconnect. /// Acknowledge this request and shutdown the connection. RCPackage disconnAck = RCPackage.CreateNetworkControlPackage(Network.FORMAT_DISCONNECT_ACK); disconnAck.WriteString(0, string.Empty); disconnAck.WriteByteArray(1, new byte[0] { }); this.connections[i].SendPackage(disconnAck); this.connections[i].Shutdown(); this.connections[i].LineState = LobbyLineState.Opened; /// Keep it opened for other clients. SendLineStateReports(); break; /// Stop processing the messages of the closed connection } else if (package.PackageType == RCPackageType.NETWORK_CONTROL_PACKAGE && package.PackageFormat.ID != Network.FORMAT_DEDICATED_MESSAGE && package.PackageFormat.ID != Network.FORMAT_DISCONNECT_ACK && package.PackageFormat.ID != Network.FORMAT_DISCONNECT_INDICATOR && package.PackageFormat.ID != Network.FORMAT_LOBBY_INFO && package.PackageFormat.ID != Network.FORMAT_LOBBY_INFO_VANISHED && package.PackageFormat.ID != Network.FORMAT_LOBBY_LINE_STATE_REPORT) { /// Custom internal message from a client --> notify the listener this.listener.ControlPackageArrived(package, i + 1); } else { /// Unexpected message from the current connection this.connections[i].Shutdown(); this.connections[i].LineState = LobbyLineState.Opened; /// Keep it opened for other clients. SendLineStateReports(); break; /// Stop processing the messages of the closed connection } } /// end-foreach (RCPackage package in incomingPackages) } else { /// In case of receive error, we shutdown the connection. this.connections[i].Shutdown(); this.connections[i].LineState = LobbyLineState.Opened; /// Keep it opened for other clients. SendLineStateReports(); } } else if (this.connections[i].ConnectionState == LobbyConnectionState.Disconnecting) { /// The connection is about to disconnect --> incoming messages will be handled by the connection itself if (this.connections[i].ContinueDisconnect()) { /// This connection remains closed because disconnection is initiated by the server. SendLineStateReports(); } } } /// end-for (int i = 0; i < this.connections.Length; i++) }
/// <summary> /// Processes the actions arrived from the UI. /// </summary> private DssSetupResult ProcessUiActions(IDssGuestChannel[] channelsToGuests) { PlayerColor currentColor = this.simulator.GetPlayer(0).Color; PlayerColor newColor = PlayerColor.White; bool newColorSelected = false; UiActionType[] uiActions = null; int[] firstParams = null; int[] secondParams = null; this.ActionQueue.GetAllActions(out uiActions, out firstParams, out secondParams); for (int i = 0; i < uiActions.Length; i++) { if (uiActions[i] == UiActionType.CloseBtnPressed) { int opID = firstParams[i]; if (channelsToGuests[opID - 1].ChannelState == DssChannelState.GUEST_CONNECTED) { if (this.simulator.GetPlayer(opID).IsActive) { this.simulator.GetPlayer(opID).Deactivate(); } /// Drop the guest if it is connected to the channel... channelsToGuests[opID - 1].DropGuest(false); } else { /// otherwise close the channel. channelsToGuests[opID - 1].CloseChannel(); } /// and notify the UI. this.uiCallMarshal.SetGuestControlStatus(opID - 1, GuestControlStatus.HostSideClosed); } else if (uiActions[i] == UiActionType.OpenBtnPressed) { int opID = firstParams[i]; if (channelsToGuests[opID - 1].ChannelState == DssChannelState.GUEST_CONNECTED) { if (this.simulator.GetPlayer(opID).IsActive) { this.simulator.GetPlayer(opID).Deactivate(); } /// Drop the guest if it is connected to the channel... channelsToGuests[opID - 1].DropGuest(true); } else { /// otherwise open the channel. channelsToGuests[opID - 1].OpenChannel(); } /// and notify the UI. this.uiCallMarshal.SetGuestControlStatus(opID - 1, GuestControlStatus.HostSideOpened); } else if (uiActions[i] == UiActionType.NewColorSelected) { int opID = firstParams[i]; if (opID == 0) { newColor = (PlayerColor)secondParams[i]; newColorSelected = true; for (int j = 0; j < channelsToGuests.Length; j++) { if (channelsToGuests[j].ChannelState == DssChannelState.GUEST_CONNECTED) { RCPackage colorChgNotif = RCPackage.CreateNetworkControlPackage(TestClientMessages.COLOR_CHANGE_NOTIFICATION); colorChgNotif.WriteInt(0, opID); colorChgNotif.WriteByte(1, (byte)newColor); this.rqs[j].Add(colorChgNotif); } } } /// Notify the UI //this.uiCallMarshal.SetHostControlStatus(HostControlStatus.AccessGranted); } else if (uiActions[i] == UiActionType.StartSimBtnPressed) { if (newColorSelected) { /// Set back the color in the combobox this.uiCallMarshal.SelectNewHostColor(currentColor); } return(DssSetupResult.START_SIMULATION); } else if (uiActions[i] == UiActionType.LeaveBtnPressed) { /// TODO: notify the UI return(DssSetupResult.LEAVE_DSS); } else { /// Otherwise ignore the action } } if (newColorSelected) { /// Save the selected new color this.simulator.GetPlayer(0).Color = newColor; } this.uiCallMarshal.SetHostControlStatus(HostControlStatus.AccessGranted); return(DssSetupResult.CONTINUE_SETUP); }
/// <summary> /// Processes the channel events and incoming answers arrived from the guests. /// </summary> private void ProcessGuestChannels(IDssGuestChannel[] channelsToGuests) { RCSet <int> newGuests = new RCSet <int>(); /// First collect the new guests for (int i = 0; i < this.previousChannelStates.Length; i++) { if (this.previousChannelStates[i] != channelsToGuests[i].ChannelState) { /// The state of a channel has changed this.previousChannelStates[i] = channelsToGuests[i].ChannelState; if (channelsToGuests[i].ChannelState == DssChannelState.CHANNEL_OPENED) { this.uiCallMarshal.SetGuestControlStatus(i, GuestControlStatus.HostSideOpened); } else if (channelsToGuests[i].ChannelState == DssChannelState.CHANNEL_CLOSED) { this.uiCallMarshal.SetGuestControlStatus(i, GuestControlStatus.HostSideClosed); } else if (channelsToGuests[i].ChannelState == DssChannelState.GUEST_CONNECTED) { this.uiCallMarshal.SetGuestControlStatus(i, GuestControlStatus.HostSideEngaged); this.simulator.GetPlayer(i + 1).Activate(); newGuests.Add(i); } } } /// Then process the answers of any other guests. for (int i = 0; i < this.previousChannelStates.Length; i++) { if (!newGuests.Contains(i) && channelsToGuests[i].ChannelState == DssChannelState.GUEST_CONNECTED) { /// If a guest is connected to this channel, process it's answer. RCPackage[] answerFromGuest = channelsToGuests[i].AnswerFromGuest; for (int j = 0; j < answerFromGuest.Length; j++) { if (answerFromGuest[j].PackageFormat.ID == TestClientMessages.COLOR_CHANGE_REQUEST) { /// Color change request arrived from the guest. PlayerColor newColor = (PlayerColor)answerFromGuest[j].ReadByte(0); this.simulator.GetPlayer(i + 1).Color = newColor; /// Notify the other connected guests. for (int k = 0; k < channelsToGuests.Length; k++) { if (!newGuests.Contains(k) && i != k && channelsToGuests[k].ChannelState == DssChannelState.GUEST_CONNECTED) { RCPackage colorChgNotif = RCPackage.CreateNetworkControlPackage(TestClientMessages.COLOR_CHANGE_NOTIFICATION); colorChgNotif.WriteInt(0, i + 1); colorChgNotif.WriteByte(1, (byte)newColor); this.rqs[k].Add(colorChgNotif); } } /// Notify the UI this.uiCallMarshal.SelectNewGuestColor(i, newColor); break; /// Ignore the remaining messages } } } } /// Send a reset message to the new guests foreach (int newGuestIdx in newGuests) { byte[] colors = new byte[this.simulator.MaxNumOfPlayers]; int[] xCoords = new int[this.simulator.MaxNumOfPlayers]; int[] yCoords = new int[this.simulator.MaxNumOfPlayers]; for (int j = 0; j < colors.Length; j++) { colors[j] = (byte)this.simulator.GetPlayer(j).Color; xCoords[j] = this.simulator.GetPlayer(j).Position.X; yCoords[j] = this.simulator.GetPlayer(j).Position.Y; } RCPackage reset = RCPackage.CreateNetworkControlPackage(TestClientMessages.RESET); reset.WriteByteArray(0, colors); reset.WriteIntArray(1, xCoords); reset.WriteIntArray(2, yCoords); this.rqs[newGuestIdx].Add(reset); } }
public DssSetupResult ExecuteNextStep(IDssGuestChannel[] channelsToGuests) { TraceManager.WriteAllTrace("SETUP_CALL:", TestConsoleTraceFilters.TEST_INFO); List <RCPackage>[] requests = new List <RCPackage> [channelsToGuests.Length]; for (int i = 0; i < requests.Length; i++) { requests[i] = new List <RCPackage>(); } while (true) { for (int i = 0; i < channelsToGuests.Length; i++) { if (channelsToGuests[i].ChannelState == DssChannelState.GUEST_CONNECTED) { TraceManager.WriteAllTrace(string.Format("SETUP_CALL: Channel-{0}: ENGAGED", i), TestConsoleTraceFilters.TEST_INFO); RCPackage[] answer = channelsToGuests[i].AnswerFromGuest; for (int j = 0; j < answer.Length; j++) { Console.WriteLine(" " + answer[j]); } } else if (channelsToGuests[i].ChannelState == DssChannelState.CHANNEL_OPENED) { TraceManager.WriteAllTrace(string.Format("SETUP_CALL: Channel-{0}: OPENED", i), TestConsoleTraceFilters.TEST_INFO); } else if (channelsToGuests[i].ChannelState == DssChannelState.CHANNEL_CLOSED) { TraceManager.WriteAllTrace(string.Format("SETUP_CALL: Channel-{0}: CLOSED", i), TestConsoleTraceFilters.TEST_INFO); } } string input = Console.ReadLine(); string[] inputTokens = input.Split(new char[1] { ' ' }); if (inputTokens.Length == 2) { if (inputTokens[0].CompareTo("CloseChannel") == 0) { channelsToGuests[int.Parse(inputTokens[1])].CloseChannel(); } else if (inputTokens[0].CompareTo("OpenChannel") == 0) { channelsToGuests[int.Parse(inputTokens[1])].OpenChannel(); } else if (inputTokens[0].CompareTo("DropAndClose") == 0) { channelsToGuests[int.Parse(inputTokens[1])].DropGuest(false); } else if (inputTokens[0].CompareTo("DropAndOpen") == 0) { channelsToGuests[int.Parse(inputTokens[1])].DropGuest(true); } else { int idx = int.Parse(inputTokens[1]); RCPackage package = RCPackage.CreateNetworkControlPackage(Program.MY_FORMAT); package.WriteString(0, input); requests[idx].Add(package); } } else if (inputTokens.Length == 1) { if (inputTokens[0].CompareTo("Continue") == 0) { for (int i = 0; i < channelsToGuests.Length; i++) { channelsToGuests[i].RequestToGuest = requests[i].ToArray(); } return(DssSetupResult.CONTINUE_SETUP); } else if (inputTokens[0].CompareTo("StartSim") == 0) { return(DssSetupResult.START_SIMULATION); } else if (inputTokens[0].CompareTo("Leave") == 0) { return(DssSetupResult.LEAVE_DSS); } } } }
/// <summary> /// The function sends every outgoing messages. /// </summary> /// <returns>False if the connection manager thread has to stop, true otherwise.</returns> private bool SendOutgoingMessages() { bool errorOccured = false; lock (this.outgoingPackages) { if (this.outgoingPackages.Count == this.outgoingPackageTargets.Count) { if (this.connection.ConnectionState == LobbyConnectionState.Connected) { for (int i = 0; i < this.outgoingPackages.Count; i++) { int[] targets = this.outgoingPackageTargets[i]; if (targets != null) { /// This is a dedicated message --> Send it as an embedded message. /// First collect the targets. List <byte> targetBytesList = new List <byte>(); for (int j = 0; j < targets.Length; j++) { if (targets[j] != this.clientID && targets[j] >= 0 && targets[j] < this.memberCount) { targetBytesList.Add((byte)targets[j]); } } /// Then create the dedicated message and send it to the server. byte[] targetBytes = targetBytesList.ToArray(); if (targetBytes != null && targetBytes.Length > 0) { byte[] packageBytes = new byte[this.outgoingPackages[i].PackageLength]; this.outgoingPackages[i].WritePackageToBuffer(packageBytes, 0); RCPackage dedicatedPackage = RCPackage.CreateNetworkControlPackage(Network.FORMAT_DEDICATED_MESSAGE); dedicatedPackage.WriteByteArray(0, targetBytes); /// The targets of the message. dedicatedPackage.WriteByteArray(1, packageBytes); /// The message itself. if (!this.connection.SendPackage(dedicatedPackage)) { /// Error when try to send --> immediate shutdown. this.connection.Shutdown(); //if (this.Disposed != null) { this.Disposed(this); } //this.listener.LobbyLost(); errorOccured = true; break; } } } else { /// This is a simple message. if (!this.connection.SendPackage(this.outgoingPackages[i])) { /// Error when try to send --> immediate shutdown. this.connection.Shutdown(); //if (this.Disposed != null) { this.Disposed(this); } //this.listener.LobbyLost(); errorOccured = true; break; } } } } /// Clear the FIFO this.outgoingPackages.Clear(); this.outgoingPackageTargets.Clear(); /// Outgoing messages sent successfully. ///return true; } else { throw new NetworkingSystemException("Inconsistence in the outgoing package FIFO!"); } } if (errorOccured) { /// An error happened during the send. if (this.Disposed != null) { this.Disposed(this); } this.listener.LobbyLost(); return(false); } else { /// Outgoing messages sent successfully. return(true); } }
/// <summary> /// This function reads and processes every incoming message arriving from the server. /// </summary> /// <returns>False, if the connection manager thread has to stop, true otherwise.</returns> private bool ProcessIncomingMessages() { List <RCPackage> incomingPackages = new List <RCPackage>(); if (this.connection.ReceiveIncomingPackages(ref incomingPackages)) { foreach (RCPackage package in incomingPackages) { if (package.PackageType == RCPackageType.NETWORK_CUSTOM_PACKAGE && package.Sender >= 0 && package.Sender < this.memberCount && package.Sender != this.clientID) { /// Custom message from a member --> notify the listener. this.listener.PackageArrived(package, package.Sender); } else if (package.PackageType == RCPackageType.NETWORK_CONTROL_PACKAGE && package.PackageFormat.ID == Network.FORMAT_LOBBY_LINE_STATE_REPORT) { /// Line state report from the server. short clientID = package.ReadShort(0); byte[] lineStateBytes = package.ReadByteArray(1); LobbyLineState[] lineStates = new LobbyLineState[lineStateBytes.Length]; bool lineStatesOK = true; for (int i = 0; i < lineStateBytes.Length; i++) { if (lineStateBytes[i] == (byte)LobbyLineState.Closed || lineStateBytes[i] == (byte)LobbyLineState.Engaged || lineStateBytes[i] == (byte)LobbyLineState.Opened) { lineStates[i] = (LobbyLineState)lineStateBytes[i]; } else { lineStatesOK = false; } } lineStatesOK = lineStatesOK && (clientID == this.clientID) && (lineStateBytes.Length == this.memberCount); if (!lineStatesOK) { /// Line state report error --> shutdown. this.connection.Shutdown(); if (this.Disposed != null) { this.Disposed(this); } this.listener.LobbyLost(); return(false); } else { /// Line state report arrived --> notify the listener this.listener.LineStateReport(clientID, lineStates); ///return true; } } else if (package.PackageType == RCPackageType.NETWORK_CONTROL_PACKAGE && package.PackageFormat.ID == Network.FORMAT_DISCONNECT_INDICATOR) { /// Disconnection indicator from the server. RCPackage disconnectAck = RCPackage.CreateNetworkControlPackage(Network.FORMAT_DISCONNECT_ACK); disconnectAck.WriteString(0, string.Empty); disconnectAck.WriteByteArray(1, new byte[0] { }); this.connection.SendPackage(disconnectAck); this.connection.Shutdown(); if (this.Disposed != null) { this.Disposed(this); } this.listener.LobbyLost(); return(false); } else if (package.PackageType == RCPackageType.NETWORK_CONTROL_PACKAGE && package.PackageFormat.ID != Network.FORMAT_DEDICATED_MESSAGE && package.PackageFormat.ID != Network.FORMAT_DISCONNECT_ACK && package.PackageFormat.ID != Network.FORMAT_DISCONNECT_INDICATOR && package.PackageFormat.ID != Network.FORMAT_LOBBY_INFO && package.PackageFormat.ID != Network.FORMAT_LOBBY_INFO_VANISHED && package.PackageFormat.ID != Network.FORMAT_LOBBY_LINE_STATE_REPORT) { /// Custom internal message from the server --> notify the listener TraceManager.WriteAllTrace(string.Format("Incoming package: {0}", package.ToString()), NetworkingSystemTraceFilters.INFO); this.listener.ControlPackageArrived(package); } else { /// Unexpected package format and type --> immediate shutdown this.connection.Shutdown(); if (this.Disposed != null) { this.Disposed(this); } this.listener.LobbyLost(); return(false); } } /// end-foreach (RCPackage package in incomingPackages) /// Incoming packages has been processed. return(true); } else { /// Receive error --> immediate shutdown this.connection.Shutdown(); if (this.Disposed != null) { this.Disposed(this); } this.listener.LobbyLost(); return(false); } }
/// <summary> /// Starts a new setup step at host side. /// </summary> /// <param name="outgoingPackages">The outgoing packages sent by the client module.</param> /// <param name="leftList">List of the guests that left or were dropped from the DSS.</param> /// <param name="lostList">List of the guests that the host left connection with.</param> /// <param name="channelStates">The current state of the channels.</param> /// <returns>List of the packages of the outgoing setup step request.</returns> /// <remarks>This function must be called at host side.</remarks> public RCPackage[] CreateRequest(RCPackage[] outgoingPackages, int[] leftList, int[] lostList, DssChannelState[] channelStates) { if (this.mode != DssMode.HOST_SIDE) { throw new DssException("This call is only allowed at host side!"); } if (this.state != SetupStepState.NOT_FINISHED) { throw new DssException("Invalid setup step state!"); } if (channelStates == null || channelStates.Length < 1) { throw new ArgumentNullException("channelStates"); } if (outgoingPackages == null) { throw new ArgumentNullException("outgoingPackages"); } int numPackages = outgoingPackages.Length + 2; /// BEGIN, END RCPackage[] retList = new RCPackage[numPackages]; for (int i = 0; i < numPackages; i++) { if (i == 0) /// BEGIN { retList[i] = RCPackage.CreateNetworkControlPackage(DssRoot.DSS_CTRL_SETUP_STEP_RQ_BEGIN); retList[i].WriteInt(0, this.stepID); retList[i].WriteIntArray(1, leftList != null ? leftList : new int[0] { }); retList[i].WriteIntArray(2, lostList != null ? lostList : new int[0] { }); byte[] stateBytes = new byte[channelStates.Length]; for (int j = 0; j < channelStates.Length; j++) { stateBytes[j] = (byte)channelStates[j]; } retList[i].WriteByteArray(3, stateBytes); } else if (i == numPackages - 1) /// END { retList[i] = RCPackage.CreateNetworkControlPackage(DssRoot.DSS_CTRL_SETUP_STEP_MSG_END); retList[i].WriteInt(0, this.stepID); } else /// Package from the client module { if (outgoingPackages[i - 1] != null && outgoingPackages[i - 1].IsCommitted && outgoingPackages[i - 1].PackageType == RCPackageType.NETWORK_CONTROL_PACKAGE && !DssRoot.IsInternalFormat(outgoingPackages[i - 1].PackageFormat)) { retList[i] = outgoingPackages[i - 1]; } else { throw new DssException("Outgoing setup package format error!"); } } } /// end-for return(retList); }
/// <summary> /// Starts the simulation. /// </summary> private void StartSimulation() { /// Collect opFlags and channel state informations. byte[] chStates = new byte[this.channelProxies.Length]; bool[] opFlags = new bool[this.hostRoot.OpCount]; opFlags[0] = true; for (int i = 0; i < chStates.Length; i++) { if (this.channels[i].CurrentState == this.channels[i].Opened) { /// If the channel is opened then it will be closed. //this.channels[i].CloseChannel(); this.channels[i].PermanentlyClose(); chStates[i] = (byte)DssChannelState.CHANNEL_CLOSED;//CHANNEL_OPENED; opFlags[i + 1] = false; } else if (this.channels[i].CurrentState == this.channels[i].Closed) { /// If the channel is closed then it will remain closed. this.channels[i].PermanentlyClose(); chStates[i] = (byte)DssChannelState.CHANNEL_CLOSED; opFlags[i + 1] = false; } else if (this.channels[i].CurrentState == this.channels[i].Engaged) { /// If the channel is engaged then we have to check whether the guest will be dropped or not. if (this.channelProxies[i].TaskToPerform == DssChannelTask.DROP_AND_OPEN || this.channelProxies[i].TaskToPerform == DssChannelTask.DROP_AND_CLOSE) { /// Close channel permanently. this.channels[i].PermanentlyClose(); this.hostRoot.GuestLeftDss(i); chStates[i] = (byte)DssChannelState.CHANNEL_CLOSED; opFlags[i + 1] = false; } else { /// Don't drop guest, it will participate in the simulation stage. chStates[i] = (byte)DssChannelState.GUEST_CONNECTED; opFlags[i + 1] = true; } } else { throw new DssException("Unexpected channel state!"); } } int[] leftList = null; int[] lostList = null; this.hostRoot.GetGuestEvents(out leftList, out lostList); RCPackage startSimMsg = RCPackage.CreateNetworkControlPackage(DssRoot.DSS_CTRL_START_SIMULATION); startSimMsg.WriteIntArray(0, leftList); startSimMsg.WriteIntArray(1, lostList); startSimMsg.WriteByteArray(2, chStates); /// Send the DSS_CTRL_START_SIMULATION message to every engaged channels for (int i = 0; i < this.sessions.Length; i++) { if (opFlags[i + 1]) { /// Push the corresponding session and channel to the Simulating state this.channels[i].StartSimulation(); this.sessions[i].StartSimulation(); /// Send the start message to the corresponding guest this.hostRoot.Lobby.SendControlPackage(startSimMsg, i + 1); } } /// Reset the simulation manager and ask it to execute the next frame immediately. this.hostRoot.SimulationMgr.Reset(opFlags); this.hostRoot.SimulationMgr.SetNextFrameExecutionTime(DssRoot.Time); /// And finally go to the SimulationStage. this.SendingSetupStepRQs_SimulationStage.Fire(); }
/// <summary> /// This function is called when a control package arrives at setup stage. /// </summary> /// <param name="package">The arrived control package.</param> public void SetupStageCtrlPackage(RCPackage package) { bool error = false; if (this.sm.CurrentState == this.WaitingConnectionRQ) { if (package.PackageFormat.ID == DssRoot.DSS_CTRL_CONN_REQUEST) { RCPackage rejPackage = null; int otherMajor = package.ReadInt(0); int otherMinor = package.ReadInt(1); int otherBuild = package.ReadInt(2); int otherRevision = package.ReadInt(3); if (otherMajor >= 0 && otherMinor >= 0 && otherBuild >= 0 && otherRevision >= 0) { Version otherVer = new Version(otherMajor, otherMinor, otherBuild, otherRevision); if (DssRoot.IsCompatibleVersion(otherVer)) { /// We send back a DSS_CTRL_CONN_ACK package. RCPackage ackPackage = RCPackage.CreateNetworkControlPackage(DssRoot.DSS_CTRL_CONN_ACK); ackPackage.WriteInt(0, DssRoot.APPLICATION_VERSION.Major); ackPackage.WriteInt(1, DssRoot.APPLICATION_VERSION.Minor); ackPackage.WriteInt(2, DssRoot.APPLICATION_VERSION.Build); ackPackage.WriteInt(3, DssRoot.APPLICATION_VERSION.Revision); this.manager.HostRoot.Lobby.SendControlPackage(ackPackage, this.channel.Index + 1); this.WaitingConnectionRQ_SendingSetupStepRQ.Fire(); } else { /// We send back a DSS_CTRL_CONN_REJECT package rejPackage = RCPackage.CreateNetworkControlPackage(DssRoot.DSS_CTRL_CONN_REJECT); string reason = string.Format("Incompatible with host version: {0} (RC.DssServices)", DssRoot.APPLICATION_VERSION.ToString()); rejPackage.WriteString(0, reason); rejPackage.WriteByteArray(1, new byte[0]); } } else { /// We create a DSS_CTRL_CONN_REJECT package rejPackage = RCPackage.CreateNetworkControlPackage(DssRoot.DSS_CTRL_CONN_REJECT); rejPackage.WriteString(0, "Unable to parse version information!"); rejPackage.WriteByteArray(1, new byte[0]); } /// We send back a DSS_CTRL_CONN_REJECT package if necessary. if (rejPackage != null && rejPackage.IsCommitted) { this.manager.HostRoot.Lobby.SendControlPackage(rejPackage, this.channel.Index + 1); error = true; } } /// end-if (package.PackageFormat.ID == DssRoot.DSS_CTRL_CONN_REQUEST) } /// end-if (this.sm.CurrentState == this.WaitingConnectionRQ) else if (this.sm.CurrentState == this.WaitingSetupStepAW) { if (package.PackageFormat.ID == DssRoot.DSS_LEAVE) { string leaveReason = package.ReadString(0); string trcMsg = string.Format("Guest-{0} has left the DSS. Reason: {1}", this.channel.Index, leaveReason.Length != 0 ? leaveReason : "-"); TraceManager.WriteAllTrace(trcMsg, DssTraceFilters.SETUP_STAGE_INFO); this.WaitingSetupStepAW_Inactive.Fire(); this.channel.GuestLeaveSetupStage(); } /// end-if (package.PackageFormat.ID == DssRoot.DSS_LEAVE) else { SetupStep currentStep = this.manager.HostRoot.GetStep(this.channel.Index); currentStep.IncomingPackage(package); if (currentStep.State == SetupStepState.READY) { /// Setup step answer arrived. this.WaitingSetupStepAW_SendingSetupStepRQ.Fire(); } else if (currentStep.State == SetupStepState.ERROR) { /// Setup step answer error. error = true; } else { /// Setup step answer not finished yet, more packages to wait. } } } /// end-if (this.sm.CurrentState == this.WaitingSetupStepAW) if (error) { /// Go to error state if the package cannot be handled until now. SetupStageError(); this.channel.SetupStageError(); return; } }