Documentation ¶
Index ¶
- Constants
- Variables
- func Blue(s interface{}) string
- func Green(s interface{}) string
- func Magenta(s interface{}) string
- func Red(s interface{}) string
- func Yellow(s interface{}) string
- type BroadcastServerMessageListener
- type ClientCodec
- type ClientDefaultCodec
- type ClientMessageListener
- type ClientPacketHandler
- type Codec
- type ConnectHandler
- type DebugLogger
- func (d *DebugLogger) Fatal(v ...interface{})
- func (d *DebugLogger) Fatalf(format string, v ...interface{})
- func (d *DebugLogger) Fatalln(v ...interface{})
- func (d *DebugLogger) Panic(v ...interface{})
- func (d *DebugLogger) Panicf(format string, v ...interface{})
- func (d *DebugLogger) Panicln(v ...interface{})
- func (d *DebugLogger) Print(v ...interface{})
- func (d *DebugLogger) Printf(format string, v ...interface{})
- func (d *DebugLogger) Println(v ...interface{})
- func (d *DebugLogger) SetDebugMode(on bool)
- type DefaultCodec
- type ExampleClientMessageListener
- type ExampleServerMessageListener
- type ExampleSessionListener
- type Logger
- type MessageListener
- type Packet
- type PacketHandler
- type Session
- func (s *Session) Attr(key string) interface{}
- func (s *Session) CloseSession(reason string)
- func (s *Session) CreateTime() time.Time
- func (s *Session) Heartbeat() time.Duration
- func (s *Session) IsClosed() bool
- func (s *Session) LastActive() time.Time
- func (s *Session) ReadDeadlin() time.Duration
- func (s *Session) RemoteAddr() string
- func (s *Session) SID() string
- func (s *Session) SendMessage(message interface{})
- func (s *Session) ServerRef() *TCPServer
- func (s *Session) SetAttr(key string, val string) *Session
- func (s *Session) SetHeartbeat(heartbeat time.Duration)
- func (s *Session) SetReadDeadline(readDeadine time.Duration)
- func (s *Session) SetWriteDeadline(writeDeadline time.Duration)
- func (s *Session) UpdateLastActive()
- func (s *Session) WriteDeadline() time.Duration
- type SessionListener
- type SessionWriter
- type TCPClient
- func (cli *TCPClient) Dial() (*TCPClient, error)
- func (cli *TCPClient) Hangup(reason string)
- func (cli *TCPClient) RegisterMessageListener(listener ClientMessageListener) *TCPClient
- func (cli *TCPClient) RemoteAddr() string
- func (cli *TCPClient) SendMessage(msg interface{}) error
- func (cli *TCPClient) SetCodec(codec ClientCodec) *TCPClient
- func (cli *TCPClient) SetDebugMode(on bool) *TCPClient
- func (cli *TCPClient) SetLogger(debugLogger Logger, logger Logger) *TCPClient
- func (cli *TCPClient) SetMaxPacketBodyLength(maxMsgBodyLen uint32) *TCPClient
- func (cli *TCPClient) SetSessionReadDeadline(read time.Duration) *TCPClient
- func (cli *TCPClient) SetSessionWriteDeadline(write time.Duration) *TCPClient
- func (cli *TCPClient) UpdateLastActive()
- type TCPServer
- func (ts *TCPServer) RegisterMessageListener(listener MessageListener) *TCPServer
- func (ts *TCPServer) RegisterSessionListener(listener SessionListener) *TCPServer
- func (ts *TCPServer) Run() (*TCPServer, error)
- func (ts *TCPServer) Sessions() map[string]*Session
- func (ts *TCPServer) SetCodec(codec Codec) *TCPServer
- func (ts *TCPServer) SetDebugMode(on bool) *TCPServer
- func (ts *TCPServer) SetDefaultSessionReadDeadline(read time.Duration) *TCPServer
- func (ts *TCPServer) SetDefaultSessionWriteDeadline(write time.Duration) *TCPServer
- func (ts *TCPServer) SetHeartbeat(heartbeat time.Duration) *TCPServer
- func (ts *TCPServer) SetLogger(debugLogger Logger, logger Logger) *TCPServer
- func (ts *TCPServer) SetMaxPacketBodyLength(maxLenBytes uint32) *TCPServer
- func (ts *TCPServer) Stop() error
- type TimeoutError
Constants ¶
const ( Preparing = "Preparing" Running = "Running" Stop = "Stop" )
Server status
const ( HeartbeatCmdPing byte = 0 HeartbeatCmdPong byte = 1 )
Heartbeat cmd
const ( DEBUG = "DEBUG" RELEASE = "RELEASE" )
Environment info
const ( PacketVersion byte = 0x2A // 101010 -> 42 // Packet ver: 42 -> 43 -> 44 -> ... PacketHeartbeatVersion byte = 0xFF // 11111111 -> 255 // Heartbeat ver: 255 -> 254- > 253 -> ... )
const Version = "0.0.1"
Variables ¶
var ( DefaultDebugLogger = log.New(os.Stderr, "[Gosocket-Debug]", log.LstdFlags) DefaultLogger = log.New(os.Stderr, "[Gosocket]", log.LstdFlags) )
var ErrTimeout error = &TimeoutError{}
ErrTimeout is returned for an expired deadline.
Functions ¶
Types ¶
type BroadcastServerMessageListener ¶
type BroadcastServerMessageListener struct{}
======== ======== Broadcast server message receive listener ======== ======== BroadcastServerMessageListener
type ClientCodec ¶
type ClientCodec interface { // Encode body to bytes Encode(ctx context.Context, message interface{}, cli *TCPClient) ([]byte, error) // Decode from bytes Decode(ctx context.Context, bytes []byte, cli *TCPClient) (interface{}, error) }
ClientCodec
type ClientMessageListener ¶
type ClientPacketHandler ¶
type ClientPacketHandler interface { PacketReceived(ctx context.Context, packet *Packet, cli *TCPClient) PacketSend(ctx context.Context, packet *Packet, cli *TCPClient) }
ClientPacketHandler
type Codec ¶
type Codec interface { // Encode body to bytes Encode(ctx context.Context, message interface{}, session *Session) ([]byte, error) // Decode from bytes Decode(ctx context.Context, bytes []byte, session *Session) (interface{}, error) }
Codec
type ConnectHandler ¶
type ConnectHandler interface {
OnConnect(ctx context.Context, conn *net.TCPConn, tcpSer *TCPServer)
}
ConnectHandler on connect accept processor
type DebugLogger ¶
type DebugLogger struct {
// contains filtered or unexported fields
}
func (*DebugLogger) Fatal ¶
func (d *DebugLogger) Fatal(v ...interface{})
func (*DebugLogger) Fatalf ¶
func (d *DebugLogger) Fatalf(format string, v ...interface{})
func (*DebugLogger) Fatalln ¶
func (d *DebugLogger) Fatalln(v ...interface{})
func (*DebugLogger) Panic ¶
func (d *DebugLogger) Panic(v ...interface{})
func (*DebugLogger) Panicf ¶
func (d *DebugLogger) Panicf(format string, v ...interface{})
func (*DebugLogger) Panicln ¶
func (d *DebugLogger) Panicln(v ...interface{})
func (*DebugLogger) Print ¶
func (d *DebugLogger) Print(v ...interface{})
func (*DebugLogger) Printf ¶
func (d *DebugLogger) Printf(format string, v ...interface{})
func (*DebugLogger) Println ¶
func (d *DebugLogger) Println(v ...interface{})
func (*DebugLogger) SetDebugMode ¶
func (d *DebugLogger) SetDebugMode(on bool)
type ExampleClientMessageListener ¶
type ExampleClientMessageListener struct{}
======== ======== Example client message receive listener ======== ========
type ExampleServerMessageListener ¶
type ExampleServerMessageListener struct{}
======== ======== Example server server message receive listener ======== ========
type ExampleSessionListener ¶
type ExampleSessionListener struct{}
======== ======== Example server session create/close listener ======== ========
func (ExampleSessionListener) OnSessionClose ¶
func (t ExampleSessionListener) OnSessionClose(s *Session)
func (ExampleSessionListener) OnSessionCreate ¶
func (t ExampleSessionListener) OnSessionCreate(s *Session)
type MessageListener ¶
type MessageListener interface {
OnMessage(ctx context.Context, message interface{}, session *Session)
}
MessageListener message processor interface Usage:
type Packet ¶
type Packet struct {
// contains filtered or unexported fields
}
func NewHeartbeatPacket ¶
Build heartbeat packet. cmd -> 0: ping; 1: pong - see const HeartbeatCmdPing, HeartbeatCmdPong
type PacketHandler ¶
type PacketHandler interface { PacketReceived(ctx context.Context, packet *Packet, session *Session) PacketSend(ctx context.Context, packet *Packet, session *Session) }
PacketHandler on packet receive processor
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
ClientSession
func NewSession ¶
func (*Session) CloseSession ¶
func (*Session) CreateTime ¶
CreateTime return the session create time
func (*Session) LastActive ¶
LastActive return the session last active time. (update on create, close, send packet, receive packet)
func (*Session) ReadDeadlin ¶
func (*Session) RemoteAddr ¶
RemoteAddr return string form of address (for example, "192.0.2.1:25", "[2001:db8::1]:80")
func (*Session) SendMessage ¶
func (s *Session) SendMessage(message interface{})
func (*Session) SetHeartbeat ¶
func (*Session) SetReadDeadline ¶
func (*Session) SetWriteDeadline ¶
func (*Session) UpdateLastActive ¶
func (s *Session) UpdateLastActive()
UpdateLastActive update the session last active time.
func (*Session) WriteDeadline ¶
WriteDeadLine return the session write deadline
type SessionListener ¶
type SessionWriter ¶
type SessionWriter interface {
Write()
}
type TCPClient ¶
type TCPClient struct {
// contains filtered or unexported fields
}
TCPClient the tcp server struct
func NewTcpClient ¶
NewTcpClient create a new tcp server Usage: * TODO: write usage
func (*TCPClient) RegisterMessageListener ¶
func (cli *TCPClient) RegisterMessageListener(listener ClientMessageListener) *TCPClient
func (*TCPClient) RemoteAddr ¶
func (*TCPClient) SendMessage ¶
func (*TCPClient) SetCodec ¶
func (cli *TCPClient) SetCodec(codec ClientCodec) *TCPClient
func (*TCPClient) SetDebugMode ¶
func (*TCPClient) SetMaxPacketBodyLength ¶
func (*TCPClient) SetSessionReadDeadline ¶
func (*TCPClient) SetSessionWriteDeadline ¶
func (*TCPClient) UpdateLastActive ¶
func (cli *TCPClient) UpdateLastActive()
type TCPServer ¶
type TCPServer struct {
// contains filtered or unexported fields
}
TCPServer the tcp server struct
func NewTCPServer ¶
NewTCPServer create a new tcp server Usage:
============================================ ! Detail see: server_test.go OR README.md! ============================================ // ==== ==== Server QuickStart ==== ==== // 1. Implement server OnMessageListener interface. see code above. // type MessageListener interface { // OnMessage(ctx context.Context, message interface{}, session *Session) // } // 2. New TCPServer, register MessageListener to the server, and startup it. // - And now, congratulations! a tcp server is ready. server, _ := NewTCPServer("[::1]:8888"). RegisterMessageListener(&TestExampleServerMessageListener{}). // Required Run() // 3. Stop the server when it is finished. go func() { <-time.NewTimer(10 * time.Second).C server.Stop() }() // ==== ==== Client QuickStart ==== ==== // 1. Implement ClientMessageListener interface. see code above. // type ClientMessageListener interface { // OnMessage(ctx context.Context, message interface{}, cli *TCPClient) // } // 2. New TCPClient, register ClientMessageListener to the client, and dial to server. // - And now, congratulations! a tcp client is ready. client, _ := NewTcpClient("[::1]:8888"). RegisterMessageListener(&TestExampleClientListener{}). Dial() // 3. Say "Hello!" to server. client.SendMessage("Hello!") // 4. Hangup the client when it is finished. go func() { <-time.NewTimer(6 * time.Second).C client.Hangup("It should be hangup now!") }() ============================================ ! Detail see: server_test.go OR README.md! ============================================
func (*TCPServer) RegisterMessageListener ¶
func (ts *TCPServer) RegisterMessageListener(listener MessageListener) *TCPServer
func (*TCPServer) RegisterSessionListener ¶
func (ts *TCPServer) RegisterSessionListener(listener SessionListener) *TCPServer
func (*TCPServer) SetDebugMode ¶
func (*TCPServer) SetDefaultSessionReadDeadline ¶
SetDefaultSessionReadDeadline session timeout if can not read any thing in this time
func (*TCPServer) SetDefaultSessionWriteDeadline ¶
func (*TCPServer) SetHeartbeat ¶
func (*TCPServer) SetMaxPacketBodyLength ¶
type TimeoutError ¶
type TimeoutError struct{}
TimeoutError is returned for an expired deadline.
func (*TimeoutError) Error ¶
func (e *TimeoutError) Error() string
Implement the net.Error interface.