IpcSerializer.hx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package asys.io;
  2. import haxe.NoData;
  3. import haxe.async.*;
  4. import haxe.io.*;
  5. import asys.net.Socket;
  6. /**
  7. Class used internally to send messages and handles over an IPC channel. See
  8. `Process.spawn` for creating an IPC channel and `Process.send` for sending
  9. messages over the channel.
  10. **/
  11. class IpcSerializer {
  12. static var activeSerializer:IpcSerializer = null;
  13. static var dummyBuffer = Bytes.ofString("s");
  14. final pipe:Socket;
  15. // final chunkSockets:Array<Socket> = [];
  16. function new(pipe:Socket) {
  17. this.pipe = pipe;
  18. }
  19. /**
  20. Sends `data` over the pipe. `data` will be serialized with a call to
  21. `haxe.Serializer.run`. Objects of type `Socket` can be sent along with the
  22. data if `handles` is provided.
  23. **/
  24. public function write(message:IpcMessage):Void {
  25. activeSerializer = this;
  26. if (message.sockets != null)
  27. for (socket in message.sockets) {
  28. if (!socket.connected)
  29. throw "cannot send unconnected socket over IPC";
  30. pipe.writeHandle(dummyBuffer, socket);
  31. }
  32. var serial = haxe.Serializer.run(message.message);
  33. pipe.write(Bytes.ofString('${serial.length}:$serial'));
  34. // chunkSockets.resize(0);
  35. activeSerializer = null;
  36. }
  37. /**
  38. // TODO: see `Socket.hxUnserialize` comment
  39. Sends `data` over the pipe. `data` will be serialized with a call to
  40. `haxe.Serializer.run`. However, objects of type `asys.async.net.Socket`
  41. will also be correctly serialized and can be received by the other end.
  42. **/
  43. }