Skip to content

Commit

Permalink
把Handler放到构造Server时传递
Browse files Browse the repository at this point in the history
  • Loading branch information
bg5sbk committed Nov 3, 2016
1 parent 2a02e40 commit da9c4d4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
4 changes: 2 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ type ClearSendChan interface {
ClearSendChan(<-chan interface{})
}

func Listen(network, address string, protocol Protocol, sendChanSize int) (*Server, error) {
func Listen(network, address string, protocol Protocol, sendChanSize int, handler Handler) (*Server, error) {
listener, err := net.Listen(network, address)
if err != nil {
return nil, err
}
return NewServer(listener, protocol, sendChanSize), nil
return NewServer(listener, protocol, sendChanSize, handler), nil
}

func Dial(network, address string, protocol Protocol, sendChanSize int) (*Session, error) {
Expand Down
12 changes: 7 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Server struct {
manager *Manager
listener net.Listener
protocol Protocol
handler Handler
sendChanSize int
}

Expand All @@ -21,11 +22,12 @@ func (f HandlerFunc) HandleSession(session *Session) {
f(session)
}

func NewServer(l net.Listener, p Protocol, sendChanSize int) *Server {
func NewServer(listener net.Listener, protocol Protocol, sendChanSize int, handler Handler) *Server {
return &Server{
manager: NewManager(),
listener: l,
protocol: p,
listener: listener,
protocol: protocol,
handler: handler,
sendChanSize: sendChanSize,
}
}
Expand All @@ -34,7 +36,7 @@ func (server *Server) Listener() net.Listener {
return server.listener
}

func (server *Server) Serve(handler Handler) error {
func (server *Server) Serve() error {
for {
conn, err := Accept(server.listener)
if err != nil {
Expand All @@ -48,7 +50,7 @@ func (server *Server) Serve(handler Handler) error {
return
}
session := server.manager.NewSession(codec, server.sendChanSize)
handler.HandleSession(session)
server.handler.HandleSession(session)
}()
}
}
Expand Down
20 changes: 10 additions & 10 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ func RandBytes(n int) []byte {
}

func SessionTest(t *testing.T, sendChanSize int, test func(*testing.T, *Session)) {
server, err := Listen("tcp", "0.0.0.0:0", ProtocolFunc(NewTestCodec), sendChanSize)
utest.IsNilNow(t, err)
addr := server.Listener().Addr().String()

go server.Serve(HandlerFunc(func(session *Session) {
server, err := Listen("tcp", "0.0.0.0:0", ProtocolFunc(NewTestCodec), sendChanSize, HandlerFunc(func(session *Session) {
defer session.Close()
for {
msg, err := session.Receive()
Expand All @@ -91,6 +87,10 @@ func SessionTest(t *testing.T, sendChanSize int, test func(*testing.T, *Session)
}
}
}))
utest.IsNilNow(t, err)
go server.Serve()

addr := server.Listener().Addr().String()

clientWait := new(sync.WaitGroup)
for i := 0; i < 60; i++ {
Expand Down Expand Up @@ -175,11 +175,7 @@ func Test_Channel(t *testing.T) {
channel.Close()
}()

server, err := Listen("tcp", "0.0.0.0:0", ProtocolFunc(NewTestCodec), 2000)
utest.IsNilNow(t, err)
addr := server.Listener().Addr().String()

go server.Serve(HandlerFunc(func(session *Session) {
server, err := Listen("tcp", "0.0.0.0:0", ProtocolFunc(NewTestCodec), 2000, HandlerFunc(func(session *Session) {
defer session.Close()
channel.Put(session.ID(), session)

Expand All @@ -193,6 +189,10 @@ func Test_Channel(t *testing.T) {

<-waitTestDone
}))
utest.IsNilNow(t, err)
go server.Serve()

addr := server.Listener().Addr().String()

waitTestFinish := new(sync.WaitGroup)
for i := 0; i < 60; i++ {
Expand Down

0 comments on commit da9c4d4

Please sign in to comment.