-
Notifications
You must be signed in to change notification settings - Fork 10
/
sender.go
54 lines (46 loc) · 1.07 KB
/
sender.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
// Copyright 2018 The go-fractal Authors
// This file is part of the go-fractal library.
// Package tx_collector contains the implementation of tx collector for packer.
package tx_collector
import (
"bytes"
"net"
"github.com/fractal-platform/fractal/core/types"
"github.com/fractal-platform/fractal/rlp"
"github.com/fractal-platform/fractal/utils/log"
)
type TxSender struct {
srvAddr string
conn net.Conn
}
func NewTxSender(srvAddr string) *TxSender {
return &TxSender{
srvAddr: srvAddr,
}
}
func (s *TxSender) Connect() error {
conn, err := net.Dial("tcp", s.srvAddr)
if err != nil {
log.Error("")
return err
}
s.conn = conn
return nil
}
func (s *TxSender) SendTx(tx *types.Transaction) error {
buf, err := rlp.EncodeToBytes(types.Transactions{tx})
if err != nil {
log.Error("TxSender Encode TX error", "err", err)
return err
}
msg := &Msg{
Code: TxMsg,
Size: uint64(len(buf)),
Payload: bytes.NewReader(buf),
}
err = WriteMsg(s.conn, msg)
if err != nil {
log.Error("TxSender WriteMsg error", "srv", s.srvAddr, "err", err)
}
return err
}