private void SynchronizeFrame(object state) { while (!isEnded) { Thread.Sleep(SyncInterval); this.currentTicks++; CommandFrame frame; lock (this.commandsLock) { frame = new CommandFrame(this.currentTicks, this.commands.ToArray()); this.commands.Clear(); } lock (this.totalFramesLock) { this.totalFrames.Add(frame); } lock (this.sessionsLock) { foreach (var session in this.sessions) { session.SynchronizeCommandFrame(frame); } } } }
public bool SendCommand(CommandFrame frame) { try { lastCmdCat = frame.commmandCategory; lastCmd = frame.commmand; // build message int messaheLen = 5 + frame.length + 1; byte[] data = new byte[messaheLen]; byte[] lenBytes = BitConverter.GetBytes(frame.length); data[0] = SerialDecoder.SOFbyte; data[1] = lenBytes[1]; data[2] = lenBytes[0]; data[3] = frame.commmandCategory; data[4] = frame.commmand; for (int idx = 0; idx < frame.data.Length; idx++) { data[idx + 5] = frame.data[idx]; } data[messaheLen - 1] = commMgr.Decoder.CalcLRC(lenBytes[1], lenBytes[0], frame.commmandCategory, frame.commmand, null, frame.data); mainWin.Log(frame.ToString()); //mainWin.Log(GenUtils.Byte2String(data)); System.Diagnostics.Debug.WriteLine($"Sent: {GenUtils.Byte2String(data)}"); //mainWin.Log(GenUtils.Byte2String(data)); commMgr.Send(data); return(cmdAre.WaitOne(250, false)); } catch { } finally { } return(false); }
public void ReceiveCommandFrame(CommandFrame frame) { foreach (var command in frame.Commands) { commands.Add(command); } }
public static byte[] CreateInputCommand(CommandFrame frame) { var fbb = new FlatBufferBuilder(1024); var oFrame = CreateCommandFrameOffset(frame, fbb); var oInputCommand = Messages.InputCommand.CreateInputCommand(fbb, oFrame); Messages.InputCommand.FinishInputCommandBuffer(fbb, oInputCommand); return fbb.ToProtocolMessage(MessageIds.InputCommand); }
static async Task RegisterTunnel() { CommandFrame frame = new CommandFrame(); frame.Command = new RegisterTunnelCommand() { Domain = ClientManager.SubDomain }; await frame.Send(ClientManager.TunnelConnection); }
private void autoPollingCHB_CheckedChanged(object sender, EventArgs e) { CommandFrame frame = new CommandFrame(); frame.commmandCategory = 0; frame.commmand = 2; frame.length = 3; frame.data = new byte[] { autoPollingCHB.Checked ? (byte)0 : (byte)1 }; bool res = tagReader?.SendCommand(frame) ?? false; Log($"Auto polling => {res}"); }
public static byte[] CreateSyncFrame(CommandFrame[] frames) { var fbb = new FlatBufferBuilder(1024); var oFrames = CreateFrameOffsets(fbb, frames); var vFrames = Messages.SyncFrame.CreateFramesVector(fbb, oFrames); var oSyncFrame = Messages.SyncFrame.CreateSyncFrame(fbb, vFrames); Messages.SyncFrame.FinishSyncFrameBuffer(fbb, oSyncFrame); return fbb.ToProtocolMessage(MessageIds.SyncFrame); }
private void beepBTN_Click(object sender, EventArgs e) { CommandFrame frame = new CommandFrame(); frame.commmandCategory = 0; frame.commmand = 4; frame.length = 2; frame.data = new byte[0]; bool res = tagReader?.SendCommand(frame) ?? false; Log($"Beep => {res}"); }
private void setProtocolBTN_Click(object sender, EventArgs e) { CommandFrame frame = new CommandFrame(); frame.commmandCategory = 0; frame.commmand = 0; frame.length = 3; frame.data = new byte[1] { (byte)0 }; bool res = tagReader?.SendCommand(frame) ?? false; Log($"Set protocol => {res}"); }
private void getVersionBTN_Click(object sender, EventArgs e) { CommandFrame frame = new CommandFrame(); frame.commmandCategory = 0; frame.commmand = 1; frame.length = 2; frame.data = new byte[0]; bool res = tagReader?.SendCommand(frame) ?? false; if (res) { string ver = Encoding.ASCII.GetString(tagReader.rxFrame.data); Log($"Get version => {res}, version => {ver}"); } else { Log($"Get version => {res}"); } }
/// <summary> /// 执行程序。 /// </summary> /// <exception cref="Exception"></exception> /// <returns></returns> public async Task StartAsync(string filename, string arguments) { if (!Started) { Started = true; //发送请求 CommandFrame frame = new CommandFrame() { FileName = filename, Arguments = arguments, }; base.GuidFilter = frame.Guid; await Send(frame).ConfigureAwait(false); } else { throw new InvalidOperationException(); } }
static async Task StartHeartBeat() { while (true) { try { CommandFrame frame = new CommandFrame(); frame.Command = new MessageCommand() { Message = "heart beat" }; await frame.Send(ClientManager.TunnelConnection); //await Task.Delay(60000); return; } catch { } } }
private void OnServerMessage_CommandFrame(NetIncomingMessage msg, CommandFrame frame) { simulator.ReceiveCommandFrame(frame); }
public void TestExampleScene() { var simulator = new ServerSimulator(); simulator.RegisterActorFactory <ExampleSceneDesc, ExampleScene>(desc => new ExampleScene()); simulator.RegisterActorFactory <BallDesc, Ball>(desc => new Ball()); simulator.RegisterActorFactory <CubeDesc, Cube>(desc => new Cube()); var sceneDesc = new ExampleSceneDesc(50, 50); var scene = simulator.CreateScene <ExampleScene>(sceneDesc); // Time: 0 - 50 simulator.Simulate(); var frame0 = simulator.DeltaSyncFrame; Assert.AreEqual(0, frame0.Time); Assert.AreEqual(50, frame0.DeltaTime); Assert.AreEqual(3, frame0.Events.Count); var ball50 = (RigidBodySnapshot)scene.Ball.Snapshot; AssertHelper.AreApproximate(-2.6f, ball50.Position.X); AssertHelper.AreApproximate(2.575475f, ball50.Position.Y); AssertHelper.AreApproximate(-2.6f, ball50.Position.Z); var cube50 = (RigidBodySnapshot)scene.Cube.Snapshot; AssertHelper.AreApproximate(1.1f, cube50.Position.X); AssertHelper.AreApproximate(1.07547498f, cube50.Position.Y); AssertHelper.AreApproximate(1.1f, cube50.Position.Z); var killBallCommand = new KickBallCommand(1, 0, 1); var clientId = new ClientId(1); var command = new Command(clientId, scene.Ball.Desc.Id, 1, 20, 20, killBallCommand); var clientFrame = new CommandFrame(clientId); clientFrame.Commands.Add(command); simulator.ReceiveCommandFrame(clientFrame); // Time: 50 - 100 simulator.Simulate(); var frame50 = simulator.DeltaSyncFrame; Assert.AreEqual(50, frame50.Time); Assert.AreEqual(50, frame50.DeltaTime); Assert.AreEqual(2, frame50.Events.Count); var ball100 = (RigidBodySnapshot)scene.Ball.Snapshot; AssertHelper.AreApproximate(-2.3499999f, ball100.Position.X); AssertHelper.AreApproximate(2.52669716f, ball100.Position.Y); AssertHelper.AreApproximate(-2.3499999f, ball100.Position.Z); var cube100 = (RigidBodySnapshot)scene.Cube.Snapshot; AssertHelper.AreApproximate(1.1f, cube100.Position.X); AssertHelper.AreApproximate(1.02669704f, cube100.Position.Y); AssertHelper.AreApproximate(1.1f, cube100.Position.Z); }
/// <summary> /// Handles incoming commands from devices connected over the command channel. /// </summary> /// <param name="clientID">Guid of client that sent the command.</param> /// <param name="connectionID">Remote client connection identification (i.e., IP:Port).</param> /// <param name="commandBuffer">Data buffer received from connected client device.</param> /// <param name="length">Valid length of data within the buffer.</param> protected override void DeviceCommandHandler(Guid clientID, string connectionID, byte[] commandBuffer, int length) { try { // Interpret data received from a client as a command frame CommandFrame commandFrame = new CommandFrame(commandBuffer, 0, length); IServer commandChannel = (IServer)CommandChannel ?? DataChannel; // Validate incoming ID code if requested if (!m_validateIDCode || commandFrame.IDCode == this.IDCode) { switch (commandFrame.Command) { case DeviceCommand.SendConfigurationFrame1: if (commandChannel != null) { ConfigurationFrame1 configFrame1 = CastToConfigurationFrame1(m_configurationFrame); commandChannel.SendToAsync(clientID, configFrame1.BinaryImage, 0, configFrame1.BinaryLength); OnStatusMessage(MessageLevel.Info, $"Received request for \"{commandFrame.Command}\" from \"{connectionID}\" - type 1 config frame was returned."); } break; case DeviceCommand.SendConfigurationFrame2: if (commandChannel != null) { commandChannel.SendToAsync(clientID, m_configurationFrame.BinaryImage, 0, m_configurationFrame.BinaryLength); OnStatusMessage(MessageLevel.Info, $"Received request for \"{commandFrame.Command}\" from \"{connectionID}\" - type 2 config frame was returned."); } break; case DeviceCommand.SendHeaderFrame: if (commandChannel != null) { StringBuilder status = new StringBuilder(); status.Append("IEEE C37.118 Concentrator:\r\n\r\n"); status.AppendFormat(" Auto-publish config frame: {0}\r\n", AutoPublishConfigurationFrame); status.AppendFormat(" Auto-start data channel: {0}\r\n", AutoStartDataChannel); status.AppendFormat(" Data stream ID code: {0}\r\n", IDCode); status.AppendFormat(" Derived system time: {0} UTC\r\n", ((DateTime)RealTime).ToString("yyyy-MM-dd HH:mm:ss.fff")); HeaderFrame headerFrame = new HeaderFrame(status.ToString()); commandChannel.SendToAsync(clientID, headerFrame.BinaryImage, 0, headerFrame.BinaryLength); OnStatusMessage(MessageLevel.Info, $"Received request for \"SendHeaderFrame\" from \"{connectionID}\" - frame was returned."); } break; case DeviceCommand.EnableRealTimeData: // Only responding to stream control command if auto-start data channel is false if (!AutoStartDataChannel) { StartDataChannel(); OnStatusMessage(MessageLevel.Info, $"Received request for \"EnableRealTimeData\" from \"{connectionID}\" - concentrator real-time data stream was started."); } else { OnStatusMessage(MessageLevel.Info, $"Request for \"EnableRealTimeData\" from \"{connectionID}\" was ignored - concentrator data channel is set for auto-start."); } break; case DeviceCommand.DisableRealTimeData: // Only responding to stream control command if auto-start data channel is false if (!AutoStartDataChannel) { StopDataChannel(); OnStatusMessage(MessageLevel.Info, $"Received request for \"DisableRealTimeData\" from \"{connectionID}\" - concentrator real-time data stream was stopped."); } else { OnStatusMessage(MessageLevel.Info, $"Request for \"DisableRealTimeData\" from \"{connectionID}\" was ignored - concentrator data channel is set for auto-start."); } break; default: OnStatusMessage(MessageLevel.Info, $"Request for \"{commandFrame.Command}\" from \"{connectionID}\" was ignored - device command is unsupported."); break; } } else { OnStatusMessage(MessageLevel.Warning, $"Concentrator ID code validation failed for device command \"{commandFrame.Command}\" from \"{connectionID}\" - no action was taken."); } } catch (Exception ex) { OnProcessException(MessageLevel.Warning, new InvalidOperationException($"Remotely connected device \"{connectionID}\" sent an unrecognized data sequence to the concentrator, no action was taken. Exception details: {ex.Message}", ex)); } }
private static Offset<Messages.CommandFrame>[] CreateFrameOffsets(FlatBufferBuilder fbb, CommandFrame[] frames) { if (frames == null || frames.Length == 0) return new Offset<Messages.CommandFrame>[0]; var oFrames = new Offset<Messages.CommandFrame>[frames.Length]; int i = 0; foreach (var f in frames) { Offset<Messages.Command>[] oCommands = null; if (f.Commands != null && f.Commands.Length > 0) { oCommands = new Offset<Messages.Command>[f.Commands.Length]; int j = 0; foreach (var c in f.Commands) { oCommands[j] = Messages.Command.CreateCommand(fbb, c.CommandId, c.SessionId); j++; } } else { oCommands = new Offset<Messages.Command>[0]; } var vCommands = Messages.CommandFrame.CreateCommandsVector(fbb, oCommands); oFrames[i] = Messages.CommandFrame.CreateCommandFrame(fbb, f.Ticks, vCommands); i++; } return oFrames; }
/// <summary> /// Handles incoming commands from devices connected over the command channel. /// </summary> /// <param name="clientID">Guid of client that sent the command.</param> /// <param name="connectionID">Remote client connection identification (i.e., IP:Port).</param> /// <param name="commandBuffer">Data buffer received from connected client device.</param> /// <param name="length">Valid length of data within the buffer.</param> protected override void DeviceCommandHandler(Guid clientID, string connectionID, byte[] commandBuffer, int length) { try { // Interpret data received from a client as a command frame CommandFrame commandFrame = new CommandFrame(commandBuffer, 0, length); IServer commandChannel = (IServer)CommandChannel ?? DataChannel; // Validate incoming ID code if requested if (!m_validateIDCode || commandFrame.IDCode == this.IDCode) { switch (commandFrame.Command) { case DeviceCommand.SendConfigurationFrame1: case DeviceCommand.SendConfigurationFrame2: if (commandChannel != null) { commandChannel.SendToAsync(clientID, m_configurationFrame.BinaryImage, 0, m_configurationFrame.BinaryLength); OnStatusMessage("Received request for \"{0}\" from \"{1}\" - frame was returned.", commandFrame.Command, connectionID); } break; case DeviceCommand.EnableRealTimeData: // Only responding to stream control command if auto-start data channel is false if (!AutoStartDataChannel) { StartDataChannel(); OnStatusMessage("Received request for \"EnableRealTimeData\" from \"{0}\" - concentrator real-time data stream was started.", connectionID); } else { OnStatusMessage("Request for \"EnableRealTimeData\" from \"{0}\" was ignored - concentrator data channel is set for auto-start.", connectionID); } break; case DeviceCommand.DisableRealTimeData: // Only responding to stream control command if auto-start data channel is false if (!AutoStartDataChannel) { StopDataChannel(); OnStatusMessage("Received request for \"DisableRealTimeData\" from \"{0}\" - concentrator real-time data stream was stopped.", connectionID); } else { OnStatusMessage("Request for \"DisableRealTimeData\" from \"{0}\" was ignored - concentrator data channel is set for auto-start.", connectionID); } break; default: OnStatusMessage("Request for \"{0}\" from \"{1}\" was ignored - device command is unsupported.", commandFrame.Command, connectionID); break; } } else { OnStatusMessage("WARNING: Concentrator ID code validation failed for device command \"{0}\" from \"{1}\" - no action was taken.", commandFrame.Command, connectionID); } } catch (Exception ex) { OnProcessException(new InvalidOperationException(string.Format("Remotely connected device \"{0}\" sent an unrecognized data sequence to the concentrator, no action was taken. Exception details: {1}", connectionID, ex.Message), ex)); } }
private static Offset<Messages.CommandFrame> CreateCommandFrameOffset(CommandFrame frame, FlatBufferBuilder fbb) { Offset<Messages.Command>[] oCommands = null; if (frame.Commands != null && frame.Commands.Length > 0) { oCommands = new Offset<Messages.Command>[frame.Commands.Length]; int j = 0; foreach (var c in frame.Commands) { oCommands[j] = Messages.Command.CreateCommand(fbb, c.CommandId, c.SessionId); j++; } } else { oCommands = new Offset<Messages.Command>[0]; } var vCommand = Messages.CommandFrame.CreateCommandsVector(fbb, oCommands); return Messages.CommandFrame.CreateCommandFrame(fbb, frame.Ticks, vCommand); }