public NetworkStream stream; // Provides the underlying stream of data for network access. (Static keyword: A static member can be accessed using the type name instead of a reference or value). public void StartSendingMessages(ControlsUpdate controlsUpdate) // This method starts writing messages once the process data button is pressed. { JavaScriptSerializer serializer = new JavaScriptSerializer(); // Provides serialisation functionality and initalizes a new instance. Additonally, Allows for data to be Serialized and Deserialized. string jsonString = serializer.Serialize(controlsUpdate); // Serializes the fields in controlsUpdate and stores it in a string called jsonString. byte[] rawData = Encoding.ASCII.GetBytes(jsonString); // Encodes all characters in the specified string into a sequence of bytes. Takes jsonString and is stored in a byte array. stream.Write(rawData, 0, rawData.Length); // rawData is written out, starting from an index of 0 and writes until the last index of the rawData. OnNewSentMessage?.Invoke(controlsUpdate); // The OnNewSentMessage event is handled here. This will only invoke the event if the event (controlsUpdate) is not null. ? = if not null then execute. }
private void BtnProcessData_Click(object sender, EventArgs e) // Adjusts the controls from this endpoint. { ControlsUpdate controlsUpdate = new ControlsUpdate(); // New object is created for ControlsUpdate. NetworkStream stream = client.GetStream(); // TcpClient has a method to ".GetStream()" this returns a NetworkStream object. It allows for data to be sent and recieved from this endpoint. controlsUpdate.Throttle = trkThrottle.Value; // The value of the throttle track bar is stored in the object created for ControlsUpdate. controlsUpdate.ElevatorPitch = trkElevatorPitch.Value * 0.1; // The value of the ElevatorPitch track bar is stored in the object created for ControlsUpdate. messageSender.stream = stream; // Allows Access to the messageSender Class. messageSender.StartSendingMessages(controlsUpdate); // Messages are sent to the flight simulator via this method once processData is clicked. }
public void SendData(ControlsUpdate controlUpdate) { JavaScriptSerializer serializer = new JavaScriptSerializer();//this is the way we start serializer //serializer is getting the controlUpdate struct that has been passed onto the SendData function in the class sender to update controlUpdate variables byte[] Data = Encoding.ASCII.GetBytes(serializer.Serialize(controlUpdate)); //this is an array of rawData that we receive from the deserialization networkStream.Write(Data, 0, Data.Length); //this is just like writing to a file however we write to a another program via socket programming using network stream SendingEvent?.Invoke(controlUpdate); //we use the event to add the data into controlUpdate }
private void updateControls(ControlsUpdate controlUpdate) { if (this.InvokeRequired) //checks if it is on the same thread { this.Invoke(new SendingDataDelegate(updateControls), new object[] { controlUpdate }); //this is from the remote class } else { controlUpdate.Throttle = trkbThrottle.Value; controlUpdate.ElevatorPitch = trkbElevatorLevel.Value * 0.1; } }
private void DeliverNewMessage(ControlsUpdate controlsUpdate) // Sends data to the remote flight simulator in the form of seralized data in a message and updates the current controls. { if (this.InvokeRequired) { this.Invoke(new OutgoingMessage(DeliverNewMessage), new object[] { controlsUpdate }); // Calls invoke on the current function. Delegate targets method to invoke, taking one arguemnt (string) message. } else { JavaScriptSerializer serializer = new JavaScriptSerializer(); // Provides serialisation functionality and initalizes a new instance. Additonally, Allows for data to be Serialized and Deserialized. string jsonText = serializer.Serialize(controlsUpdate); // Serializes the fields in controlsUpdate and stores it in a string called jsonText. txtControlDetails.AppendText(jsonText + Environment.NewLine); // Appends the string to a text box so that the fields of controlsUpdate will appear on the same line. } }
public void planeStatsChanged() { NetworkStream networkStream = tcpClient.GetStream(); //everytime we scroll in the trackbars we have to get the stream ControlsUpdate controlsUpdate = new ControlsUpdate(); //we get the controlsupdate ready to be used to update controlsUpdate.Throttle = trkbThrottle.Value; controlsUpdate.ElevatorPitch = trkbElevatorLevel.Value * 0.1; //passing on the correct double value remote.networkStream = networkStream; //this is where we send the stream info remote.SendData(controlsUpdate); //we then pass on the variables in the struct to SendData object Thread.Sleep(50); //it will send a lot of data at once so we pause the thread for a bit }
/// <summary> /// Will take data from form controls and update labels with the sent data. /// </summary> /// <param name="controlData">ControlsUpdate struct that gets passed through SendDataToSimulator()</param> void OnDataSendHandler(ControlsUpdate controlData) { // Check if the delegate was invoked, and then invoke it. if (InvokeRequired) { Invoke(new DataSend(OnDataSendHandler), new object[] { controlData }); } else { // Append labels with the sent data whilst rounding values and formatting data with units. lblSentThrottleValue.Text = Math.Round(controlData.throttle).ToString() + "%"; lblSentElevatorPitchValue.Text = Math.Round(controlData.elevatorPitch, 1).ToString() + "°"; } }
// Constructor public Sender(TcpClient networkClient) { controlsData = new ControlsUpdate(); this.networkClient = networkClient; }