2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
package gtcp
|
|
|
|
|
|
|
|
import (
|
2019-05-20 08:59:16 +08:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"net"
|
2019-12-13 17:40:29 +08:00
|
|
|
"sync"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/container/gmap"
|
|
|
|
"github.com/gogf/gf/os/glog"
|
|
|
|
"github.com/gogf/gf/util/gconv"
|
2017-11-23 10:21:28 +08:00
|
|
|
)
|
|
|
|
|
2018-07-20 11:41:23 +08:00
|
|
|
const (
|
2019-10-30 19:55:50 +08:00
|
|
|
// Default TCP server name.
|
2019-06-19 09:06:52 +08:00
|
|
|
gDEFAULT_SERVER = "default"
|
2018-07-20 11:41:23 +08:00
|
|
|
)
|
|
|
|
|
2019-05-20 08:59:16 +08:00
|
|
|
// TCP Server.
|
2018-07-20 11:41:23 +08:00
|
|
|
type Server struct {
|
2019-12-13 17:40:29 +08:00
|
|
|
mu sync.Mutex // Used for Server.listen concurrent safety.
|
|
|
|
listen net.Listener // Listener.
|
|
|
|
address string // Server listening address.
|
|
|
|
handler func(*Conn) // Connection handler.
|
|
|
|
tlsConfig *tls.Config // TLS configuration.
|
2018-07-20 11:41:23 +08:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:59:16 +08:00
|
|
|
// Map for name to server, for singleton purpose.
|
2019-07-23 23:20:27 +08:00
|
|
|
var serverMapping = gmap.NewStrAnyMap(true)
|
2018-07-20 11:41:23 +08:00
|
|
|
|
2019-05-20 08:59:16 +08:00
|
|
|
// GetServer returns the TCP server with specified <name>,
|
|
|
|
// or it returns a new normal TCP server named <name> if it does not exist.
|
|
|
|
// The parameter <name> is used to specify the TCP server
|
2019-06-19 09:06:52 +08:00
|
|
|
func GetServer(name ...interface{}) *Server {
|
|
|
|
serverName := gDEFAULT_SERVER
|
2019-07-28 17:37:13 +08:00
|
|
|
if len(name) > 0 && name[0] != "" {
|
2019-06-19 09:06:52 +08:00
|
|
|
serverName = gconv.String(name[0])
|
|
|
|
}
|
|
|
|
return serverMapping.GetOrSetFuncLock(serverName, func() interface{} {
|
|
|
|
return NewServer("", nil)
|
|
|
|
}).(*Server)
|
2018-07-20 11:41:23 +08:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:59:16 +08:00
|
|
|
// NewServer creates and returns a new normal TCP server.
|
2019-06-11 20:57:43 +08:00
|
|
|
// The parameter <name> is optional, which is used to specify the instance name of the server.
|
2019-06-19 09:06:52 +08:00
|
|
|
func NewServer(address string, handler func(*Conn), name ...string) *Server {
|
|
|
|
s := &Server{
|
|
|
|
address: address,
|
|
|
|
handler: handler,
|
|
|
|
}
|
2019-12-13 13:43:28 +08:00
|
|
|
if len(name) > 0 && name[0] != "" {
|
2019-06-19 09:06:52 +08:00
|
|
|
serverMapping.Set(name[0], s)
|
|
|
|
}
|
|
|
|
return s
|
2018-07-20 11:41:23 +08:00
|
|
|
}
|
|
|
|
|
2019-06-04 23:33:46 +08:00
|
|
|
// NewServerTLS creates and returns a new TCP server with TLS support.
|
2019-06-11 20:57:43 +08:00
|
|
|
// The parameter <name> is optional, which is used to specify the instance name of the server.
|
2019-06-19 09:06:52 +08:00
|
|
|
func NewServerTLS(address string, tlsConfig *tls.Config, handler func(*Conn), name ...string) *Server {
|
2019-05-20 08:59:16 +08:00
|
|
|
s := NewServer(address, handler, name...)
|
2019-06-04 23:33:46 +08:00
|
|
|
s.SetTLSConfig(tlsConfig)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServerKeyCrt creates and returns a new TCP server with TLS support.
|
2019-06-11 20:57:43 +08:00
|
|
|
// The parameter <name> is optional, which is used to specify the instance name of the server.
|
2019-06-19 09:06:52 +08:00
|
|
|
func NewServerKeyCrt(address, crtFile, keyFile string, handler func(*Conn), name ...string) *Server {
|
2019-06-04 23:33:46 +08:00
|
|
|
s := NewServer(address, handler, name...)
|
|
|
|
if err := s.SetTLSKeyCrt(crtFile, keyFile); err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
2019-05-20 08:59:16 +08:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-06-04 18:26:32 +08:00
|
|
|
// SetAddress sets the listening address for server.
|
2019-06-04 23:33:46 +08:00
|
|
|
func (s *Server) SetAddress(address string) {
|
2019-06-19 09:06:52 +08:00
|
|
|
s.address = address
|
2017-12-30 17:09:00 +08:00
|
|
|
}
|
|
|
|
|
2019-06-04 18:26:32 +08:00
|
|
|
// SetHandler sets the connection handler for server.
|
2019-06-19 09:06:52 +08:00
|
|
|
func (s *Server) SetHandler(handler func(*Conn)) {
|
|
|
|
s.handler = handler
|
2017-12-30 17:09:00 +08:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:59:16 +08:00
|
|
|
// SetTlsKeyCrt sets the certificate and key file for TLS configuration of server.
|
2019-06-04 23:33:46 +08:00
|
|
|
func (s *Server) SetTLSKeyCrt(crtFile, keyFile string) error {
|
|
|
|
tlsConfig, err := LoadKeyCrt(crtFile, keyFile)
|
2019-05-20 08:59:16 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-04 23:33:46 +08:00
|
|
|
s.tlsConfig = tlsConfig
|
2019-05-20 08:59:16 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTlsConfig sets the TLS configuration of server.
|
|
|
|
func (s *Server) SetTLSConfig(tlsConfig *tls.Config) {
|
|
|
|
s.tlsConfig = tlsConfig
|
|
|
|
}
|
|
|
|
|
2019-06-05 22:12:22 +08:00
|
|
|
// Close closes the listener and shutdowns the server.
|
|
|
|
func (s *Server) Close() error {
|
2019-12-13 17:40:29 +08:00
|
|
|
if s.listen == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2019-06-05 22:12:22 +08:00
|
|
|
return s.listen.Close()
|
|
|
|
}
|
|
|
|
|
2019-05-20 08:59:16 +08:00
|
|
|
// Run starts running the TCP Server.
|
|
|
|
func (s *Server) Run() (err error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
if s.handler == nil {
|
|
|
|
err = errors.New("start running failed: socket handler not defined")
|
2019-12-13 17:40:29 +08:00
|
|
|
glog.Error(err)
|
2019-06-19 09:06:52 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.tlsConfig != nil {
|
|
|
|
// TLS Server
|
2019-12-13 17:40:29 +08:00
|
|
|
s.mu.Lock()
|
2019-06-19 09:06:52 +08:00
|
|
|
s.listen, err = tls.Listen("tcp", s.address, s.tlsConfig)
|
2019-12-13 17:40:29 +08:00
|
|
|
s.mu.Unlock()
|
2019-06-19 09:06:52 +08:00
|
|
|
if err != nil {
|
2019-12-13 17:40:29 +08:00
|
|
|
glog.Error(err)
|
2019-06-19 09:06:52 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Normal Server
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp", s.address)
|
|
|
|
if err != nil {
|
2019-12-13 17:40:29 +08:00
|
|
|
glog.Error(err)
|
2019-06-19 09:06:52 +08:00
|
|
|
return err
|
|
|
|
}
|
2019-12-13 17:40:29 +08:00
|
|
|
s.mu.Lock()
|
2019-06-19 09:06:52 +08:00
|
|
|
s.listen, err = net.ListenTCP("tcp", addr)
|
2019-12-13 17:40:29 +08:00
|
|
|
s.mu.Unlock()
|
2019-06-19 09:06:52 +08:00
|
|
|
if err != nil {
|
2019-12-13 17:40:29 +08:00
|
|
|
glog.Error(err)
|
2019-06-19 09:06:52 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-12-11 14:50:25 +08:00
|
|
|
// Listening loop.
|
2019-06-19 09:06:52 +08:00
|
|
|
for {
|
|
|
|
if conn, err := s.listen.Accept(); err != nil {
|
|
|
|
return err
|
|
|
|
} else if conn != nil {
|
|
|
|
go s.handler(NewConnByNetConn(conn))
|
|
|
|
}
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|