Просмотр исходного кода

Moving the addr stuff to a single file

Armon Dadgar 10 лет назад
Родитель
Сommit
83a32c00ea
4 измененных файлов с 101 добавлено и 96 удалено
  1. 60 0
      addr.go
  2. 41 0
      const.go
  3. 0 85
      session.go
  4. 0 11
      stream.go

+ 60 - 0
addr.go

@@ -0,0 +1,60 @@
+package yamux
+
+import (
+	"fmt"
+	"net"
+)
+
+// hasAddr is used to get the address from the underlying connection
+type hasAddr interface {
+	LocalAddr() net.Addr
+	RemoteAddr() net.Addr
+}
+
+// yamuxAddr is used when we cannot get the underlying address
+type yamuxAddr struct {
+	Addr string
+}
+
+func (*yamuxAddr) Network() string {
+	return "yamux"
+}
+
+func (y *yamuxAddr) String() string {
+	return fmt.Sprintf("yamux:%s", y.Addr)
+}
+
+// Addr is used to get the address of the listener.
+func (s *Session) Addr() net.Addr {
+	return s.LocalAddr()
+}
+
+// LocalAddr is used to get the local address of the
+// underlying connection.
+func (s *Session) LocalAddr() net.Addr {
+	addr, ok := s.conn.(hasAddr)
+	if !ok {
+		return &yamuxAddr{"local"}
+	}
+	return addr.LocalAddr()
+}
+
+// RemoteAddr is used to get the address of remote end
+// of the underlying connection
+func (s *Session) RemoteAddr() net.Addr {
+	addr, ok := s.conn.(hasAddr)
+	if !ok {
+		return &yamuxAddr{"remote"}
+	}
+	return addr.RemoteAddr()
+}
+
+// LocalAddr returns the local address
+func (s *Stream) LocalAddr() net.Addr {
+	return s.session.LocalAddr()
+}
+
+// LocalAddr returns the remote address
+func (s *Stream) RemoteAddr() net.Addr {
+	return s.session.RemoteAddr()
+}

+ 41 - 0
const.go

@@ -5,6 +5,47 @@ import (
 	"fmt"
 )
 
