private void OnSocketMessageAvailable(ProsthesisMessage message, TCP.ConnectionState state) { //Only one message allowed in at a time lock (this) { ProsthesisStateBase newState = mCurrentState; //Capture handshakes and send appropriate events if (message is ProsthesisHandshakeRequest) { ProsthesisHandshakeRequest hsReq = message as ProsthesisHandshakeRequest; if (hsReq.VersionId == ProsthesisCore.ProsthesisConstants.OSVersion) { newState = mCurrentState.OnClientAuthorization(state, true); mAuthorizedConnection = state; } else { newState = mCurrentState.OnClientAuthorization(state, false); } } else if (state == mAuthorizedConnection) { //Capture commands and send appropriate events if (message is ProsthesisCommand) { ProsthesisCommand command = message as ProsthesisCommand; mLogger.LogMessage(Logger.LoggerChannels.Events, string.Format("Received command {0} from {1}", command.Command, state.RemoteEndPoint)); ProsthesisCommandAck ack = new ProsthesisCommandAck(); ack.Command = command.Command; ack.Timestamp = System.DateTime.Now.Ticks; ProsthesisDataPacket pack = ProsthesisDataPacket.BoxMessage <ProsthesisCommandAck>(ack); state._conn.Send(pack.Bytes, pack.Bytes.Length, System.Net.Sockets.SocketFlags.None); if (command.Command == ProsthesisConstants.ProsthesisCommand.EmergencyStop) { Terminate(string.Format("Emergency stop from {0}", state.RemoteEndPoint)); } else { newState = mCurrentState.OnProsthesisCommand(command.Command, state); } } else { newState = mCurrentState.OnSocketMessage(message, state); } } else { state._server.DropConnection(state); } ChangeState(newState); } }
private static void SendCommand(ProsthesisCore.ProsthesisConstants.ProsthesisCommand command) { if (mClient != null && mClient.Connected) { ProsthesisCommand cmd = new ProsthesisCommand(); cmd.Command = command; mLogger.LogMessage(Logger.LoggerChannels.Events, string.Format("Sending command {0}", command)); ProsthesisDataPacket packet = ProsthesisDataPacket.BoxMessage <ProsthesisCommand>(cmd); mClient.Send(packet.Bytes, 0, packet.Bytes.Length); } }
private static void SendCommand(ProsthesisCore.ProsthesisConstants.ProsthesisCommand command) { if (mClient != null && mClient.Connected) { ProsthesisCommand cmd = new ProsthesisCommand(); cmd.Command = command; mLogger.LogMessage(Logger.LoggerChannels.Events, string.Format("Sending command {0}", command)); ProsthesisDataPacket packet = ProsthesisDataPacket.BoxMessage<ProsthesisCommand>(cmd); mClient.Send(packet.Bytes, 0, packet.Bytes.Length); } }