Commit 9c017bc9 authored by cvictory's avatar cvictory

support return length

parent 3eb8b38b
......@@ -172,6 +172,7 @@ func TestUDPClient(t *testing.T) {
var (
err error
conn *net.UDPConn
leng int
)
func() {
ip := net.ParseIP("127.0.0.1")
......@@ -205,10 +206,12 @@ func TestUDPClient(t *testing.T) {
assert.Equal(t, 1, msgHandler.SessionNumber())
ss := msgHandler.array[0]
err = ss.WritePkg(nil, 0)
leng, err = ss.WritePkg(nil, 0)
assert.NotNil(t, err)
err = ss.WritePkg([]byte("hello"), 0)
assert.True(t, leng == 0)
leng, err = ss.WritePkg([]byte("hello"), 0)
assert.NotNil(t, perrors.Cause(err))
assert.True(t, leng == 0)
l, err := ss.WriteBytes([]byte("hello"))
assert.Zero(t, l)
assert.NotNil(t, err)
......@@ -240,9 +243,10 @@ func TestUDPClient(t *testing.T) {
assert.Nil(t, err)
beforeWritePkgNum := atomic.LoadUint32(&udpConn.writePkgNum)
err = ss.WritePkg(udpCtx, 0)
leng, err = ss.WritePkg(udpCtx, 0)
assert.Equal(t, beforeWritePkgNum+1, atomic.LoadUint32(&udpConn.writePkgNum))
assert.Nil(t, err)
assert.True(t, leng == 0)
clt.Close()
assert.True(t, clt.IsClosed())
......
......@@ -31,7 +31,7 @@ func ClientRequest() {
go func() {
echoTimes := 10
for i := 0; i < echoTimes; i++ {
err := ss.WritePkg("hello", WritePkgTimeout)
_, err := ss.WritePkg("hello", WritePkgTimeout)
if err != nil {
log.Infof("session.WritePkg(session{%s}, error{%v}", ss.Stat(), err)
ss.Close()
......
......@@ -171,7 +171,7 @@ type Session interface {
// the Writer will invoke this function. Pls attention that if timeout is less than 0, WritePkg will send @pkg asap.
// for udp session, the first parameter should be UDPContext.
WritePkg(pkg interface{}, timeout time.Duration) error
WritePkg(pkg interface{}, timeout time.Duration) (int, error)
WriteBytes([]byte) (int, error)
WriteBytesArray(...[]byte) (int, error)
Close()
......
......@@ -347,12 +347,12 @@ func (s *session) sessionToken() string {
s.name, s.EndPoint().EndPointType(), s.ID(), s.LocalAddr(), s.RemoteAddr())
}
func (s *session) WritePkg(pkg interface{}, timeout time.Duration) error {
func (s *session) WritePkg(pkg interface{}, timeout time.Duration) (int, error) {
if pkg == nil {
return fmt.Errorf("@pkg is nil")
return 0, fmt.Errorf("@pkg is nil")
}
if s.IsClosed() {
return ErrSessionClosed
return 0, ErrSessionClosed
}
defer func() {
......@@ -367,7 +367,7 @@ func (s *session) WritePkg(pkg interface{}, timeout time.Duration) error {
pkgBytes, err := s.writer.Write(s, pkg)
if err != nil {
log.Warnf("%s, [session.WritePkg] session.writer.Write(@pkg:%#v) = error:%+v", s.Stat(), pkg, err)
return perrors.WithStack(err)
return 0, perrors.WithStack(err)
}
var udpCtxPtr *UDPContext
if udpCtx, ok := pkg.(UDPContext); ok {
......@@ -384,13 +384,13 @@ func (s *session) WritePkg(pkg interface{}, timeout time.Duration) error {
if 0 < timeout {
s.Connection.SetWriteTimeout(timeout)
}
_, err = s.Connection.send(pkg)
var len int
len, err = s.Connection.send(pkg)
if err != nil {
log.Warnf("%s, [session.WritePkg] @s.Connection.Write(pkg:%#v) = err:%+v", s.Stat(), pkg, err)
return perrors.WithStack(err)
return len, perrors.WithStack(err)
}
return nil
return len, nil
}
// for codecs
......
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