private void ReplySendBase(CommandDefinition.CommandFormat reply) { try { lock (_sessionSocket) { byte[] buffer = reply.Serialize(); if (_sessionSocket.Send(buffer) < 1) { throw new Exception(); } } } catch { SessionClose(/*_sessionSocket == null*/); /*make ServerAgent reconnect*/ throw; } }
private CommandDefinition.CommandFormat?SendCommand(CommandDefinition.CommandFormat command, bool expectingReply) { CommandDefinition.CommandFormat?response = null; try { if (Connected) { byte[] cmd = command.Serialize(); _responseEvent.Reset(); _networkStream.Write(cmd, 0, cmd.Length); for (int i = 0; i < _sendCommandRetrys; i++) { if (_responseEvent.WaitOne(_apcsCommandTimeout)) { if (_commandResponse.CommandAck.Command == command.CommandAck.Command && _commandResponse.Action.Action == ActionEnum.Response) { response = _commandResponse; _responseEvent.Reset(); break; } } } } #if false else { throw new Exception("SendCommand called while not connected to APCS."); } #endif } catch (Exception ex) { if (/*not terminated?*/ !_cancelEvent.WaitOne(0)) { Debug.Assert(Lgr != null); Lgr.LogError(ex); } } return(response); }