Commit 41c6d4ac authored by AlexStocks's avatar AlexStocks

add remark

parent 43624e3f
......@@ -51,5 +51,7 @@ type EventListener interface {
// invoked when receive packge. Pls attention that do not handle long time logic processing in this func.
// You'd better set the package's maximum length. If the message's length is greater than it, u should
// should return err in Reader{Read} and getty will close this connection soon.
//
// If this is a udp event listener, the second parameter type is UDPContext.
OnMessage(Session, interface{})
}
......@@ -342,7 +342,7 @@ func (t *gettyTCPConn) close(waitSec int) {
/////////////////////////////////////////
type UDPContext struct {
Pkg []byte
Pkg interface{}
PeerAddr *net.UDPAddr
}
......@@ -450,12 +450,16 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) {
length int
ok bool
ctx UDPContext
buf []byte
peerAddr *net.UDPAddr
)
if ctx, ok = udpCtx.(UDPContext); !ok {
return 0, fmt.Errorf("illegal @udpCtx{%#v} type", udpCtx)
}
if buf, ok = ctx.Pkg.([]byte); !ok {
return 0, fmt.Errorf("illegal @udpCtx.Pkg{%#v} type", udpCtx)
}
if u.wTimeout > 0 {
// Optimization: update write deadline only if more than 25%
......@@ -470,12 +474,12 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) {
}
}
atomic.AddUint32(&u.writeCount, (uint32)(len(ctx.Pkg)))
atomic.AddUint32(&u.writeCount, (uint32)(len(buf)))
peerAddr = ctx.PeerAddr
if u.peerAddr != nil {
peerAddr = u.peerAddr
}
length, _, err = u.conn.WriteMsgUDP(ctx.Pkg, nil, peerAddr)
length, _, err = u.conn.WriteMsgUDP(buf, nil, peerAddr)
return length, err
}
......
......@@ -77,6 +77,8 @@ type Session interface {
SetAttribute(interface{}, interface{})
RemoveAttribute(interface{})
// the Writer will invoke this function.
// for udp session, the first parameter should be UDPContext. Otherwise its type is []byte.
WritePkg(interface{}, time.Duration) error
WriteBytes([]byte) error
WriteBytesArray(...[]byte) error
......@@ -347,7 +349,8 @@ func (s *session) sessionToken() string {
return fmt.Sprintf("{%s:%d:%s<->%s}", s.name, s.ID(), s.LocalAddr(), s.RemoteAddr())
}
// Queued Write, for handler
// Queued Write, for handler.
// For udp session, the @pkg should be UDPContext.
func (s *session) WritePkg(pkg interface{}, timeout time.Duration) error {
if s.IsClosed() {
return ErrSessionClosed
......@@ -674,6 +677,8 @@ func (s *session) handleUDPPackage() error {
bufLen int
buf []byte
addr *net.UDPAddr
pkgLen int
pkg interface{}
)
buf = make([]byte, s.maxMsgLen)
......@@ -697,6 +702,7 @@ func (s *session) handleUDPPackage() error {
continue
}
pkg, pkgLen, err = s.reader.Read(s, buf[:bufLen])
if err == nil && s.maxMsgLen > 0 && bufLen > int(s.maxMsgLen) {
err = ErrMsgTooLong
}
......@@ -705,7 +711,7 @@ func (s *session) handleUDPPackage() error {
continue
}
s.UpdateActive()
s.rQ <- UDPContext{Pkg: buf[:bufLen], PeerAddr: addr}
s.rQ <- UDPContext{Pkg: pkg, PeerAddr: addr}
}
return err
......
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