Skip to content

Commit

Permalink
update interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
liujianping committed Dec 17, 2019
1 parent ea98056 commit cc6a138
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
8 changes: 5 additions & 3 deletions example/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (
)

func main() {
srv := tcpserver.NewServer(
srv := tcpserver.New(
tcpserver.Network("tcp"),
tcpserver.Address("127.0.0.1:8080"),
tcpserver.Handler(EchoHandler),
tcpserver.TCPHandler(EchoHandler),
)
if err := routine.Main(routine.ExecutorFunc(srv.Serve)); err != nil {
if err := routine.Main(
context.TODO(),
routine.ExecutorFunc(srv.Serve)); err != nil {
log.Println("tcpserver failed:", err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/x-mod/tcpserver

go 1.12
go 1.13

require github.com/x-mod/routine v1.1.0
require github.com/x-mod/routine v1.2.7
24 changes: 6 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudflare/cfssl v0.0.0-20190328212615-ea569c5aa1be/go.mod h1:yMWuSON2oQp+43nFtAV/uvKQIFpSPerB57DCt9t8sSA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75 h1:f0n1xnMSmBLzVfsMMvriDyA75NB/oBgILX2GcHXIQzY=
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75/go.mod h1:g2644b03hfBX9Ov0ZBDgXXens4rxSxmqFBbhvKv2yVA=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rakyll/hey v0.1.1/go.mod h1:sX26N/FW0nkvNZFzqnBp13eDIZNs5gJXSljSHj0vOXA=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/x-mod/cronexpr v1.0.0/go.mod h1:p2v5cgbUf0RY9jS/KOriOiKIGxEdHRqPOkFxOFvmLco=
github.com/x-mod/errors v0.1.0 h1:uuN9qGgq9vEHrlMqdsSC7kyE88q8Uv/jfOwRfRPLLQM=
github.com/x-mod/errors v0.1.0/go.mod h1:d+HrEt85NDHN0I4yJ9pQjy1JZEhPsSNou3sIFu5vXOk=
github.com/x-mod/routine v1.1.0 h1:peyTprIvl3xScEtr2NcW+cNcLEm8FMovUpYMNoUyvBE=
github.com/x-mod/routine v1.1.0/go.mod h1:ep1ekcf9g36JOjz82/PmDYcKzYiNKFlkv5P1A4CZz2g=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/x-mod/errors v0.1.6 h1:k0XsZCl/aQKPGo293ewOuYnRuxy9YVxDmUIC/b4C47s=
github.com/x-mod/errors v0.1.6/go.mod h1:sWzfaUOjYKcW4cQaF/VEUQibdxGjkD+8LnL+GJhkIs0=
github.com/x-mod/routine v1.2.7 h1:P55Bfv9YYOQDwisHyb/bjmcPw8ep+kYqSQ8WE5E/YQ8=
github.com/x-mod/routine v1.2.7/go.mod h1:FUJxsh8BYaKeT88jyhr0ubhumCMusZla3VlrrL46dcs=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
Expand Down
35 changes: 23 additions & 12 deletions tcpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"sync"
)

//ConnectionHandler connection handler definition
type ConnectionHandler func(ctx context.Context, conn net.Conn) error
//Handler connection handler definition
type Handler func(ctx context.Context, conn net.Conn) error

//Server represent tcpserver
type Server struct {
network string
address string
handler ConnectionHandler
handler Handler
listener net.Listener
wgroup sync.WaitGroup
}
Expand All @@ -37,8 +37,17 @@ func Address(addr string) ServerOpt {
}
}

//Handler option for connection
func Handler(h ConnectionHandler) ServerOpt {
//Listener option for listener
func Listener(ln net.Listener) ServerOpt {
return func(srv *Server) {
if ln != nil {
srv.listener = ln
}
}
}

//TCPHandler option for Connection Handler
func TCPHandler(h Handler) ServerOpt {
return func(srv *Server) {
if h != nil {
srv.handler = h
Expand All @@ -50,7 +59,7 @@ func Handler(h ConnectionHandler) ServerOpt {
type ServerOpt func(*Server)

//NewServer create a new tcpserver
func NewServer(opts ...ServerOpt) *Server {
func New(opts ...ServerOpt) *Server {
serv := &Server{}
for _, opt := range opts {
opt(serv)
Expand All @@ -60,12 +69,14 @@ func NewServer(opts ...ServerOpt) *Server {

//Serve tcpserver serving
func (srv *Server) Serve(ctx context.Context) error {
ln, err := net.Listen(srv.network, srv.address)
if err != nil {
return err
if srv.listener == nil {
ln, err := net.Listen(srv.network, srv.address)
if err != nil {
return err
}
log.Println("tcpserver serving at ", srv.network, srv.address)
srv.listener = ln
}
log.Println("tcpserver serving at ", srv.network, srv.address)
srv.listener = ln
for {
select {
case <-ctx.Done():
Expand All @@ -77,7 +88,7 @@ func (srv *Server) Serve(ctx context.Context) error {
log.Printf("warning: accept temp err: %v", ne)
continue
}
log.Println(err)
log.Println("failed: ", err)
return err
}

Expand Down

0 comments on commit cc6a138

Please sign in to comment.