2424 ErrWrongDirection = errors .New ("Wrong channel direction" )
2525)
2626
27- // session is a transport session on top of a network
27+ // SpdyTransport is a transport session on top of a network
2828// connection using spdy.
29- type session struct {
29+ type SpdyTransport struct {
3030 conn * spdystream.Connection
3131 handler codec.Handle
3232
@@ -44,32 +44,32 @@ type session struct {
4444
4545type channel struct {
4646 stream * spdystream.Stream
47- session * session
47+ session * SpdyTransport
4848 direction direction
4949}
5050
51- // NewClientSession creates a new stream session from the
51+ // NewClientTransport creates a new stream transport from the
5252// provided network connection. The network connection is
5353// expected to already provide a tls session.
54- func NewClientSession (conn net.Conn ) (libchan. Transport , error ) {
54+ func NewClientTransport (conn net.Conn ) (* SpdyTransport , error ) {
5555 return newSession (conn , false )
5656}
5757
58- // NewServerSession creates a new stream session from the
58+ // NewServerTransport creates a new stream transport from the
5959// provided network connection. The network connection is
6060// expected to already have completed the tls handshake.
61- func NewServerSession (conn net.Conn ) (libchan. Transport , error ) {
61+ func NewServerTransport (conn net.Conn ) (* SpdyTransport , error ) {
6262 return newSession (conn , true )
6363}
6464
65- func newSession (conn net.Conn , server bool ) (* session , error ) {
65+ func newSession (conn net.Conn , server bool ) (* SpdyTransport , error ) {
6666 var referenceCounter uint64
6767 if server {
6868 referenceCounter = 2
6969 } else {
7070 referenceCounter = 1
7171 }
72- session := & session {
72+ session := & SpdyTransport {
7373 streamChan : make (chan * spdystream.Stream ),
7474 referenceCounter : referenceCounter ,
7575 byteStreamC : sync .NewCond (new (sync.Mutex )),
@@ -91,7 +91,7 @@ func newSession(conn net.Conn, server bool) (*session, error) {
9191 return session , nil
9292}
9393
94- func (s * session ) newStreamHandler (stream * spdystream.Stream ) {
94+ func (s * SpdyTransport ) newStreamHandler (stream * spdystream.Stream ) {
9595 referenceIdString := stream .Headers ().Get ("libchan-ref" )
9696
9797 returnHeaders := http.Header {}
@@ -120,7 +120,7 @@ func (s *session) newStreamHandler(stream *spdystream.Stream) {
120120 stream .SendReply (returnHeaders , finish )
121121}
122122
123- func (s * session ) getByteStream (referenceId uint64 ) * byteStream {
123+ func (s * SpdyTransport ) getByteStream (referenceId uint64 ) * byteStream {
124124 s .byteStreamC .L .Lock ()
125125 bs , ok := s .byteStreams [referenceId ]
126126 if ! ok {
@@ -131,7 +131,7 @@ func (s *session) getByteStream(referenceId uint64) *byteStream {
131131 return bs
132132}
133133
134- func (s * session ) dial (referenceId uint64 ) (* byteStream , error ) {
134+ func (s * SpdyTransport ) dial (referenceId uint64 ) (* byteStream , error ) {
135135 headers := http.Header {}
136136 headers .Set ("libchan-ref" , strconv .FormatUint (referenceId , 10 ))
137137 stream , streamErr := s .conn .CreateStream (headers , nil , false )
@@ -145,7 +145,7 @@ func (s *session) dial(referenceId uint64) (*byteStream, error) {
145145 return bs , nil
146146}
147147
148- func (s * session ) createByteStream () (io.ReadWriteCloser , error ) {
148+ func (s * SpdyTransport ) createByteStream () (io.ReadWriteCloser , error ) {
149149 s .referenceLock .Lock ()
150150 referenceId := s .referenceCounter
151151 s .referenceCounter = referenceId + 2
@@ -172,7 +172,14 @@ func addrKey(local, remote string) string {
172172 return string (b )
173173}
174174
175- func (s * session ) RegisterConn (conn net.Conn ) error {
175+ // RegisterConn registers a network connection to be used
176+ // by inbound messages referring to the connection
177+ // with the registered connection's local and remote address.
178+ // Note: a connection does not need to be registered before
179+ // being sent in a message, but does need to be registered
180+ // to by the receiver of a message. If registration should be
181+ // automatic, register a listener instead.
182+ func (s * SpdyTransport ) RegisterConn (conn net.Conn ) error {
176183 s .netConnC .L .Lock ()
177184 defer s .netConnC .L .Unlock ()
178185 networkType , ok := s .networks [conn .LocalAddr ().Network ()]
@@ -186,7 +193,9 @@ func (s *session) RegisterConn(conn net.Conn) error {
186193 return nil
187194}
188195
189- func (s * session ) RegisterListener (listener net.Listener ) {
196+ // RegisterListener accepts all connections from the listener
197+ // and immediately registers them.
198+ func (s * SpdyTransport ) RegisterListener (listener net.Listener ) {
190199 go func () {
191200 for {
192201 conn , err := listener .Accept ()
@@ -203,18 +212,24 @@ func (s *session) RegisterListener(listener net.Listener) {
203212 }()
204213}
205214
206- func (s * session ) Unregister (net.Conn ) {
215+ // Unregister removes the connection from the list of known
216+ // connections. This should be called when a connection is
217+ // closed and no longer expected in inbound messages.
218+ // Failure to unregister connections will increase memory
219+ // usage since the transport is not notified of closed
220+ // connections to automatically unregister.
221+ func (s * SpdyTransport ) Unregister (net.Conn ) {
207222
208223}
209224
210- func (s * session ) Close () error {
225+ func (s * SpdyTransport ) Close () error {
211226 return s .conn .Close ()
212227}
213228
214229// NewSendChannel creates and returns a new send channel. The receive
215230// end will get picked up on the remote end through the remote calling
216231// WaitReceiveChannel.
217- func (s * session ) NewSendChannel () (libchan.Sender , error ) {
232+ func (s * SpdyTransport ) NewSendChannel () (libchan.Sender , error ) {
218233 stream , streamErr := s .conn .CreateStream (http.Header {}, nil , false )
219234 if streamErr != nil {
220235 return nil , streamErr
@@ -224,7 +239,7 @@ func (s *session) NewSendChannel() (libchan.Sender, error) {
224239
225240// WaitReceiveChannel waits for a new channel be created by a remote
226241// call to NewSendChannel.
227- func (s * session ) WaitReceiveChannel () (libchan.Receiver , error ) {
242+ func (s * SpdyTransport ) WaitReceiveChannel () (libchan.Receiver , error ) {
228243 stream , ok := <- s .streamChan
229244 if ! ok {
230245 return nil , io .EOF
0 commit comments