Browse Source

format code

joe 4 years ago
parent
commit
c8a5f019bb
12 changed files with 49 additions and 63 deletions
  1. 1 2
      errs.go
  2. 3 3
      iconn.go
  3. 15 15
      ihub.go
  4. 2 2
      internal/client_tcp.go
  5. 6 18
      internal/client_ws.go
  6. 0 2
      internal/conn_tcp.go
  7. 1 1
      internal/hub.go
  8. 0 1
      internal/server_tcp.go
  9. 11 12
      internal/session.go
  10. 3 0
      iprotocol.go
  11. 6 6
      isession.go
  12. 1 1
      ver.go

+ 1 - 2
errs.go

@@ -1,7 +1,7 @@
 package nnet
 
 import (
-"errors"
+	"errors"
 )
 
 var (
@@ -15,4 +15,3 @@ var (
 	ErrSliceOutOfRange        = errors.New("the slice is out of range")
 	ErrBufferSizeInsufficient = errors.New("buffer size is too small")
 )
-

+ 3 - 3
iconn.go

@@ -1,9 +1,9 @@
 package nnet
 
 import (
-"io"
-"net"
-"time"
+	"io"
+	"net"
+	"time"
 )
 
 type IConn interface {

+ 15 - 15
ihub.go

@@ -6,7 +6,7 @@ import (
 )
 
 var (
-	// Hub 状态
+	//
 	DefHubConfig = HubConfig{
 		SizeOfSendChan: 1024,
 		SizeOfRecvChan: 1024,
@@ -21,28 +21,28 @@ var (
 type HubConfig struct {
 	SizeOfSendChan uint32
 	SizeOfRecvChan uint32
-	ReadBufSize    int
-	WriteBufSize   int
-	Timeout        time.Duration // 发送等超时
-	Tick           time.Duration // 定时回调
-	ReadTimeout    time.Duration // 讀超時,如果為0,則無限等待。超時到達,意味著客戶端心跳丟失
+	ReadBufSize    int           // used in websocket
+	WriteBufSize   int           // used in websocket
+	Timeout        time.Duration // for write && listener
+	Tick           time.Duration // heartbeat callback interval
+	ReadTimeout    time.Duration // read timeout
 }
 
 type IHub interface {
 	Lock() // support locker semantics
 	Unlock()
 
-	Wg() *sync.WaitGroup        // object
-	ChQuit() <-chan struct{}    // 返回一个通道,用于退出 hub 循环
-	Conf() *HubConfig           // 返回配置信息
-	Callback() ISessionCallback // 返回回调对象
-	Protocol() IProtocol        // 返回数据协议
+	Wg() *sync.WaitGroup        // for waiting goroutines quit
+	ChQuit() <-chan struct{}    // notify to quit
+	Conf() *HubConfig           //
+	Callback() ISessionCallback // callback impl
+	Protocol() IProtocol        // protocol impl
 
-	Start() error // 启动 hub
-	Stop() error  // 停止 hub
-	DoJob(int)    // 执行 hub 中其他任务
+	Start() error // start hub
+	Stop() error  // stop hub
+	DoJob(int)    // not used now
 
-	PutSession(uint64, ISession) error // session 管理,这里的 session 必须基于 id
+	PutSession(uint64, ISession) error // session management base on id
 	DelSession(uint64) error
 	GetSession(uint64) (ISession, error)
 	PeekSession(uint64) (ISession, error)

+ 2 - 2
internal/client_tcp.go

@@ -20,7 +20,7 @@ func (self *TcpClient) Start() error {
 	return nil
 }
 
-func (self * TcpClient) NewConnection(addr string, id uint64) error {
+func (self *TcpClient) NewConnection(addr string, id uint64) error {
 	tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
 	if err != nil {
 		return err
@@ -48,4 +48,4 @@ func (self *TcpClient) Stop() error {
 	close(self.chQuit)
 	self.wg.Wait()
 	return nil
-}
+}

+ 6 - 18
internal/client_ws.go

@@ -1,25 +1,21 @@
 package internal
 
-
 import (
-	//	"math/rand"
-	//	"time"
-
 	"github.com/gorilla/websocket"
 	"one.com/nnet"
 )
 
-// 客户端组
+//
 type WsClient struct {
 	*Hub
-	addr 	string
-	pos   int // 指示当前连接第几个 addr
+	addr string
+	pos  int //
 }
 
 func NewWsClient(cf *nnet.HubConfig, cb nnet.ISessionCallback, p nnet.IProtocol) *WsClient {
 	return &WsClient{
-		Hub:   newHub(cf, cb, p),
-		pos:   0,
+		Hub: newHub(cf, cb, p),
+		pos: 0,
 	}
 }
 
@@ -27,7 +23,7 @@ func (self *WsClient) Start() error {
 	return nil
 }
 
-func (self *WsClient)NewConnection(addr string, id uint64) error {
+func (self *WsClient) NewConnection(addr string, id uint64) error {
 	conn, _, err := websocket.DefaultDialer.Dial(addr, nil)
 	if err != nil {
 		return err
@@ -47,14 +43,6 @@ func (self *WsClient) DoJob(int) {
 
 }
 
-//func (self *WsClient) ConnectRand(addrs []string) error {
-//	src := rand.NewSource(time.Now().UnixNano())
-//	rnd := rand.New(src)
-//	n := rnd.Intn(len(addrs))
-//	addr := addrs[n]
-//	return self.Connect(1, addr)
-//}
-
 func (self *WsClient) Stop() error {
 	close(self.chQuit)
 	self.wg.Wait()

+ 0 - 2
internal/conn_tcp.go

@@ -1,6 +1,5 @@
 package internal
 
-
 import (
 	"net"
 )
@@ -8,4 +7,3 @@ import (
 type TcpConn struct {
 	*net.TCPConn
 }
-

+ 1 - 1
internal/hub.go

@@ -51,7 +51,7 @@ func (self *Hub) Protocol() nnet.IProtocol {
 
 func (self *Hub) PutSession(id uint64, ses nnet.ISession) error {
 	self.Lock()
-	//@Notice: 顶替
+	//@Notice: replace
 	self.sess[id] = ses
 	self.Unlock()
 	return nil

+ 0 - 1
internal/server_tcp.go

@@ -1,6 +1,5 @@
 package internal
 
-
 import (
 	"net"
 	"one.com/nnet"

+ 11 - 12
internal/session.go

@@ -8,14 +8,13 @@ import (
 	"time"
 )
 
-// 关闭原因
 const (
-	// Close Reason 是一个 int32 型数据,这是系统预置的几个代码
-	CLOSE_REASON_READ          = 0
-	CLOSE_REASON_WRITE         = 0
-	CLOSE_REASON_PROTOCOL      = 1
-	CLOSE_REASON_READTIMEOUT   = 4  // HEARTBEAT
-	CLOSE_REASON_SERVER_CLOSED = 16 // 本服务器关闭
+	// Close Reason builtin
+	CLOSE_REASON_READ          = 1
+	CLOSE_REASON_WRITE         = 2
+	CLOSE_REASON_PROTOCOL      = 4
+	CLOSE_REASON_READTIMEOUT   = 8  // HEARTBEAT
+	CLOSE_REASON_SERVER_CLOSED = 16 //
 )
 
 // 长连接
@@ -25,8 +24,8 @@ type Session struct {
 	conn      nnet.IConn
 	extraData interface{}
 	once      sync.Once // Close once
-	closed    int32     // session 是否关闭
-	frozen    int32     // is session frozen. when no responding, no receiving, but can send.
+	closed    int32     // Flag: session closed or not
+	frozen    int32     // Flag: is session frozen. when no responding, no receiving, but can send.
 	chClose   chan struct{}
 	chSend    chan nnet.IPacket
 	chRecv    chan nnet.IPacket
@@ -153,7 +152,7 @@ func (self *Session) AWrite(pkt nnet.IPacket, timeout time.Duration) (err error)
 	}
 }
 
-// 循环从 socket 读取数据,置入 chRecv 通道
+//
 func (self *Session) loopRead() {
 	var reason int32 = 0
 
@@ -191,7 +190,7 @@ func (self *Session) loopRead() {
 	}
 }
 
-// 循环从 cbSend 通道读取数据,发送到 socket
+//
 func (self *Session) loopWrite() {
 	var reason int32 = 0
 
@@ -234,7 +233,7 @@ func (self *Session) loopWrite() {
 	}
 }
 
-// 循环从 chRecv 读取解析好的数据,回调到实现层
+//
 func (self *Session) loopHandle() {
 	var reason int32 = 0
 

+ 3 - 0
iprotocol.go

@@ -5,8 +5,11 @@ type IPacket interface {
 	ShouldClose() (bool, int32)
 }
 
+// Simple definition of Encoder/Decoder
+// NO middleware-like mechanism here, just do it directly within those 2 functions.
 type IProtocol interface {
 	// parse by raw data to a packet
 	ReadPacket(conn IConn) (IPacket, error)
+	// encode user-impl packet to []byte
 	Serialize(packet IPacket) ([]byte, error)
 }

+ 6 - 6
isession.go

@@ -9,20 +9,20 @@ var (
 )
 
 type ISession interface {
-	Do() // session 开始工作
+	Do() // session
 
 	SetFrozen(int32)
-	Close(int32) // 停止所有工作
+	Close(int32) //
 	IsClosed() bool
 
 	Write(IPacket, time.Duration) error
-	AWrite(IPacket, time.Duration) error // 异步发送
+	AWrite(IPacket, time.Duration) error // async write
 
-	GetData() interface{} // 辅助数据
+	GetData() interface{} // user data
 	SetData(interface{})
 
-	UpdateId(uint64) //更新ID
-	Id() uint64
+	UpdateId(uint64) // update session id, this will add session pointer into Hub, indexed by its id
+	Id() uint64      // get session id
 	SetId(uint64)
 
 	GetRawConn() IConn

+ 1 - 1
ver.go

@@ -4,6 +4,6 @@ const (
 	VERSION = "0.0.1"
 )
 
-func Version () string {
+func Version() string {
 	return VERSION
 }