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
4fbff185
Commit
4fbff185
authored
Jun 23, 2019
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add: tcp read case 2;
Fix: add buf slice length
parent
a14ac8a9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
22 deletions
+26
-22
getty.go
getty.go
+7
-5
session.go
session.go
+19
-17
No files found.
getty.go
View file @
4fbff185
...
...
@@ -29,14 +29,16 @@ type Reader interface {
// Parse tcp/udp/websocket pkg from buffer and if possible return a complete pkg.
// When receiving a tcp network streaming segment, there are 4 cases as following:
// case 1: a error found in the streaming segment;
// case 2: can not unmarshal a pkg from the streaming segment;
// case 3: just unmarshal a pkg from the streaming segment;
// case 4: unmarshal more than one pkg from the streaming segment;
// case 2: can not unmarshal a pkg header from the streaming segment;
// case 3: unmarshal a pkg header but can not unmarshal a pkg from the streaming segment;
// case 4: just unmarshal a pkg from the streaming segment;
// case 5: unmarshal more than one pkg from the streaming segment;
//
// The return value is (nil, 0, error) as case 1.
// The return value is (nil, 0, nil) as case 2.
// The return value is (pkg, pkgLen, nil) as case 3.
// The handleTcpPackage may invoke func Read many times as case 4.
// The return value is (nil, pkgLen, nil) as case 3.
// The return value is (pkg, pkgLen, nil) as case 4.
// The handleTcpPackage may invoke func Read many times as case 5.
Read
(
Session
,
[]
byte
)
(
interface
{},
int
,
error
)
}
...
...
session.go
View file @
4fbff185
...
...
@@ -641,7 +641,7 @@ func (s *session) handleTCPPackage() error {
for
{
// for clause for the network timeout condition check
// s.conn.SetReadTimeout(time.Now().Add(s.rTimeout))
bufLen
,
err
=
conn
.
read
(
buf
)
bufLen
,
err
=
conn
.
read
(
buf
[
:
maxReadBufLen
]
)
if
err
!=
nil
{
if
netError
,
ok
=
perrors
.
Cause
(
err
)
.
(
net
.
Error
);
ok
&&
netError
.
Timeout
()
{
break
...
...
@@ -664,6 +664,7 @@ func (s *session) handleTCPPackage() error {
}
// pkg, err = s.pkgHandler.Read(s, pktBuf)
pkg
,
pkgLen
,
err
=
s
.
reader
.
Read
(
s
,
pktBuf
.
Bytes
())
// for case 3/case 4
if
err
==
nil
&&
s
.
maxMsgLen
>
0
&&
pkgLen
>
int
(
s
.
maxMsgLen
)
{
err
=
perrors
.
Errorf
(
"pkgLen %d > session max message len %d"
,
pkgLen
,
s
.
maxMsgLen
)
}
...
...
@@ -674,15 +675,15 @@ func (s *session) handleTCPPackage() error {
exit
=
true
break
}
// handle case 2
// handle case 2
/case 3
if
pkg
==
nil
{
break
}
// handle case
3
// handle case
4
s
.
UpdateActive
()
s
.
addTask
(
pkg
)
pktBuf
.
Next
(
pkgLen
)
// continue to handle case
4
// continue to handle case
5
}
if
exit
{
break
...
...
@@ -695,29 +696,30 @@ func (s *session) handleTCPPackage() error {
// get package from udp packet
func
(
s
*
session
)
handleUDPPackage
()
error
{
var
(
ok
bool
err
error
netError
net
.
Error
conn
*
gettyUDPConn
bufLen
int
buf
[]
byte
addr
*
net
.
UDPAddr
pkgLen
int
pkg
interface
{}
ok
bool
err
error
netError
net
.
Error
conn
*
gettyUDPConn
bufLen
int
maxBufLen
int
buf
[]
byte
addr
*
net
.
UDPAddr
pkgLen
int
pkg
interface
{}
)
conn
=
s
.
Connection
.
(
*
gettyUDPConn
)
b
ufLen
=
int
(
s
.
maxMsgLen
+
maxReadBufLen
)
maxB
ufLen
=
int
(
s
.
maxMsgLen
+
maxReadBufLen
)
if
int
(
s
.
maxMsgLen
<<
1
)
<
bufLen
{
b
ufLen
=
int
(
s
.
maxMsgLen
<<
1
)
maxB
ufLen
=
int
(
s
.
maxMsgLen
<<
1
)
}
buf
=
make
([]
byte
,
b
ufLen
)
buf
=
make
([]
byte
,
maxB
ufLen
)
for
{
if
s
.
IsClosed
()
{
break
}
bufLen
,
addr
,
err
=
conn
.
read
(
buf
)
bufLen
,
addr
,
err
=
conn
.
read
(
buf
[
:
maxBufLen
]
)
log
.
Debugf
(
"conn.read() = bufLen:%d, addr:%#v, err:%+v"
,
bufLen
,
addr
,
err
)
if
netError
,
ok
=
perrors
.
Cause
(
err
)
.
(
net
.
Error
);
ok
&&
netError
.
Timeout
()
{
continue
...
...
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