Commit 10a4efd0 authored by alexstocks's avatar alexstocks

add (server)Listen

parent d63a5e25
......@@ -9,7 +9,11 @@
## develop history ##
---
- 2016/09/04
> 1 add server.go:(Server)Listen
>
> 2 version: 0.3.02
- 2016/09/03
> 1 modify return value of session.go:(Session)Close from void to error
>
......@@ -17,9 +21,9 @@
>
> 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
>
......
......@@ -22,8 +22,7 @@ import (
type Server struct {
// net
host string
port int
addr string
listener net.Listener
sync.Once
......@@ -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 {
if port <= 0 {
return errors.New("port<=0 illegal")
}
this.host = host
this.port = port
listener, err := net.Listen(network, HostAddress(host, port))
return this.Listen(network, HostAddress(host, port))
}
func (this *Server) Listen(network string, addr string) error {
listener, err := net.Listen(network, addr)
if err != nil {
return err
}
this.addr = addr
this.listener = listener
return nil
......@@ -85,7 +88,7 @@ func (this *Server) RunEventloop(newSession SessionCallback) {
)
for {
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
}
if delay != 0 {
......@@ -104,7 +107,7 @@ func (this *Server) RunEventloop(newSession SessionCallback) {
}
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
}
delay = 0
......
......@@ -16,6 +16,7 @@ import (
"strconv"
)
// HostAddress composes a ip:port style address. Its opposite function is net.SplitHostPort.
func HostAddress(host string, port int) string {
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