Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. type Muxed struct {
  2. IsLast bool;
  3. Payload []byte;
  4. }
  5.  
  6. type State struct {
  7. ReplyTo chan interface{}
  8. }
  9.  
  10. // Incoming attaches [channel(buffer: 1)], if Mux finds multi-packet reply,
  11. // it pushes [channel(buffer: 2)] with 1st packet and swaps the channel in state
  12. func dealWithMux(packet Muxed, state State) {
  13. if packet.IsLast {
  14. state.ReplyTo <- packet.Payload
  15. } else {
  16. currentChan := state.ReplyTo
  17. state.ReplyTo = make(chan interface{}, 2)
  18. currentChan <- state.ReplyTo
  19. state.ReplyTo <- packet.Payload
  20. }
  21. }
  22.  
  23. // ReadResponse Continuosly reads a channel, containing either bytes or
  24. // continuations, until no more data is available. This allows for using
  25. // buffered channels, while still allowing indefinite streams of data.
  26. func ReadResponse(w io.Writer, stream chan interface{}) {
  27. for {
  28. result, hasData := <-stream
  29. if !hasData {
  30. return
  31. }
  32. continuation, isContinuation := result.(chan interface{})
  33. if (isContinuation) {
  34. stream = continuation
  35. } else {
  36. data, isData := result.([]byte)
  37. if isData {
  38. w.Write(data)
  39. }
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement