Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-kad-dht
Commits
da02d4a7
Commit
da02d4a7
authored
Aug 05, 2018
by
Marten Seemann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
also use the buffered writer for handling new messages
parent
6d6ed078
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
27 deletions
+31
-27
dht_net.go
dht_net.go
+31
-27
No files found.
dht_net.go
View file @
da02d4a7
...
...
@@ -18,6 +18,31 @@ import (
var
dhtReadMessageTimeout
=
time
.
Minute
var
ErrReadTimeout
=
fmt
.
Errorf
(
"timed out reading response"
)
type
bufferedWriteCloser
interface
{
ggio
.
WriteCloser
Flush
()
error
}
// The Protobuf writer performs multiple small writes when writing a message.
// We need to buffer those writes, to make sure that we're not sending a new
// packet for every single write.
type
bufferedDelimitedWriter
struct
{
*
bufio
.
Writer
ggio
.
WriteCloser
}
func
newBufferedDelimitedWriter
(
str
io
.
Writer
)
bufferedWriteCloser
{
w
:=
bufio
.
NewWriter
(
str
)
return
&
bufferedDelimitedWriter
{
Writer
:
w
,
WriteCloser
:
ggio
.
NewDelimitedWriter
(
w
),
}
}
func
(
w
*
bufferedDelimitedWriter
)
Flush
()
error
{
return
w
.
Writer
.
Flush
()
}
// handleNewStream implements the inet.StreamHandler
func
(
dht
*
IpfsDHT
)
handleNewStream
(
s
inet
.
Stream
)
{
go
dht
.
handleNewMessage
(
s
)
...
...
@@ -28,7 +53,7 @@ func (dht *IpfsDHT) handleNewMessage(s inet.Stream) {
cr
:=
ctxio
.
NewReader
(
ctx
,
s
)
// ok to use. we defer close stream in this func
cw
:=
ctxio
.
NewWriter
(
ctx
,
s
)
// ok to use. we defer close stream in this func
r
:=
ggio
.
NewDelimitedReader
(
cr
,
inet
.
MessageSizeMax
)
w
:=
ggio
.
New
DelimitedWriter
(
cw
)
w
:=
newBuffered
DelimitedWriter
(
cw
)
mPeer
:=
s
.
Conn
()
.
RemotePeer
()
for
{
...
...
@@ -71,7 +96,11 @@ func (dht *IpfsDHT) handleNewMessage(s inet.Stream) {
}
// send out response msg
if
err
:=
w
.
WriteMsg
(
rpmes
);
err
!=
nil
{
err
=
w
.
WriteMsg
(
rpmes
)
if
err
==
nil
{
err
=
w
.
Flush
()
}
if
err
!=
nil
{
s
.
Reset
()
log
.
Debugf
(
"send response error: %s"
,
err
)
return
...
...
@@ -158,31 +187,6 @@ func (dht *IpfsDHT) messageSenderForPeer(p peer.ID) (*messageSender, error) {
return
ms
,
nil
}
type
bufferedWriteCloser
interface
{
ggio
.
WriteCloser
Flush
()
error
}
// The Protobuf writer performs multiple small writes when writing a message.
// We need to buffer those writes, to make sure that we're not sending a new
// packet for every single write.
type
bufferedDelimitedWriter
struct
{
*
bufio
.
Writer
ggio
.
WriteCloser
}
func
newBufferedDelimitedWriter
(
str
inet
.
Stream
)
bufferedWriteCloser
{
w
:=
bufio
.
NewWriter
(
str
)
return
&
bufferedDelimitedWriter
{
Writer
:
w
,
WriteCloser
:
ggio
.
NewDelimitedWriter
(
w
),
}
}
func
(
w
*
bufferedDelimitedWriter
)
Flush
()
error
{
return
w
.
Writer
.
Flush
()
}
type
messageSender
struct
{
s
inet
.
Stream
r
ggio
.
ReadCloser
...
...
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