2018-04-03 08:55:48 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2018-04-04 04:01:26 +00:00
|
|
|
"time"
|
2018-04-03 08:55:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ServerHandler interface {
|
|
|
|
GetName() string
|
2018-04-04 04:01:26 +00:00
|
|
|
GetConcurrency() int
|
|
|
|
GetHandshakeTimeout() time.Duration
|
|
|
|
GetMaxMessageSize() int64
|
|
|
|
GetReadBufferSize() int
|
|
|
|
GetWriteBufferSize() int
|
|
|
|
GetReadTimeout() time.Duration
|
|
|
|
GetWriteTimeout() time.Duration
|
|
|
|
GetPongTimeout() time.Duration
|
|
|
|
GetPingTimeout() time.Duration
|
|
|
|
GetPingPeriod() time.Duration
|
2018-04-03 08:55:48 +00:00
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
IsEnableCompression() bool
|
2018-04-03 08:55:48 +00:00
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
ServerCtx() ServerCtx
|
2018-04-03 08:55:48 +00:00
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
Init(serverCtx ServerCtx) error
|
|
|
|
OnStart(serverCtx ServerCtx) error
|
|
|
|
OnStop(serverCtx ServerCtx)
|
|
|
|
Destroy(serverCtx ServerCtx)
|
2018-04-03 08:55:48 +00:00
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
Listener(serverCtx ServerCtx) (net.Listener, error)
|
|
|
|
|
|
|
|
Validate() error
|
2018-04-03 08:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ServerHandlers struct {
|
|
|
|
ServerHandler
|
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
// Server name for sending in response headers.
|
|
|
|
//
|
|
|
|
// Default server name is used if left blank.
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// The maximum number of concurrent connections the server may serve.
|
|
|
|
//
|
|
|
|
// DefaultConcurrency is used if not set.
|
|
|
|
Concurrency int
|
|
|
|
|
|
|
|
HandshakeTimeout time.Duration
|
|
|
|
|
|
|
|
MaxMessageSize int64
|
|
|
|
// Per-connection buffer size for requests' reading.
|
|
|
|
// This also limits the maximum header size.
|
|
|
|
//
|
|
|
|
// Increase this buffer if your clients send multi-KB RequestURIs
|
|
|
|
// and/or multi-KB headers (for example, BIG cookies).
|
|
|
|
//
|
|
|
|
// Default buffer size is used if not set.
|
|
|
|
ReadBufferSize int
|
|
|
|
// Per-connection buffer size for responses' writing.
|
|
|
|
//
|
|
|
|
// Default buffer size is used if not set.
|
|
|
|
WriteBufferSize int
|
|
|
|
// Maximum duration for reading the full request (including body).
|
|
|
|
//
|
|
|
|
// This also limits the maximum duration for idle keep-alive
|
|
|
|
// connections.
|
|
|
|
//
|
|
|
|
// By default request read timeout is unlimited.
|
|
|
|
ReadTimeout time.Duration
|
|
|
|
|
|
|
|
// Maximum duration for writing the full response (including body).
|
|
|
|
//
|
|
|
|
// By default response write timeout is unlimited.
|
|
|
|
WriteTimeout time.Duration
|
|
|
|
|
|
|
|
PongTimeout time.Duration
|
|
|
|
PingTimeout time.Duration
|
|
|
|
PingPeriod time.Duration
|
|
|
|
|
|
|
|
EnableCompression bool
|
2018-04-03 08:55:48 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
func (sh *ServerHandlers) ServerCtx() ServerCtx {
|
2018-04-03 08:55:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
func (sh *ServerHandlers) Init(serverCtx ServerCtx) error {
|
2018-04-03 08:55:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
func (sh *ServerHandlers) OnStart(serverCtx ServerCtx) error {
|
2018-04-03 08:55:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
func (sh *ServerHandlers) OnStop(serverCtx ServerCtx) {
|
|
|
|
|
2018-04-03 08:55:48 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
func (sh *ServerHandlers) Destroy(serverCtx ServerCtx) {
|
2018-04-03 08:55:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
func (sh *ServerHandlers) Listener(serverCtx ServerCtx) (net.Listener, error) {
|
|
|
|
return nil, fmt.Errorf("Server: Method[ServerHandler.Listener] is not implemented")
|
|
|
|
}
|
2018-04-03 08:55:48 +00:00
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
func (sh *ServerHandlers) GetName() string {
|
|
|
|
return sh.Name
|
2018-04-03 08:55:48 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
func (sh *ServerHandlers) GetConcurrency() int {
|
|
|
|
return sh.Concurrency
|
2018-04-03 08:55:48 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
func (sh *ServerHandlers) GetHandshakeTimeout() time.Duration {
|
|
|
|
return sh.HandshakeTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) GetMaxMessageSize() int64 {
|
|
|
|
return sh.MaxMessageSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) GetReadBufferSize() int {
|
|
|
|
return sh.ReadBufferSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) GetWriteBufferSize() int {
|
|
|
|
return sh.WriteBufferSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) GetReadTimeout() time.Duration {
|
|
|
|
return sh.ReadTimeout
|
|
|
|
}
|
|
|
|
func (sh *ServerHandlers) GetWriteTimeout() time.Duration {
|
|
|
|
return sh.WriteTimeout
|
|
|
|
}
|
|
|
|
func (sh *ServerHandlers) GetPongTimeout() time.Duration {
|
|
|
|
return sh.PongTimeout
|
|
|
|
}
|
|
|
|
func (sh *ServerHandlers) GetPingTimeout() time.Duration {
|
|
|
|
return sh.PingTimeout
|
|
|
|
}
|
|
|
|
func (sh *ServerHandlers) GetPingPeriod() time.Duration {
|
|
|
|
return sh.PingPeriod
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) IsEnableCompression() bool {
|
|
|
|
return sh.EnableCompression
|
2018-04-03 08:55:48 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
func (sh *ServerHandlers) Validate() error {
|
2018-04-03 08:55:48 +00:00
|
|
|
if "" == sh.Name {
|
|
|
|
sh.Name = "Server"
|
|
|
|
}
|
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
if sh.Concurrency <= 0 {
|
|
|
|
sh.Concurrency = DefaultConcurrency
|
|
|
|
}
|
|
|
|
|
|
|
|
if sh.HandshakeTimeout <= 0 {
|
|
|
|
sh.HandshakeTimeout = DefaultHandshakeTimeout
|
|
|
|
}
|
|
|
|
if sh.MaxMessageSize <= 0 {
|
|
|
|
sh.MaxMessageSize = DefaultMaxMessageSize
|
|
|
|
}
|
|
|
|
if sh.ReadBufferSize <= 0 {
|
|
|
|
sh.ReadBufferSize = DefaultReadBufferSize
|
|
|
|
}
|
|
|
|
if sh.WriteBufferSize <= 0 {
|
|
|
|
sh.WriteBufferSize = DefaultWriteBufferSize
|
2018-04-03 08:55:48 +00:00
|
|
|
}
|
2018-04-04 04:01:26 +00:00
|
|
|
if sh.ReadTimeout <= 0 {
|
|
|
|
sh.ReadTimeout = DefaultReadTimeout
|
|
|
|
}
|
|
|
|
if sh.WriteTimeout <= 0 {
|
|
|
|
sh.WriteTimeout = DefaultWriteTimeout
|
|
|
|
}
|
|
|
|
if sh.PongTimeout <= 0 {
|
|
|
|
sh.PongTimeout = DefaultPongTimeout
|
|
|
|
}
|
|
|
|
if sh.PingTimeout <= 0 {
|
|
|
|
sh.PingTimeout = DefaultPingTimeout
|
|
|
|
}
|
|
|
|
if sh.PingPeriod <= 0 {
|
|
|
|
sh.PingPeriod = DefaultPingPeriod
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-04-03 08:55:48 +00:00
|
|
|
}
|