Serve reads and dispatches messages until ctx is canceled or in hits EOF.
(ctx context.Context)
| 88 | |
| 89 | // Serve reads and dispatches messages until ctx is canceled or in hits EOF. |
| 90 | func (s *Server) Serve(ctx context.Context) error { |
| 91 | scanner := bufio.NewScanner(s.in) |
| 92 | scanner.Buffer(make([]byte, 0, 64*1024), 8*1024*1024) // allow large payloads |
| 93 | |
| 94 | for scanner.Scan() { |
| 95 | if ctx.Err() != nil { |
| 96 | return ctx.Err() |
| 97 | } |
| 98 | line := scanner.Bytes() |
| 99 | if len(line) == 0 { |
| 100 | continue |
| 101 | } |
| 102 | s.dispatch(ctx, line) |
| 103 | } |
| 104 | return scanner.Err() |
| 105 | } |
| 106 | |
| 107 | func (s *Server) dispatch(ctx context.Context, line []byte) { |
| 108 | var req Request |