public bool ReadBytes(uint address, byte[] buffer) { uint id = NextID; _message_handler($"LuaConnector is reading: [${address:X6}]"); LuaBlock block = new LuaBlock(id, LuaBlock.CommandType.ReadBlock) { Address = address, Block = buffer }; if (!WriteBlock(block)) { return(false); } _response_events.Put(id, new ManualResetEvent(false)); // ReSharper disable once PossibleNullReferenceException if (!_response_events[id].WaitOne()) { return(false); } _response_values[id].Block.CopyTo(buffer, 0); return(true); }
/// <summary> /// Writes a LuaBlock to the socket. /// </summary> /// <param name="block">The LuaBlock to send.</param> /// <returns>True if successful, otherwise false.</returns> private bool WriteBlock([NotNull] LuaBlock block) { /*if ((Mapper != null) && (block.Address != 0)) * { * (uint offset, string domain) t = Mapper.Translate(block.Address); * block.Domain = t.domain; * }*/ if (_stream == null) { return(false); } string json = JsonConvert.SerializeObject(block); if (json == null) { return(false); } byte[] buffer = Encoding.UTF8.GetBytes(json); try { lock (_stream) { _stream.Write(buffer, 0, buffer.Length); _stream.WriteByte(0); } } catch { return(false); } return(true); }
/// <summary> /// Sends a message to be displayed. /// </summary> /// <param name="message">The message text.</param> /// <returns>True if the text display was successful, otherwise false.</returns> /// <remarks>Connector implementations that do not support messaging should return false rather than throwing a NotSupportedException.</remarks> public bool SendMessage(string message) { LuaBlock block = new LuaBlock(NextID, LuaBlock.CommandType.SendMessage) { Message = message ?? string.Empty }; return(WriteBlock(block)); }
public bool WriteBytes(uint address, byte[] data) { _message_handler($"LuaConnector is writing {data?.Length??0} bytes: [${address:X6}]"); LuaBlock block = new LuaBlock(NextID, LuaBlock.CommandType.WriteWord) { Address = address, Block = data }; return(WriteBlock(block)); }
public bool WriteU16(uint address, ushort value) { _message_handler($"LuaConnector is writing: [${address:X6} = #${value:X4}]"); LuaBlock block = new LuaBlock(NextID, LuaBlock.CommandType.WriteWord) { Address = address, Value = value }; return(WriteBlock(block)); }
public bool UnsetBit(uint address, byte value) { _message_handler($"LuaConnector is clearing a bit: [${address:X6} = #${value:X2}]"); LuaBlock block = new LuaBlock(NextID, LuaBlock.CommandType.UnsetBits) { Address = address, Value = value }; return(WriteBlock(block)); }
/// <summary> /// Reads and enqueues a LuaBlock JSON string. /// </summary> /// <param name="json">The JSON string describing the LuaBlock to be processed.</param> private void ProcessBlock([NotNull] string json) { LuaBlock request = JsonConvert.DeserializeObject <LuaBlock>(json); if (request == null) { return; } if (request.Type != LuaBlock.CommandType.KeepAlive) { _message_handler($"LuaConnector got a block. [{request.ID}]"); } _response_values.Put(request.ID, request); if (_response_events.ContainsKey(request.ID)) { _response_events[request.ID]?.Set(); } }
public byte?ReadU8(uint address) { uint id = NextID; _message_handler($"LuaConnector is reading: [${address:X6}]"); LuaBlock block = new LuaBlock(id, LuaBlock.CommandType.ReadByte) { Address = address }; if (!WriteBlock(block)) { return(null); } _response_events.Put(id, new ManualResetEvent(false)); // ReSharper disable once PossibleNullReferenceException _response_events[id].WaitOne(); return((byte?)_response_values[id].Value); }