ServerInput.vala 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Invercargill;
  2. using Invercargill.DataStructures;
  3. namespace Astralis {
  4. internal class ServerInput : Object, AsyncInput {
  5. private bool write_occurred;
  6. private Series<ByteBuffer> chunks = new Series<ByteBuffer>();
  7. private bool writes_complete;
  8. public BinaryData? peek() {
  9. return chunks.first_or_default();
  10. }
  11. public async BinaryData? read() throws Error {
  12. while (chunks.length == 0 && !writes_complete) {
  13. Idle.add(read.callback);
  14. yield;
  15. }
  16. if(chunks.length == 0) {
  17. return null;
  18. }
  19. return chunks.pop_start();
  20. }
  21. public async BinaryData read_all() {
  22. while (!writes_complete) {
  23. Idle.add(read_all.callback);
  24. yield;
  25. }
  26. var data = new ByteComposition();
  27. data.append_all(chunks);
  28. chunks.clear();
  29. return data;
  30. }
  31. internal void write(uint8[] data) {
  32. chunks.add(new ByteBuffer.from_byte_array(data));
  33. write_occurred = true;
  34. }
  35. internal void complete() {
  36. writes_complete = true;
  37. }
  38. }
  39. }