package transport import ( "encoding/binary" "net" "ripple/crypto" "ripple/types" "ripple/udpr" ) type CounterpartTransport struct { d *Data c *udpr.UDPRConn } func NewCounterpartTransport(c *udpr.UDPRConn, d *Data) *CounterpartTransport { return &CounterpartTransport{d: d, c: c} } func (t *CounterpartTransport) Receive(tx []byte, addr *net.UDPAddr) (types.Instruction, error) { if len(tx) < 1+32+64+4+1+32 { return types.Instruction{}, errInvalidTransaction } if t.d.Memory.Counterpart.Identifier != parseTxFrom(tx) { return types.Instruction{}, errInvalidTransaction } if !crypto.VerifySignature(tx, t.d.Memory.Counterpart.SecretKey) { return types.Instruction{}, errInvalidTransaction } counter := binary.BigEndian.Uint32(tx[97:101]) if counter <= t.d.Memory.Counterpart.CounterIn { return types.Instruction{}, errInvalidTransaction } t.d.Memory.Counterpart.CounterIn = counter cmd := tx[101] args := tx[102 : len(tx)-32] return types.Instruction{Command: cmd, Arguments: args}, nil } func (t *CounterpartTransport) Send(instr types.Instruction) { t.d.Memory.Counterpart.CounterOut++ counter := t.d.Memory.Counterpart.CounterOut id := t.d.Memory.Counterpart.Identifier raw := buildTx( counterpartType, id.Username, t.d.Storage.User.UserIdentifier, counter, instr, ) tx := crypto.SignData(raw, t.d.Memory.Counterpart.SecretKey) addr, err := getAddr(id.ServerAddress, t.d.Memory.Counterpart.Port) if err != nil { return } go t.c.WriteToUDPR(tx, addr) }