-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
78 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,31 @@ | ||
package p2p | ||
|
||
import "io" | ||
import ( | ||
"bytes" | ||
"encoding/gob" | ||
"io" | ||
) | ||
|
||
type Decoder interface { | ||
Decode(io.Reader, any) error | ||
Decode(io.Reader, *RPC) error | ||
} | ||
|
||
type GOBDecoder struct{} | ||
|
||
func (dec GOBDecoder) Decode(r io.Reader, rpc *RPC) error { | ||
return gob.NewDecoder(r).Decode(rpc) | ||
} | ||
|
||
type NOPDecoder struct{} | ||
|
||
func (dec NOPDecoder) Decode(r io.Reader, rpc *RPC) error { | ||
buf := new(bytes.Buffer) | ||
n, err := buf.ReadFrom(r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rpc.Payload = buf.Bytes()[:n] | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package p2p | ||
|
||
import "net" | ||
|
||
// Message represents any arbitrary data | ||
// that is being sent over the transport | ||
// between two nodes in the network. | ||
type RPC struct { | ||
From net.Addr | ||
Payload []byte | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters