MessageWriter.vala 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * MessageWriter - Message writing to output stream
  3. *
  4. * Provides functionality for writing protocol messages to
  5. * an OutputStream with framing and length prefixes.
  6. *
  7. * @version 0.1
  8. * @since 0.1
  9. */
  10. namespace Implexus.Protocol {
  11. /**
  12. * Writes protocol messages to an OutputStream.
  13. *
  14. * Handles the binary message format including:
  15. * - Header serialization
  16. * - Payload writing
  17. * - Request ID assignment
  18. *
  19. * Example usage:
  20. * {{{
  21. * var writer = new MessageWriter(output_stream);
  22. * try {
  23. * var request = new GetEntityRequest.for_path(path);
  24. * var request_id = writer.write_request(request);
  25. * // wait for response with matching request_id
  26. * } catch (ProtocolError e) {
  27. * warning("Failed to write message: %s", e.message);
  28. * }
  29. * }}}
  30. */
  31. public class MessageWriter : Object {
  32. /**
  33. * The underlying output stream.
  34. */
  35. private OutputStream _stream;
  36. /**
  37. * The next request ID to assign.
  38. */
  39. private uint16 _next_request_id;
  40. /**
  41. * Whether the writer is closed.
  42. */
  43. private bool _closed = false;
  44. /**
  45. * Creates a new MessageWriter for the given stream.
  46. *
  47. * @param stream The output stream to write to
  48. */
  49. public MessageWriter(OutputStream stream) {
  50. _stream = stream;
  51. _next_request_id = 1;
  52. }
  53. /**
  54. * Writes a message to the stream.
  55. *
  56. * @param message The message to write
  57. * @throws ProtocolError if writing fails
  58. */
  59. public void write_message(Message message) throws ProtocolError {
  60. if (_closed) {
  61. throw new ProtocolError.CONNECTION_CLOSED("Writer is closed");
  62. }
  63. try {
  64. // Serialize the complete message
  65. var data = message.serialize();
  66. // Write to stream
  67. size_t bytes_written = 0;
  68. _stream.write_all(data, out bytes_written);
  69. _stream.flush();
  70. } catch (IOError e) {
  71. if (e is IOError.CLOSED) {
  72. _closed = true;
  73. }
  74. throw new ProtocolError.IO_ERROR("Write error: %s".printf(e.message));
  75. }
  76. }
  77. /**
  78. * Writes a message asynchronously.
  79. *
  80. * @param message The message to write
  81. * @param priority The I/O priority
  82. * @param cancellable Optional cancellation token
  83. * @throws ProtocolError if writing fails
  84. */
  85. public async void write_message_async(
  86. Message message,
  87. int priority = GLib.Priority.DEFAULT,
  88. Cancellable? cancellable = null
  89. ) throws ProtocolError {
  90. if (_closed) {
  91. throw new ProtocolError.CONNECTION_CLOSED("Writer is closed");
  92. }
  93. try {
  94. // Serialize the complete message
  95. var data = message.serialize();
  96. // Write to stream asynchronously
  97. size_t bytes_written = 0;
  98. yield _stream.write_all_async(data, priority, cancellable, out bytes_written);
  99. yield _stream.flush_async(priority, cancellable);
  100. } catch (IOError e) {
  101. if (e is IOError.CLOSED) {
  102. _closed = true;
  103. }
  104. throw new ProtocolError.IO_ERROR("Async write error: %s".printf(e.message));
  105. }
  106. }
  107. /**
  108. * Writes a request message and assigns a request ID.
  109. *
  110. * This method automatically assigns a unique request ID to
  111. * the message before writing it.
  112. *
  113. * @param request The request message to write
  114. * @return The assigned request ID
  115. * @throws ProtocolError if writing fails
  116. */
  117. public uint16 write_request(Message request) throws ProtocolError {
  118. var request_id = _next_request_id++;
  119. request.request_id = request_id;
  120. write_message(request);
  121. return request_id;
  122. }
  123. /**
  124. * Writes a request message asynchronously.
  125. *
  126. * @param request The request message to write
  127. * @param priority The I/O priority
  128. * @param cancellable Optional cancellation token
  129. * @return The assigned request ID
  130. * @throws ProtocolError if writing fails
  131. */
  132. public async uint16 write_request_async(
  133. Message request,
  134. int priority = GLib.Priority.DEFAULT,
  135. Cancellable? cancellable = null
  136. ) throws ProtocolError {
  137. var request_id = _next_request_id++;
  138. request.request_id = request_id;
  139. yield write_message_async(request, priority, cancellable);
  140. return request_id;
  141. }
  142. /**
  143. * Writes a response message with a specific request ID.
  144. *
  145. * @param response The response message to write
  146. * @param request_id The request ID to match
  147. * @throws ProtocolError if writing fails
  148. */
  149. public void write_response(Message response, uint16 request_id) throws ProtocolError {
  150. response.request_id = request_id;
  151. write_message(response);
  152. }
  153. /**
  154. * Writes a response message asynchronously.
  155. *
  156. * @param response The response message to write
  157. * @param request_id The request ID to match
  158. * @param priority The I/O priority
  159. * @param cancellable Optional cancellation token
  160. * @throws ProtocolError if writing fails
  161. */
  162. public async void write_response_async(
  163. Message response,
  164. uint16 request_id,
  165. int priority = GLib.Priority.DEFAULT,
  166. Cancellable? cancellable = null
  167. ) throws ProtocolError {
  168. response.request_id = request_id;
  169. yield write_message_async(response, priority, cancellable);
  170. }
  171. /**
  172. * Writes an error response.
  173. *
  174. * @param request_id The request ID to respond to
  175. * @param error_code The error code
  176. * @param error_message The error message
  177. * @throws ProtocolError if writing fails
  178. */
  179. public void write_error(uint16 request_id, int error_code, string error_message) throws ProtocolError {
  180. var error = new ErrorResponse.with_error(error_code, error_message);
  181. write_response(error, request_id);
  182. }
  183. /**
  184. * Writes a success response.
  185. *
  186. * @param request_id The request ID to respond to
  187. * @throws ProtocolError if writing fails
  188. */
  189. public void write_success(uint16 request_id) throws ProtocolError {
  190. var success = new SuccessResponse();
  191. write_response(success, request_id);
  192. }
  193. /**
  194. * Flushes the underlying stream.
  195. *
  196. * @throws ProtocolError if flushing fails
  197. */
  198. public void flush() throws ProtocolError {
  199. if (_closed) {
  200. throw new ProtocolError.CONNECTION_CLOSED("Writer is closed");
  201. }
  202. try {
  203. _stream.flush();
  204. } catch (IOError e) {
  205. throw new ProtocolError.IO_ERROR("Flush error: %s".printf(e.message));
  206. }
  207. }
  208. /**
  209. * Flushes the underlying stream asynchronously.
  210. *
  211. * @param priority The I/O priority
  212. * @param cancellable Optional cancellation token
  213. * @throws ProtocolError if flushing fails
  214. */
  215. public async void flush_async(
  216. int priority = GLib.Priority.DEFAULT,
  217. Cancellable? cancellable = null
  218. ) throws ProtocolError {
  219. if (_closed) {
  220. throw new ProtocolError.CONNECTION_CLOSED("Writer is closed");
  221. }
  222. try {
  223. yield _stream.flush_async(priority, cancellable);
  224. } catch (IOError e) {
  225. throw new ProtocolError.IO_ERROR("Async flush error: %s".printf(e.message));
  226. }
  227. }
  228. /**
  229. * Closes the writer.
  230. *
  231. * After closing, all write operations will throw CONNECTION_CLOSED.
  232. */
  233. public void close() {
  234. _closed = true;
  235. }
  236. /**
  237. * Whether the writer is closed.
  238. */
  239. public bool is_closed {
  240. get { return _closed; }
  241. }
  242. /**
  243. * The next request ID that will be assigned.
  244. */
  245. public uint16 next_request_id {
  246. get { return _next_request_id; }
  247. }
  248. }
  249. } // namespace Implexus.Protocol