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)); }
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(); } } }