private CommandDefinition.CommandFormat ReplyCreate(CommandEnum command, BooleanValue isAck, ActionEnum action, byte subAction, byte[] data) { CommandDefinition.CommandAckStruct /*can't name it command*/ cmd = new CommandDefinition.CommandAckStruct(command, BooleanValue.False, isAck); CommandDefinition.ActionStruct /*can't name it action*/ act = new CommandDefinition.ActionStruct(ActionEnum.Response, subAction); CommandDefinition.CommandFormat packet = new CommandDefinition.CommandFormat(cmd, act, data); return packet; }
private CommandDefinition.CommandFormat CommandMake(CommandEnum commandCode, BooleanValue acknowledgeIsRequested, ActionEnum actionCode, byte subAction) { CommandDefinition.CommandAckStruct acknowledgement = new CommandDefinition.CommandAckStruct(commandCode, acknowledgeIsRequested, BooleanValue.False); CommandDefinition.ActionStruct action = new CommandDefinition.ActionStruct(actionCode, subAction); CommandDefinition.CommandFormat returnedCommand = new CommandDefinition.CommandFormat(acknowledgement.CommandWithAck, action.ActionAndSubAction); return returnedCommand; }
private void ServerAgent() { while (!_serverTerminate) try { WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di7, true); NetworkInterface.EnableStaticIP(ServerAddress, ServerSubnet, ServerGateway, ServerMac); NetworkInterface.EnableStaticDns(ServerGateway); try { ListenerClose( /*_listenerSocket == null*/ ); /*just to ensure tidiness*/ _listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, ServerPort); _listenerSocket.Bind(endPoint); _listenerSocket.Listen(1); /*blocks...*/ SessionClose( /*_sessionSocket == null*/ ); /*just to ensure tidiness*/ using (_sessionSocket = _listenerSocket.Accept()) /*using overkill, but what th' hey...*/ try { SignOfLifeStart( /*_signOfLifeTimer != null*/ ); try { byte[] buffer = new byte[/*extra for safety*/ 2 * CommandDefinition.PacketSize]; while (/*read OK?*/_sessionSocket.Receive(buffer, buffer.Length, SocketFlags.None) >= 1 /*blocks...*/) { CommandDefinition.CommandFormat command = new CommandDefinition.CommandFormat(); command = command.Deserialize(buffer); ProcessCommand(command); } } catch { SendSignOfLife(uint.MaxValue - 4); } finally { SignOfLifeStop( /*_signOfLifeTimer == null*/ ); } } catch { SendSignOfLife(uint.MaxValue -3); } finally { SessionClose( /*_sessionSocket == null*/ ); } } catch { SendSignOfLife(uint.MaxValue - 2); } finally { ListenerClose( /*_listenerSocket == null*/ ); } } catch { SendSignOfLife(uint.MaxValue - 1); } finally { WIZnet_W5100.ReintializeNetworking(); } } catch { SendSignOfLife(uint.MaxValue); } finally { Thread.Sleep(/*1s*/ 1000 /*ms*/ ); Program.ResetBoard(); } }
private void SendAck(CommandDefinition.CommandFormat command) { CommandDefinition.CommandFormat /*can't name it command*/ cmd = new CommandDefinition.CommandFormat(); cmd.CommandAck.Command = command.CommandAck.Command; cmd.CommandAck.IsAck = BooleanValue.True; cmd.Action.Action = ActionEnum.Response; cmd.Action.SubAction = command.Action.SubAction; cmd.Payload = command.Payload; ReplySend(cmd); }
private CommandDefinition.CommandFormat ConstructCommand(CommandEnum command, BooleanValue ackReq, ActionEnum action, byte subAction) { CommandDefinition.CommandAckStruct cmdAck = new CommandDefinition.CommandAckStruct(command, ackReq, BooleanValue.False); CommandDefinition.ActionStruct a = new CommandDefinition.ActionStruct(action, subAction); CommandDefinition.CommandFormat cmd = new CommandDefinition.CommandFormat(cmdAck.CommandWithAck, a.ActionAndSubAction); return cmd; }
private void ResponseThreadMethod() { int length = Marshal.SizeOf(typeof(CommandDefinition.CommandFormat)); do { while (Connected) { try { byte[] response = new byte[length]; int dataRead = _networkStream.Read(response, 0, response.Length); if (dataRead > 0) { CommandDefinition.CommandFormat commandResponse = _commandResponse.Deserialize(response); if (commandResponse.CommandAck.Command == CommandEnum.SignOfLife) { SignOfLifeSequence = BitConverter.ToUInt32(commandResponse.Payload, 0); if (_signOfLifeTimer != null) _signOfLifeTimer.Change(_signOfLifeTimerDueTime, Timeout.Infinite); } else if (commandResponse.CommandAck.Command == CommandEnum.AdaptiveModeSpeed) { _adaptiveSpeed = BitConverter.ToSingle(commandResponse.Payload, 0); _speedMsgOpMode = (OperatingMode)commandResponse.Action.SubAction; SpeedMsgEvent.Set(); } else { _commandResponse = commandResponse; _responseEvent.Set(); if (ApcsUpdate != null) ApcsUpdate(commandResponse.CommandAck.Command, commandResponse.Action.Action, commandResponse.Action.SubAction, commandResponse.Payload); } } else throw new Exception("Data read from APCS has size 0. Restarting Connection."); } catch (Exception ex) { if (/*not terminated?*/ !_cancelEvent.WaitOne(0)) { Debug.Assert(Lgr != null); Lgr.LogError(ex); } if (_tcpClient != null && _tcpClient.Connected) { //CloseConnection(); _tcpClient = null; } } } } while (!_cancelEvent.WaitOne(ApcsDelay)); }