package socket

import (
	"blazing/common/socket/codec"

	"github.com/gogf/gf/v2/util/gconv"
	"github.com/panjf2000/gnet/pkg/pool/goroutine"
	"github.com/panjf2000/gnet/v2"
)

type Handler interface {
	Handle(gnet.Conn, []byte)
}
type Server struct {
	gnet.BuiltinEventEngine
	eng        gnet.Engine
	addr       string
	connected  int64
	network    string
	multicore  bool
	bufferSize int
	workerPool *goroutine.Pool
	codec      codec.SocketCodec
	handler    Handler
	discorse   bool
}

type Option func(*Server)

// NewServer returns a new Server

func NewServer(options ...Option) *Server {
	server := &Server{
		//	handler:    handler.NewTomeeHandler(),   //请求返回
		codec:      codec.NewTomeeSocketCodec(), //默认解码器 len+pack
		workerPool: goroutine.Default(),
		bufferSize: 4096, //默认缓冲区大小
		multicore:  true,
		discorse:   true,
	}
	for _, option := range options {
		option(server)
	}
	//...
	return server
}
func WithSocketCodec(codec codec.SocketCodec) Option {
	return func(u *Server) {
		u.codec = codec
	}
}
func WithCORS() Option {
	return func(u *Server) {
		u.discorse = false
	}
}
func WithSocketHandler(handler Handler) Option {
	return func(u *Server) {
		u.handler = handler
	}
}
func WithBufferSize(bufferSize int) Option {
	return func(u *Server) {
		u.bufferSize = bufferSize
	}
}
func WithPort(port int) Option {
	return func(s *Server) {

		s.network = "tcp"
		s.addr = ":" + gconv.String(port)
	}
}