+var (
+	// ErrInvalidVersion means we received a frame with an
+	// invalid version
+	ErrInvalidVersion = fmt.Errorf("invalid protocol version")
+
+	// ErrInvalidMsgType means we received a frame with an
+	// invalid message type
+	ErrInvalidMsgType = fmt.Errorf("invalid msg type")
+
+	// ErrSessionShutdown is used if there is a shutdown during
+	// an operation
+	ErrSessionShutdown = fmt.Errorf("session shutdown")
+
+	// ErrStreamsExhausted is returned if we have no more
+	// stream ids to issue
+	ErrStreamsExhausted = fmt.Errorf("streams exhausted")
+
+	// ErrDuplicateStream is used if a duplicate stream is
+	// opened inbound
+	ErrDuplicateStream = fmt.Errorf("duplicate stream initiated")
+
+	// ErrMissingStream indicates a stream was named which
+	// does not exist.
+	ErrMissingStream = fmt.Errorf("missing stream references")
+
+	// ErrReceiveWindowExceeded indicates the window was exceeded
+	ErrRecvWindowExceeded = fmt.Errorf("recv window exceeded")
+
+	// ErrTimeout is used when we reach an IO deadline
+	ErrTimeout = fmt.Errorf("i/o deadline reached")
+
+	// ErrStreamClosed is returned when using a closed stream
+	ErrStreamClosed = fmt.Errorf("stream closed")
+
+	// ErrUnexpectedFlag is set when we get an unexpected flag
+	ErrUnexpectedFlag = fmt.Errorf("unexpected flag")
+
+	// ErrRemoteGoAway is used when we get a go away from the other side
+	ErrRemoteGoAway = fmt.Errorf("remote end is not accepting connections")
+)
+
 const (
 	// protoVersion is the only version we support
 	protoVersion uint8 = 0

+ 0 - 85
session.go

@@ -9,47 +9,6 @@ import (
 	"time"
 )
 
-var (
-	// ErrInvalidVersion means we received a frame with an
-	// invalid version
-	ErrInvalidVersion = fmt.Errorf("invalid protocol version")
-
-	// ErrInvalidMsgType means we received a frame with an
-	// invalid message type
-	ErrInvalidMsgType = fmt.Errorf("invalid msg type")
-
-	// ErrSessionShutdown is used if there is a shutdown during
-	// an operation
-	ErrSessionShutdown = fmt.Errorf("session shutdown")
-
-	// ErrStreamsExhausted is returned if we have no more
-	// stream ids to issue
-	ErrStreamsExhausted = fmt.Errorf("streams exhausted")
-
-	// ErrDuplicateStream is used if a duplicate stream is
-	// opened inbound
-	ErrDuplicateStream = fmt.Errorf("duplicate stream initiated")
-
-	// ErrMissingStream indicates a stream was named which
-	// does not exist.
-	ErrMissingStream = fmt.Errorf("missing stream references")
-
-	// ErrReceiveWindowExceeded indicates the window was exceeded
-	ErrRecvWindowExceeded = fmt.Errorf("recv window exceeded")
-
-	// ErrTimeout is used when we reach an IO deadline
-	ErrTimeout = fmt.Errorf("i/o deadline reached")
-
-	// ErrStreamClosed is returned when using a closed stream
-	ErrStreamClosed = fmt.Errorf("stream closed")
-
-	// ErrUnexpectedFlag is set when we get an unexpected flag
-	ErrUnexpectedFlag = fmt.Errorf("unexpected flag")
-
-	// ErrRemoteGoAway is used when we get a go away from the other side
-	ErrRemoteGoAway = fmt.Errorf("remote end is not accepting connections")
-)
-
 // Session is used to wrap a reliable ordered connection and to
 // multiplex it into multiple streams.
 type Session struct {
@@ -97,25 +56,6 @@ type Session struct {
 	shutdownLock sync.Mutex
 }
 
-// hasAddr is used to get the address from the underlying connection
-type hasAddr interface {
-	LocalAddr() net.Addr
-	RemoteAddr() net.Addr
-}
-
-// yamuxAddr is used when we cannot get the underlying address
-type yamuxAddr struct {
-	Addr string
-}
-
-func (*yamuxAddr) Network() string {
-	return "yamux"
-}
-
-func (y *yamuxAddr) String() string {
-	return fmt.Sprintf("yamux:%s", y.Addr)
-}
-
 // sendReady is used to either mark a stream as ready
 // or to directly send a header
 type sendReady struct {
@@ -235,31 +175,6 @@ func (s *Session) GoAway() error {
 	return nil
 }
 
-// Addr is used to get the address of the listener.
-func (s *Session) Addr() net.Addr {
-	return s.LocalAddr()
-}
-
-// LocalAddr is used to get the local address of the
-// underlying connection.
-func (s *Session) LocalAddr() net.Addr {
-	addr, ok := s.conn.(hasAddr)
-	if !ok {
-		return &yamuxAddr{"local"}
-	}
-	return addr.LocalAddr()
-}
-
-// RemoteAddr is used to get the address of remote end
-// of the underlying connection
-func (s *Session) RemoteAddr() net.Addr {
-	addr, ok := s.conn.(hasAddr)
-	if !ok {
-		return &yamuxAddr{"remote"}
-	}
-	return addr.RemoteAddr()
-}
-
 // Ping is used to measure the RTT response time
 func (s *Session) Ping() (time.Duration, error) {
 	// Get a channel for the ping

+ 0 - 11
stream.go

@@ -5,7 +5,6 @@ import (
 	"compress/lzw"
 	"io"
 	"log"
-	"net"
 	"sync"
 	"time"
 )
@@ -277,16 +276,6 @@ func (s *Stream) forceClose() {
 	asyncNotify(s.notifyCh)
 }
 
-// LocalAddr returns the local address
-func (s *Stream) LocalAddr() net.Addr {
-	return s.session.LocalAddr()
-}
-
-// LocalAddr returns the remote address
-func (s *Stream) RemoteAddr() net.Addr {
-	return s.session.RemoteAddr()
-}
-
 // SetDeadline sets the read and write deadlines
 func (s *Stream) SetDeadline(t time.Time) error {
 	if err := s.SetReadDeadline(t); err != nil {