| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package streamserver | 5 package streamserver |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "io" | 9 "io" |
| 10 "net" | 10 "net" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 rc io.ReadCloser | 25 rc io.ReadCloser |
| 26 // Negotiated stream properties. | 26 // Negotiated stream properties. |
| 27 properties *streamproto.Properties | 27 properties *streamproto.Properties |
| 28 } | 28 } |
| 29 | 29 |
| 30 // listenerStreamServer is the class for Listener-based stream server | 30 // listenerStreamServer is the class for Listener-based stream server |
| 31 // implementations. | 31 // implementations. |
| 32 type listenerStreamServer struct { | 32 type listenerStreamServer struct { |
| 33 context.Context | 33 context.Context |
| 34 | 34 |
| 35 // address is the string returned by the Address method. |
| 36 address string |
| 37 |
| 35 // gen is a generator function that is called to produce the stream serv
er's | 38 // gen is a generator function that is called to produce the stream serv
er's |
| 36 // Listener. On success, it returns the instantiated Listener, which wil
l be | 39 // Listener. On success, it returns the instantiated Listener, which wil
l be |
| 37 // closed when the stream server is closed. | 40 // closed when the stream server is closed. |
| 38 » gen func() (net.Listener, error) | 41 » // |
| 42 » // The string returned is the address for this server. |
| 43 » gen func() (net.Listener, string, error) |
| 39 l net.Listener | 44 l net.Listener |
| 40 laddr string | 45 laddr string |
| 41 | 46 |
| 42 streamParamsC chan *streamParams | 47 streamParamsC chan *streamParams |
| 43 closedC chan struct{} | 48 closedC chan struct{} |
| 44 acceptFinishedC chan struct{} | 49 acceptFinishedC chan struct{} |
| 45 | 50 |
| 46 // closed is an atomically-protected integer. If its value is 1, the str
eam | 51 // closed is an atomically-protected integer. If its value is 1, the str
eam |
| 47 // server has been closed. | 52 // server has been closed. |
| 48 closed int32 | 53 closed int32 |
| 49 | 54 |
| 50 // discardC is a testing channel. If not nil, failed client connections
will | 55 // discardC is a testing channel. If not nil, failed client connections
will |
| 51 // be written here. | 56 // be written here. |
| 52 discardC chan *streamClient | 57 discardC chan *streamClient |
| 53 } | 58 } |
| 54 | 59 |
| 55 var _ StreamServer = (*listenerStreamServer)(nil) | 60 var _ StreamServer = (*listenerStreamServer)(nil) |
| 56 | 61 |
| 57 func (s *listenerStreamServer) String() string { | 62 func (s *listenerStreamServer) String() string { |
| 58 return fmt.Sprintf("%T(%s)", s, s.laddr) | 63 return fmt.Sprintf("%T(%s)", s, s.laddr) |
| 59 } | 64 } |
| 60 | 65 |
| 66 func (s *listenerStreamServer) Address() string { |
| 67 if s.address == "" { |
| 68 panic("server must be listening to get its address") |
| 69 } |
| 70 return s.address |
| 71 } |
| 72 |
| 61 func (s *listenerStreamServer) Listen() error { | 73 func (s *listenerStreamServer) Listen() error { |
| 62 // Create a listener (OS-specific). | 74 // Create a listener (OS-specific). |
| 63 var err error | 75 var err error |
| 64 » s.l, err = s.gen() | 76 » s.l, s.address, err = s.gen() |
| 65 if err != nil { | 77 if err != nil { |
| 66 return err | 78 return err |
| 67 } | 79 } |
| 68 | 80 |
| 69 s.laddr = s.l.Addr().String() | 81 s.laddr = s.l.Addr().String() |
| 70 s.streamParamsC = make(chan *streamParams) | 82 s.streamParamsC = make(chan *streamParams) |
| 71 s.closedC = make(chan struct{}) | 83 s.closedC = make(chan struct{}) |
| 72 s.acceptFinishedC = make(chan struct{}) | 84 s.acceptFinishedC = make(chan struct{}) |
| 73 | 85 |
| 74 // Poll the Listener for new connections in a separate goroutine. This w
ill | 86 // Poll the Listener for new connections in a separate goroutine. This w
ill |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 | 263 |
| 252 // Decouples the active connection, returning it and setting the connection to | 264 // Decouples the active connection, returning it and setting the connection to |
| 253 // nil. | 265 // nil. |
| 254 func (c *streamClient) decoupleConn() (conn net.Conn) { | 266 func (c *streamClient) decoupleConn() (conn net.Conn) { |
| 255 c.decoupleMu.Lock() | 267 c.decoupleMu.Lock() |
| 256 defer c.decoupleMu.Unlock() | 268 defer c.decoupleMu.Unlock() |
| 257 | 269 |
| 258 conn, c.conn = c.conn, nil | 270 conn, c.conn = c.conn, nil |
| 259 return | 271 return |
| 260 } | 272 } |
| OLD | NEW |