private void _SendLog(MasterCommandLogOpcode op, int code, params string[] strings) { //Determine size int size = 8; foreach (var s in strings) { size += 4 + Encoding.UTF8.GetByteCount(s); } //Allocate buffer and begin writing byte[] b = new byte[size]; BitConverter.GetBytes((short)op).CopyTo(b, 0); BitConverter.GetBytes((short)strings.Length).CopyTo(b, 2); BitConverter.GetBytes(code).CopyTo(b, 4); //Write strings int pos = 8; foreach (var s in strings) { byte[] stringBytes = Encoding.UTF8.GetBytes(s); BitConverter.GetBytes(stringBytes.Length).CopyTo(b, pos); pos += 4; stringBytes.CopyTo(b, pos); pos += stringBytes.Length; } //Send message.Respond(b, op == MasterCommandLogOpcode.FINISHED_FAIL || op == MasterCommandLogOpcode.FINISHED_SUCCESS); }
private void ProcessProxiedMessage(System.Threading.Channels.ChannelReader <RouterMessage> channel, RouterSession session, RouterMessage msg) { channel.ReadAsync().AsTask().ContinueWith((Task <RouterMessage> m) => { msg.Respond(m.Result.payload, m.Result.flagIsLast); if (!m.Result.flagIsLast) { ProcessProxiedMessage(channel, session, msg); } }); }
private async Task OnCmdGetInstanceStatus(RouterMessage msg) { //Create buffer for output. It consists of this format, each a byte unless otherwise stated: [status, reserved, appVersionMajor, appVersionMinor, libVersionMajor, libVersionMinor, (ushort)time] byte[] buffer = new byte[8]; buffer[0] = (byte)InstanceStatusResult.NOT_CONNECTED; //Find instance ManagerInstance instance = session.GetInstanceById(BitConverter.ToInt64(msg.payload, 0)); if (instance != null && instance.linkedSession != null) { try { //Send ping DateTime start = DateTime.UtcNow; var pingResult = await instance.linkedSession.SendPing(4000); if (pingResult != null) { //Success buffer[0] = (byte)InstanceStatusResult.ONLINE; buffer[2] = pingResult.Value.lib_version_major; buffer[3] = pingResult.Value.lib_version_minor; buffer[4] = pingResult.Value.app_version_major; buffer[5] = pingResult.Value.app_version_minor; BitConverter.GetBytes((ushort)Math.Min(ushort.MaxValue, (DateTime.UtcNow - start).TotalMilliseconds)).CopyTo(buffer, 6); } else { //Timed out buffer[0] = (byte)InstanceStatusResult.PING_TIMED_OUT; } } catch { buffer[0] = (byte)InstanceStatusResult.PING_FAILED; } } //Send msg.Respond(buffer, true); }
private void HandleGetUserConfigCmd(MasterControlClient session, RouterMessage msg) { //Read strings int nameLen = BitConverter.ToInt32(msg.payload, 0); string name = Encoding.UTF8.GetString(msg.payload, 4, nameLen); int defaultLen = BitConverter.ToInt32(msg.payload, nameLen + 4); string defaultValue = Encoding.UTF8.GetString(msg.payload, 4 + nameLen + 4, defaultLen); //Get file path string path = Program.cfg.general.sub_configs_directory + name; //Create default if needed if (!File.Exists(path)) { File.WriteAllText(path, defaultValue); } //Load and return string content = File.ReadAllText(path); msg.Respond(Encoding.UTF8.GetBytes(content), true); }