-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathio.go
59 lines (47 loc) · 1.16 KB
/
io.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// io.go
//
// Copyright (c) 2023-2024 Markku Rossi
//
// All rights reserved.
package ot
import (
"math/big"
)
// IO defines an I/O interface to communicate between peers.
type IO interface {
// SendByte sends a byte value.
SendByte(val byte) error
// SendUint32 sends an uint32 value.
SendUint32(val int) error
// SendData sends binary data.
SendData(val []byte) error
// Flush flushed any pending data in the connection.
Flush() error
// ReceiveByte receives a byte value.
ReceiveByte() (byte, error)
// ReceiveUint32 receives an uint32 value.
ReceiveUint32() (int, error)
// ReceiveData receives binary data.
ReceiveData() ([]byte, error)
}
// SendString sends a string value.
func SendString(io IO, str string) error {
return io.SendData([]byte(str))
}
// ReceiveString receives a string value.
func ReceiveString(io IO) (string, error) {
data, err := io.ReceiveData()
if err != nil {
return "", err
}
return string(data), nil
}
// ReceiveBigInt receives a bit.Int from the connection.
func ReceiveBigInt(io IO) (*big.Int, error) {
data, err := io.ReceiveData()
if err != nil {
return nil, err
}
return big.NewInt(0).SetBytes(data), nil
}