Commit 10a4efd0 authored by alexstocks's avatar alexstocks

add (server)Listen

parent d63a5e25
...@@ -9,6 +9,10 @@ ...@@ -9,6 +9,10 @@
## develop history ## ## develop history ##
--- ---
- 2016/09/04
> 1 add server.go:(Server)Listen
>
> 2 version: 0.3.02
- 2016/09/03 - 2016/09/03
> 1 modify return value of session.go:(Session)Close from void to error > 1 modify return value of session.go:(Session)Close from void to error
...@@ -17,9 +21,9 @@ ...@@ -17,9 +21,9 @@
> >
> 3 session.go:Session{*gettyConn, readDeadline, writeDeadline} -> session.go:Session{gettyConn, rDeadline, wDeadline} > 3 session.go:Session{*gettyConn, readDeadline, writeDeadline} -> session.go:Session{gettyConn, rDeadline, wDeadline}
> >
> 4 session.go:(Session)Reset > 4 add session.go:(Session)Reset
> >
> 5 session.go:(Session)SetConn > 5 add session.go:(Session)SetConn
> >
> 6 add elastic sleep time machanism in client.go:(Client)RunEventLoop > 6 add elastic sleep time machanism in client.go:(Client)RunEventLoop
> >
......
...@@ -22,8 +22,7 @@ import ( ...@@ -22,8 +22,7 @@ import (
type Server struct { type Server struct {
// net // net
host string addr string
port int
listener net.Listener listener net.Listener
sync.Once sync.Once
...@@ -58,17 +57,21 @@ func (this *Server) IsClosed() bool { ...@@ -58,17 +57,21 @@ func (this *Server) IsClosed() bool {
} }
} }
// (Server)Bind's functionality is equal to (Server)Listen.
func (this *Server) Bind(network string, host string, port int) error { func (this *Server) Bind(network string, host string, port int) error {
if port <= 0 { if port <= 0 {
return errors.New("port<=0 illegal") return errors.New("port<=0 illegal")
} }
this.host = host return this.Listen(network, HostAddress(host, port))
this.port = port }
listener, err := net.Listen(network, HostAddress(host, port))
func (this *Server) Listen(network string, addr string) error {
listener, err := net.Listen(network, addr)
if err != nil { if err != nil {
return err return err
} }
this.addr = addr
this.listener = listener this.listener = listener
return nil return nil
...@@ -85,7 +88,7 @@ func (this *Server) RunEventloop(newSession SessionCallback) { ...@@ -85,7 +88,7 @@ func (this *Server) RunEventloop(newSession SessionCallback) {
) )
for { for {
if this.IsClosed() { if this.IsClosed() {
log.Warn("Server{%s:%d} stop acceptting client connect request.", this.host, this.port) log.Warn("Server{%s} stop acceptting client connect request.", this.addr)
return return
} }
if delay != 0 { if delay != 0 {
...@@ -104,7 +107,7 @@ func (this *Server) RunEventloop(newSession SessionCallback) { ...@@ -104,7 +107,7 @@ func (this *Server) RunEventloop(newSession SessionCallback) {
} }
continue continue
} }
log.Info("Server{%s:%d}.Accept() = err {%+v}", this.host, this.port, err) log.Info("Server{%s}.Accept() = err {%#v}", this.addr, err)
continue continue
} }
delay = 0 delay = 0
......
...@@ -16,6 +16,7 @@ import ( ...@@ -16,6 +16,7 @@ import (
"strconv" "strconv"
) )
// HostAddress composes a ip:port style address. Its opposite function is net.SplitHostPort.
func HostAddress(host string, port int) string { func HostAddress(host string, port int) string {
return net.JoinHostPort(host, strconv.Itoa(port)) return net.JoinHostPort(host, strconv.Itoa(port))
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment