Skip to content

Commit

Permalink
mod 侦听器和连接器都拥有port
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Aug 16, 2018
1 parent 83e3686 commit 9d03309
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
5 changes: 2 additions & 3 deletions peer/tcp/acceptor.go
@@ -1,7 +1,6 @@
package tcp

import (
"fmt"
"github.com/davyxu/cellnet"
"github.com/davyxu/cellnet/peer"
"github.com/davyxu/cellnet/util"
Expand All @@ -21,7 +20,7 @@ type tcpAcceptor struct {
listener net.Listener
}

func (self *tcpAcceptor) ListenPort() int {
func (self *tcpAcceptor) Port() int {
if self.listener == nil {
return 0
}
Expand Down Expand Up @@ -65,7 +64,7 @@ func (self *tcpAcceptor) ListenAddress() string {
return self.Address()
}

return fmt.Sprintf("%s:%d", host, self.ListenPort())
return util.JoinAddress(host, self.Port())
}

func (self *tcpAcceptor) accept() {
Expand Down
8 changes: 8 additions & 0 deletions peer/tcp/connector.go
Expand Up @@ -75,6 +75,14 @@ func (self *tcpConnector) SetReconnectDuration(v time.Duration) {
self.reconDur = v
}

func (self *tcpConnector) Port() int {
if self.defaultSes.conn == nil {
return 0
}

return self.defaultSes.conn.LocalAddr().(*net.TCPAddr).Port
}

const reportConnectFailedLimitTimes = 3

// 连接器,传入连接地址和发送封包次数
Expand Down
8 changes: 8 additions & 0 deletions peer/tcp/syncconn.go
Expand Up @@ -18,6 +18,14 @@ type tcpSyncConnector struct {
defaultSes *tcpSession
}

func (self *tcpSyncConnector) Port() int {
if self.defaultSes.conn == nil {
return 0
}

return self.defaultSes.conn.LocalAddr().(*net.TCPAddr).Port
}

func (self *tcpSyncConnector) Start() cellnet.Peer {

// 尝试用Socket连接地址
Expand Down
4 changes: 3 additions & 1 deletion peertype.go
Expand Up @@ -58,6 +58,8 @@ type TCPConnector interface {

// 设置会话管理器 实现peer.SessionManager接口
SetSessionManager(raw interface{})

Port() int
}

// TCP接受器,具备会话访问
Expand All @@ -70,7 +72,7 @@ type TCPAcceptor interface {
TCPSocketOption

// 查看当前侦听端口,使用host:0 作为Address时,socket底层自动分配侦听端口
ListenPort() int
Port() int
}

// UDP连接器
Expand Down

0 comments on commit 9d03309

Please sign in to comment.