Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
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
张蕾
Geth-Modification
Commits
e28c60ca
Commit
e28c60ca
authored
Dec 12, 2014
by
Felix Lange
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
p2p: improve and test eofSignal
parent
9423401d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
5 deletions
+68
-5
peer.go
p2p/peer.go
+12
-5
peer_test.go
p2p/peer_test.go
+56
-0
No files found.
p2p/peer.go
View file @
e28c60ca
...
@@ -300,7 +300,7 @@ func (p *Peer) dispatch(msg Msg, protoDone chan struct{}) (wait bool, err error)
...
@@ -300,7 +300,7 @@ func (p *Peer) dispatch(msg Msg, protoDone chan struct{}) (wait bool, err error)
proto
.
in
<-
msg
proto
.
in
<-
msg
}
else
{
}
else
{
wait
=
true
wait
=
true
pr
:=
&
eofSignal
{
msg
.
Payload
,
protoDone
}
pr
:=
&
eofSignal
{
msg
.
Payload
,
int64
(
msg
.
Size
),
protoDone
}
msg
.
Payload
=
pr
msg
.
Payload
=
pr
proto
.
in
<-
msg
proto
.
in
<-
msg
}
}
...
@@ -438,18 +438,25 @@ func (rw *proto) ReadMsg() (Msg, error) {
...
@@ -438,18 +438,25 @@ func (rw *proto) ReadMsg() (Msg, error) {
return
msg
,
nil
return
msg
,
nil
}
}
// eofSignal wraps a reader with eof signaling.
// eofSignal wraps a reader with eof signaling. the eof channel is
// the eof channel is closed when the wrapped reader
// closed when the wrapped reader returns an error or when count bytes
// reaches EOF.
// have been read.
//
type
eofSignal
struct
{
type
eofSignal
struct
{
wrapped
io
.
Reader
wrapped
io
.
Reader
count
int64
eof
chan
<-
struct
{}
eof
chan
<-
struct
{}
}
}
// note: when using eofSignal to detect whether a message payload
// has been read, Read might not be called for zero sized messages.
func
(
r
*
eofSignal
)
Read
(
buf
[]
byte
)
(
int
,
error
)
{
func
(
r
*
eofSignal
)
Read
(
buf
[]
byte
)
(
int
,
error
)
{
n
,
err
:=
r
.
wrapped
.
Read
(
buf
)
n
,
err
:=
r
.
wrapped
.
Read
(
buf
)
if
err
!=
nil
{
r
.
count
-=
int64
(
n
)
if
(
err
!=
nil
||
r
.
count
<=
0
)
&&
r
.
eof
!=
nil
{
r
.
eof
<-
struct
{}{}
// tell Peer that msg has been consumed
r
.
eof
<-
struct
{}{}
// tell Peer that msg has been consumed
r
.
eof
=
nil
}
}
return
n
,
err
return
n
,
err
}
}
p2p/peer_test.go
View file @
e28c60ca
...
@@ -4,6 +4,7 @@ import (
...
@@ -4,6 +4,7 @@ import (
"bufio"
"bufio"
"bytes"
"bytes"
"encoding/hex"
"encoding/hex"
"io"
"io/ioutil"
"io/ioutil"
"net"
"net"
"reflect"
"reflect"
...
@@ -237,3 +238,58 @@ func TestNewPeer(t *testing.T) {
...
@@ -237,3 +238,58 @@ func TestNewPeer(t *testing.T) {
// Should not hang.
// Should not hang.
p
.
Disconnect
(
DiscAlreadyConnected
)
p
.
Disconnect
(
DiscAlreadyConnected
)
}
}
func
TestEOFSignal
(
t
*
testing
.
T
)
{
rb
:=
make
([]
byte
,
10
)
// empty reader
eof
:=
make
(
chan
struct
{},
1
)
sig
:=
&
eofSignal
{
new
(
bytes
.
Buffer
),
0
,
eof
}
if
n
,
err
:=
sig
.
Read
(
rb
);
n
!=
0
||
err
!=
io
.
EOF
{
t
.
Errorf
(
"Read returned unexpected values: (%v, %v)"
,
n
,
err
)
}
select
{
case
<-
eof
:
default
:
t
.
Error
(
"EOF chan not signaled"
)
}
// count before error
eof
=
make
(
chan
struct
{},
1
)
sig
=
&
eofSignal
{
bytes
.
NewBufferString
(
"aaaaaaaa"
),
4
,
eof
}
if
n
,
err
:=
sig
.
Read
(
rb
);
n
!=
8
||
err
!=
nil
{
t
.
Errorf
(
"Read returned unexpected values: (%v, %v)"
,
n
,
err
)
}
select
{
case
<-
eof
:
default
:
t
.
Error
(
"EOF chan not signaled"
)
}
// error before count
eof
=
make
(
chan
struct
{},
1
)
sig
=
&
eofSignal
{
bytes
.
NewBufferString
(
"aaaa"
),
999
,
eof
}
if
n
,
err
:=
sig
.
Read
(
rb
);
n
!=
4
||
err
!=
nil
{
t
.
Errorf
(
"Read returned unexpected values: (%v, %v)"
,
n
,
err
)
}
if
n
,
err
:=
sig
.
Read
(
rb
);
n
!=
0
||
err
!=
io
.
EOF
{
t
.
Errorf
(
"Read returned unexpected values: (%v, %v)"
,
n
,
err
)
}
select
{
case
<-
eof
:
default
:
t
.
Error
(
"EOF chan not signaled"
)
}
// no signal if neither occurs
eof
=
make
(
chan
struct
{},
1
)
sig
=
&
eofSignal
{
bytes
.
NewBufferString
(
"aaaaaaaaaaaaaaaaaaaaa"
),
999
,
eof
}
if
n
,
err
:=
sig
.
Read
(
rb
);
n
!=
10
||
err
!=
nil
{
t
.
Errorf
(
"Read returned unexpected values: (%v, %v)"
,
n
,
err
)
}
select
{
case
<-
eof
:
t
.
Error
(
"unexpected EOF signal"
)
default
:
}
}
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