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
41c6d4ac
Commit
41c6d4ac
authored
Mar 16, 2018
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add remark
parent
43624e3f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
5 deletions
+17
-5
codec.go
codec.go
+2
-0
conn.go
conn.go
+7
-3
session.go
session.go
+8
-2
No files found.
codec.go
View file @
41c6d4ac
...
...
@@ -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
{})
}
conn.go
View file @
41c6d4ac
...
...
@@ -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
}
...
...
session.go
View file @
41c6d4ac
...
...
@@ -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
...
...
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