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