/// <summary> /// 异步接收回调 /// </summary> /// <param name="ar"></param> private void CallBackRecvive(IAsyncResult ar) { try { //将传过来的异步结果转为我们需要解析的类型 UDPData state = ar.AsyncState as UDPData; IPEndPoint ipEndPoint = state.EndPoint; //结束异步接受 不结束会导致重复挂起线程卡死 byte[] data = state.UDPClient.EndReceive(ar, ref ipEndPoint); //解析数据 编码自己调整暂定为默认 依客户端传过来的编码而定 receiveData = Encoding.UTF8.GetString(data); ipEnd = ipEndPoint; //Debug.Log(receiveData); if (!client.Contains(ipEndPoint)) { client.Add(ipEndPoint); } //数据的解析再Update里执行 Unity中Thread无法调用主线程的方法 //再次开启异步接收数据 state.UDPClient.BeginReceive(CallBackRecvive, state); } catch (Exception e) { Debug.LogError(e.Message); throw; } }
public ItemDetailPage() { InitializeComponent(); UDPData item = new UDPData(); // viewModel = new ItemDetailViewModel(item); // BindingContext = viewModel; }
private static void WriteData(UDPData data) { Console.SetCursorPosition(0, 2); Console.WriteLine($"IsRaceOn:\t{data.IsRaceOn,15:N0}\t\tTimestampMS:\t {data.TimestampMS,15:N0}", data); Console.WriteLine($"CarOrdinal:{data.CarOrdinal,7:N0}\tCarClass:{data.CarClass,4:N0}\tCarPerformanceIndex:{data.CarPerformanceIndex,4:N0}\tDrivetrainType:{data.DrivetrainType,4:N0}\tNumCylinders:{data.NumCylinders,4:N0}", data); Console.WriteLine($"EngineMaxRpm:\t{data.EngineMaxRpm,17:F11}\tEngineIdleRpm:\t {data.EngineIdleRpm,15:F11}\tCurrentEngineRpm: {data.CurrentEngineRpm,17:F11}", data); Console.WriteLine($"AccelerationX:\t{data.AccelerationX,17:F11}\tAccelerationY:\t {data.AccelerationY,15:F11}\tAccelerationZ:\t\t{data.AccelerationZ,15:F11}", data); Console.WriteLine($"VelocityX:\t{data.VelocityX,17:F11}\tVelocityY:\t {data.VelocityY,15:F11}\tVelocityZ:\t\t{data.VelocityZ,15:F11}", data); Console.WriteLine($"AngularVelocityX: {data.AngularVelocityX,15:F11}\tAngularVelocityY:{data.AngularVelocityY,15:F11}\tAngularVelocityZ:\t{data.AngularVelocityZ,15:F11}", data); Console.WriteLine($"Yaw:\t\t{data.Yaw,17:F11}\tPitch:\t\t {data.Pitch,15:F11}\tRoll:\t\t\t{data.Roll,15:F11}", data); Console.WriteLine($"NormalizedSuspensionTravelFrontLeft:\t{data.NormalizedSuspensionTravelFrontLeft,15:F11}\t\tNormalizedSuspensionTravelFrontRight:{data.NormalizedSuspensionTravelFrontRight,15:F11}", data); Console.WriteLine($"NormalizedSuspensionTravelRearLeft:\t{data.NormalizedSuspensionTravelRearLeft,15:F11}\t\tNormalizedSuspensionTravelRearRight: {data.NormalizedSuspensionTravelRearRight,15:F11}", data); Console.WriteLine($"TireSlipRatioFrontLeft:\t\t\t{data.TireSlipRatioFrontLeft,15:F11}\t\tTireSlipRatioFrontRight:\t\t {data.TireSlipRatioFrontRight,15:F11}", data); Console.WriteLine($"TireSlipRatioRearLeft:\t\t\t{data.TireSlipRatioRearLeft,15:F11}\t\tTireSlipRatioRearRight:\t\t\t {data.TireSlipRatioRearRight,15:F11}", data); Console.WriteLine($"WheelRotationSpeedFrontLeft:\t\t{data.WheelRotationSpeedFrontLeft,15:F11}\t\tWheelRotationSpeedFrontRight:\t\t {data.WheelRotationSpeedFrontRight,15:F11}", data); Console.WriteLine($"WheelRotationSpeedRearLeft:\t\t{data.WheelRotationSpeedRearLeft,15:F11}\t\tWheelRotationSpeedRearRight:\t\t {data.WheelRotationSpeedRearRight,15:F11}", data); Console.WriteLine($"WheelOnRumbleStripFrontLeft:\t\t{data.WheelOnRumbleStripFrontLeft,15:N0}\t\tWheelOnRumbleStripFrontRight:\t\t {data.WheelOnRumbleStripFrontRight,15:N0}", data); Console.WriteLine($"WheelOnRumbleStripRearLeft:\t\t{data.WheelOnRumbleStripRearLeft,15:N0}\t\tWheelOnRumbleStripRearRight:\t\t {data.WheelOnRumbleStripRearRight,15:N0}", data); Console.WriteLine($"WheelInPuddleDepthFrontLeft:\t\t{data.WheelInPuddleDepthFrontLeft,15:F11}\t\tWheelInPuddleDepthFrontRight:\t\t {data.WheelInPuddleDepthFrontRight,15:F11}", data); Console.WriteLine($"WheelInPuddleDepthRearLeft:\t\t{data.WheelInPuddleDepthRearLeft,15:F11}\t\tWheelInPuddleDepthRearRight:\t\t {data.WheelInPuddleDepthRearRight,15:F11}", data); Console.WriteLine($"SurfaceRumbleFrontLeft:\t\t\t{data.SurfaceRumbleFrontLeft,15:F11}\t\tSurfaceRumbleFrontRight:\t\t {data.SurfaceRumbleFrontRight,15:F11}", data); Console.WriteLine($"SurfaceRumbleRearLeft:\t\t\t{data.SurfaceRumbleRearLeft,15:F11}\t\tSurfaceRumbleRearRight:\t\t\t {data.SurfaceRumbleRearRight,15:F11}", data); Console.WriteLine($"TireSlipAngleFrontLeft:\t\t\t{data.TireSlipAngleFrontLeft,15:F11}\t\tTireSlipAngleFrontRight:\t\t {data.TireSlipAngleFrontRight,15:F11}", data); Console.WriteLine($"TireSlipAngleRearLeft:\t\t\t{data.TireSlipAngleRearLeft,15:F11}\t\tTireSlipAngleRearRight:\t\t\t {data.TireSlipAngleRearRight,15:F11}", data); Console.WriteLine($"TireCombinedSlipFrontLeft:\t\t{data.TireCombinedSlipFrontLeft,15:F11}\t\tTireCombinedSlipFrontRight:\t\t {data.TireCombinedSlipFrontRight,15:F11}", data); Console.WriteLine($"TireCombinedSlipRearLeft:\t\t{data.TireCombinedSlipRearLeft,15:F11}\t\tTireCombinedSlipRearRight:\t\t {data.TireCombinedSlipRearRight,15:F11}", data); Console.WriteLine($"SuspensionTravelMetersFrontLeft:\t{data.SuspensionTravelMetersFrontLeft,15:F11}\t\tSuspensionTravelMetersFrontRight:\t {data.SuspensionTravelMetersFrontRight,15:F11}", data); Console.WriteLine($"SuspensionTravelMetersRearLeft:\t\t{data.SuspensionTravelMetersRearLeft,15:F11}\t\tSuspensionTravelMetersRearRight:\t {data.SuspensionTravelMetersRearRight,15:F11}", data); SaveDataAsync(data); }
/// <summary> /// 接收UDP会话 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void eventArgs_Completed(object sender, SocketAsyncEventArgs e) { try { if (e.SocketError != SocketError.Success) { var errorCode = (int)e.SocketError; //The listen socket was closed if (errorCode == 995 || errorCode == 10004 || errorCode == 10038) { return; } _logger.LogError($"UDP协议监听异常,异常码{errorCode}"); } if (e.LastOperation == SocketAsyncOperation.ReceiveFrom) { try { var handler = NewClientAccepted; IChannel channel = _channelFactory(_listenSocket, e.RemoteEndPoint); if (handler != null && channel != null) { handler.Invoke(channel); } if (UDPData != null) { UDPData.Invoke(e.MemoryBuffer.Slice(0, e.BytesTransferred), e.RemoteEndPoint); } } catch (Exception exc) { _logger.LogError(exc.ToString()); } _listenSocket.ReceiveFromAsync(e); } } catch (Exception ex) { _logger.LogError(ex.ToString()); } }
//初始化 void InitSocket() { ipList = new Dictionary <string, IPEndPoint>(); client = new List <IPEndPoint>(); //LoadXMLPassword(); RecviveThread = new Thread(() => { //实例化一个IPEndPoint,任意IP和对应端口 端口自行修改 IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, ConnectPort); UdpClient udpReceive = new UdpClient(endPoint); UDPData data = new UDPData(endPoint, udpReceive); //开启异步接收 udpReceive.BeginReceive(CallBackRecvive, data); }) { //设置为后台线程 IsBackground = true }; //开启线程 RecviveThread.Start(); }
public ItemDetailViewModel(UDPData item) : this() { Item = item; }
//==================================================================================== // 関数 //==================================================================================== /// <summary> /// 表示を設定します /// </summary> public void SetDisp(UDPData data) { SetDisp(data.Text, data.OnClick); }
//==================================================================================== // 関数 //==================================================================================== /// <summary> /// 表示を設定します /// </summary> public void SetDisp(UDPData data) { SetDisp(data.m_text, data.m_onClick); }
private static async void SaveDataAsync(UDPData data) { if (context != null) { Data.Models.UDPData record = new Data.Models.UDPData() { Created = DateTime.Now.ToUniversalTime(), IsRaceOn = data.IsRaceOn, // = 1 when race is on. = 0 when in menus/race stopped … //u32 TimestampMS = data.TimestampMS, //Can overflow to 0 eventually //f32 EngineMaxRpm = data.EngineMaxRpm, //f32 EngineIdleRpm = data.EngineIdleRpm, //f32 CurrentEngineRpm = data.CurrentEngineRpm, //f32 AccelerationX = data.AccelerationX, //In the car's local space; X = right, Y = up, Z = forward AccelerationY = data.AccelerationY, //f32 AccelerationZ = data.AccelerationZ, //f32 VelocityX = data.VelocityX, //In the car's local space; X = right, Y = up, Z = forward //f32 VelocityY = data.VelocityY, //f32 VelocityZ = data.VelocityZ, //f32 AngularVelocityX = data.AngularVelocityX, //In the car's local space; X = pitch, Y = yaw, Z = roll //f32 AngularVelocityY = data.AngularVelocityY, //f32 AngularVelocityZ = data.AngularVelocityZ, //f32 Yaw = data.Yaw, //f32 Pitch = data.Pitch, //f32 Roll = data.Roll, //f32 NormalizedSuspensionTravelFrontLeft = data.NormalizedSuspensionTravelFrontLeft, // Suspension travel normalized: 0.0f = max stretch; 1.0 = max compression NormalizedSuspensionTravelFrontRight = data.NormalizedSuspensionTravelFrontRight, //f32 NormalizedSuspensionTravelRearLeft = data.NormalizedSuspensionTravelRearLeft, //f32 NormalizedSuspensionTravelRearRight = data.NormalizedSuspensionTravelRearRight, //f32 TireSlipRatioFrontLeft = data.TireSlipRatioFrontLeft, // Tire normalized slip ratio, = 0 means 100% grip and |ratio| > 1.0 means loss of grip. TireSlipRatioFrontRight = data.TireSlipRatioFrontRight, //f32 TireSlipRatioRearLeft = data.TireSlipRatioRearLeft, //f32 TireSlipRatioRearRight = data.TireSlipRatioRearRight, //f32 WheelRotationSpeedFrontLeft = data.WheelRotationSpeedFrontLeft, // Wheel rotation speed radians/sec. WheelRotationSpeedFrontRight = data.WheelRotationSpeedFrontRight, //f32 WheelRotationSpeedRearLeft = data.WheelRotationSpeedRearLeft, //f32 WheelRotationSpeedRearRight = data.WheelRotationSpeedRearRight, //s32 WheelOnRumbleStripFrontLeft = data.WheelOnRumbleStripFrontLeft, // = 1 when wheel is on rumble strip, = 0 when off. //s32 WheelOnRumbleStripFrontRight = data.WheelOnRumbleStripFrontRight, //s32 WheelOnRumbleStripRearLeft = data.WheelOnRumbleStripRearLeft, //s32 WheelOnRumbleStripRearRight = data.WheelOnRumbleStripRearRight, //f32 WheelInPuddleDepthFrontLeft = data.WheelInPuddleDepthFrontLeft, // = from 0 to 1, where 1 is the deepest puddle //f32 WheelInPuddleDepthFrontRight = data.WheelInPuddleDepthFrontRight, //f32 WheelInPuddleDepthRearLeft = data.WheelInPuddleDepthRearLeft, //f32 WheelInPuddleDepthRearRight = data.WheelInPuddleDepthRearRight, //f32 SurfaceRumbleFrontLeft = data.SurfaceRumbleFrontLeft, // Non-dimensional surface rumble values passed to controller force feedback //f32 SurfaceRumbleFrontRight = data.SurfaceRumbleFrontRight, //f32 SurfaceRumbleRearLeft = data.SurfaceRumbleRearLeft, //f32 SurfaceRumbleRearRight = data.SurfaceRumbleRearRight, //f32 TireSlipAngleFrontLeft = data.TireSlipAngleFrontLeft, // Tire normalized slip angle, = 0 means 100% grip and |angle| > 1.0 means loss of grip. //f32 TireSlipAngleFrontRight = data.TireSlipAngleFrontRight, //f32 TireSlipAngleRearLeft = data.TireSlipAngleRearLeft, //f32 TireSlipAngleRearRight = data.TireSlipAngleRearRight, //f32 TireCombinedSlipFrontLeft = data.TireCombinedSlipFrontLeft, // Tire normalized combined slip, = 0 means 100% grip and |slip| > 1.0 means loss of grip. //f32 TireCombinedSlipFrontRight = data.TireCombinedSlipFrontRight, //f32 TireCombinedSlipRearLeft = data.TireCombinedSlipRearLeft, //f32 TireCombinedSlipRearRight = data.TireCombinedSlipRearRight, //f32 SuspensionTravelMetersFrontLeft = data.SuspensionTravelMetersFrontLeft, // Actual suspension travel in meters //f32 SuspensionTravelMetersFrontRight = data.SuspensionTravelMetersFrontRight, //f32 SuspensionTravelMetersRearLeft = data.SuspensionTravelMetersRearLeft, //f32 SuspensionTravelMetersRearRight = data.SuspensionTravelMetersRearRight, //s32 CarOrdinal = data.CarOrdinal, //Unique ID of the car make/model //s32 CarClass = data.CarClass, //Between 0 (D -- worst cars) and 7 (X class -- best cars) inclusive //s32 CarPerformanceIndex = data.CarPerformanceIndex, //Between 100 (slowest car) and 999 (fastest car) inclusive //s32 DrivetrainType = data.DrivetrainType, //Corresponds to EDrivetrainType; 0 = FWD, 1 = RWD, 2 = AWD //s32 NumCylinders = data.NumCylinders, //Number of cylinders in the engine }; await context.UDPDatas.AddAsync(record); await context.SaveChangesAsync(); } }
private static void Listener_OnReceive(object sender, ReceiveEventArgs e) { UDPData data = e.UdpReceiveResult.Buffer.Deserialize <UDPData>(); WriteData(data); }