IpcSerializer.hx 1.4 KB

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