connection context

This commit is contained in:
Josh Baker 2016-08-29 07:01:30 -07:00
parent eadbf61d4c
commit 349b9006f1
1 changed files with 12 additions and 0 deletions

View File

@ -37,6 +37,10 @@ type Conn interface {
WriteNull() WriteNull()
// SetReadBuffer updates the buffer read size for the connection // SetReadBuffer updates the buffer read size for the connection
SetReadBuffer(bytes int) SetReadBuffer(bytes int)
// Context returns a user-defined context
Context() interface{}
// SetContext sets a user-defined context
SetContext(v interface{})
} }
var ( var (
@ -149,6 +153,7 @@ func (s *Server) ListenServeAndSignal(signal chan error) error {
newWriter(tcpc), newWriter(tcpc),
newReader(tcpc), newReader(tcpc),
tcpc.RemoteAddr().String(), tcpc.RemoteAddr().String(),
nil,
} }
if accept != nil && !accept(c) { if accept != nil && !accept(c) {
c.Close() c.Close()
@ -221,6 +226,7 @@ type conn struct {
wr *writer wr *writer
rd *reader rd *reader
addr string addr string
ctx interface{}
} }
func (c *conn) Close() error { func (c *conn) Close() error {
@ -228,6 +234,12 @@ func (c *conn) Close() error {
c.conn.Close() // close the connection. ignore this error c.conn.Close() // close the connection. ignore this error
return err // return the writer error only return err // return the writer error only
} }
func (c *conn) Context() interface{} {
return c.ctx
}
func (c *conn) SetContext(v interface{}) {
c.ctx = v
}
func (c *conn) WriteString(str string) { func (c *conn) WriteString(str string) {
c.wr.WriteString(str) c.wr.WriteString(str)
} }