@ -2,7 +2,6 @@ package p2p
import (
import (
"bufio"
"bufio"
"bytes"
"encoding/binary"
"encoding/binary"
"io"
"io"
"time"
"time"
@ -28,46 +27,39 @@ content (n bytes) - actual message content
const BatchSizeInByte = 1 << 16
const BatchSizeInByte = 1 << 16
// ReadMessageContent reads the message type and content size, and return the actual content.
// ReadMessageContent reads the message type and content size, and return the actual content.
func ReadMessageContent ( s Stream ) ( [ ] byte , error ) {
func ReadMessageContent ( s Stream ) ( content [ ] byte , err error ) {
var (
var (
contentBuf = bytes . NewBuffer ( [ ] byte { } )
r = bufio . NewReader ( s )
r = bufio . NewReader ( s )
)
)
timeoutDuration := 1 * time . Second
timeoutDuration := 1 * time . Second
s . SetReadDeadline ( time . Now ( ) . Add ( timeoutDuration ) )
if err = s . SetReadDeadline ( time . Now ( ) . Add ( timeoutDuration ) ) ; err != nil {
log . Error ( "cannot reset deadline for message header read" , "error" , err )
return
}
//// Read 1 byte for message type
//// Read 1 byte for message type
_ , err := r . ReadByte ( )
if _ , err = r . ReadByte ( ) ; err != nil {
switch err {
log . Error ( "failed to read p2p message type field" , "error" , err )
case io . EOF :
return
log . Error ( "Error reading the p2p message type field" , "io.EOF" , err )
return contentBuf . Bytes ( ) , err
case nil :
//log.Printf("Received p2p message type: %x\n", msgType)
default :
log . Error ( "Error reading the p2p message type field" , "msg" , err )
return contentBuf . Bytes ( ) , err
}
}
// TODO: check on msgType and take actions accordingly
// TODO: check on msgType and take actions accordingly
//// Read 4 bytes for message size
//// Read 4 bytes for message size
fourBytes := make ( [ ] byte , 4 )
fourBytes := make ( [ ] byte , 4 )
n , err := r . Read ( fourBytes )
if _ , err = io . ReadFull ( r , fourBytes ) ; err != nil {
if err != nil {
log . Error ( "failed to read p2p message size field" , "error" , err )
log . Error ( "Error reading the p2p message size field" )
return
return contentBuf . Bytes ( ) , err
} else if n < len ( fourBytes ) {
log . Error ( "Failed reading the p2p message size field: only read" , "Num of bytes" , n )
return contentBuf . Bytes ( ) , err
}
}
contentLength := int ( binary . BigEndian . Uint32 ( fourBytes ) )
contentLength := int ( binary . BigEndian . Uint32 ( fourBytes ) )
tmp Buf := make ( [ ] byte , contentLength )
contentBuf := make ( [ ] byte , contentLength )
timeoutDuration = 20 * time . Second
timeoutDuration = 20 * time . Second
s . SetReadDeadline ( time . Now ( ) . Add ( timeoutDuration ) )
if err = s . SetReadDeadline ( time . Now ( ) . Add ( timeoutDuration ) ) ; err != nil {
m , err := io . ReadFull ( r , tmpBuf )
log . Error ( "cannot reset deadline for message content read" , "error" , err )
if err != nil || m < contentLength {
return
log . Error ( "Read %v bytes, we need %v bytes" , m , contentLength )
}
return [ ] byte { } , err
if _ , err = io . ReadFull ( r , contentBuf ) ; err != nil {
log . Error ( "failed to read p2p message contents" , "error" , err )
return
}
}
contentBuf . Write ( tmpBuf )
content = contentBuf
return contentBuf . Bytes ( ) , nil
return
}
}