private async void sendHandshake(AcConverter.handshaker.HandshakeOperation operationId) { // Calculate handshake bytes and send them. byte[] sendbytes = AcConverter.structToBytes(new AcConverter.handshaker(operationId)); writer.WriteBytes(sendbytes); await writer.StoreAsync(); }
private void Socket_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args) { DataReader reader = args.GetDataReader(); byte[] receivebytes = new byte[reader.UnconsumedBufferLength]; reader.ReadBytes(receivebytes); if (!isConnected) // Received data is handshake response. { // Check if it is a Handshake response-packet. System.Diagnostics.Debug.Assert(receivebytes.Length == Marshal.SizeOf <AcConverter.handshakerResponse>()); AcConverter.handshakerResponse response = AcConverter.bytesToStruct <AcConverter.handshakerResponse>(receivebytes); // Set session info data. sessionInfo.driverName = AcHelperFunctions.SanitiseString(response.driverName); sessionInfo.carName = AcHelperFunctions.SanitiseString(response.carName); sessionInfo.trackName = AcHelperFunctions.SanitiseString(response.trackName); sessionInfo.trackLayout = AcHelperFunctions.SanitiseString(response.trackConfig); // Confirm handshake with data type. sendHandshake((AcConverter.handshaker.HandshakeOperation)dataType); isConnected = true; } else // An actual info packet! { switch (dataType) { case ConnectionType.CarInfo: System.Diagnostics.Debug.Assert(receivebytes.Length == Marshal.SizeOf <AcConverter.RTCarInfo>()); AcConverter.RTCarInfo rtcar = AcConverter.bytesToStruct <AcConverter.RTCarInfo>(receivebytes); carInfo.speedAsKmh = rtcar.speed_Kmh; carInfo.engineRPM = rtcar.engineRPM; carInfo.Gear = rtcar.gear; carInfo.currentLapTime = TimeSpan.FromMilliseconds(rtcar.lapTime); carInfo.lastLapTime = TimeSpan.FromMilliseconds(rtcar.lastLap); carInfo.bestLapTime = TimeSpan.FromMilliseconds(rtcar.bestLap); if (CarUpdate != null) { AcUpdateEventArgs updateArgs = new AcUpdateEventArgs(); updateArgs.carInfo = this.carInfo; CarUpdate(this, updateArgs); } break; case ConnectionType.LapTime: // Check if it is the right packet. System.Diagnostics.Debug.Assert(receivebytes.Length == Marshal.SizeOf <AcConverter.RTLap>()); AcConverter.RTLap rtlap = AcConverter.bytesToStruct <AcConverter.RTLap>(receivebytes); // Set last lap info data. lapInfo.carName = AcHelperFunctions.SanitiseString(rtlap.carName); lapInfo.driverName = AcHelperFunctions.SanitiseString(rtlap.driverName); lapInfo.carNumber = rtlap.carIdentifierNumber; lapInfo.lapNumber = rtlap.lap; lapInfo.lapTime = TimeSpan.FromMilliseconds(rtlap.time); if (LapUpdate != null) { AcUpdateEventArgs updateArgs = new AcUpdateEventArgs(); updateArgs.lapInfo = this.lapInfo; LapUpdate(this, updateArgs); } break; default: break; } } }