Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
getty
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
getty
Commits
9c017bc9
Commit
9c017bc9
authored
Jan 29, 2021
by
cvictory
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support return length
parent
3eb8b38b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
17 additions
and
13 deletions
+17
-13
client_test.go
client_test.go
+7
-3
hello.go
demo/hello/hello.go
+1
-1
getty.go
getty.go
+1
-1
session.go
session.go
+8
-8
No files found.
client_test.go
View file @
9c017bc9
...
...
@@ -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
())
...
...
demo/hello/hello.go
View file @
9c017bc9
...
...
@@ -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
()
...
...
getty.go
View file @
9c017bc9
...
...
@@ -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
()
...
...
session.go
View file @
9c017bc9
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment