Advertisement
Guest User

Ponylang batched TCP write

a guest
Aug 21st, 2019
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. use "net"
  2.  
  3. actor Main
  4. new create(env: Env) =>
  5. try
  6. TCPListener(env.root as AmbientAuth, recover Server end, "", "12345")
  7. end
  8.  
  9. primitive Response
  10. fun apply(): String =>
  11. "HTTP/1.1 200 Ok\r\nContent-Length: 11\r\n\r\nHello World"
  12.  
  13. class Server is TCPListenNotify
  14. let _response: String val
  15. new create() =>
  16. _response = recover val Response().repeat_str(18 * 1024) end
  17.  
  18. fun ref not_listening(self: TCPListener ref) =>
  19. self.close()
  20.  
  21. fun ref connected(self: TCPListener ref): TCPConnectionNotify iso^ =>
  22. recover Client(_response) end
  23.  
  24. class Client is TCPConnectionNotify
  25. let _response: String val
  26.  
  27. new create(response: String val) =>
  28. _response = response
  29.  
  30. fun ref connect_failed(self: TCPConnection ref) =>
  31. None
  32.  
  33. fun ref received(self: TCPConnection ref, data: Array[U8] iso, times: USize): Bool =>
  34. let requests = String.from_iso_array(consume data).count("\r\n\r\n")
  35. self.write(_response.trim(Response().size() * requests))
  36. true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement