-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsender_packet.go
More file actions
52 lines (46 loc) · 1.63 KB
/
sender_packet.go
File metadata and controls
52 lines (46 loc) · 1.63 KB
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
// -----------------------------------------------------------------------------
// github.com/balacode/udpt /[sender_packet.go]
// (c) balarabe@protonmail.com License: MIT
// -----------------------------------------------------------------------------
package udpt
import (
"bytes"
"io"
"time"
)
// senderPacket contains data, hash and timing details of
// a UDP packet (datagram) being sent by the Sender.
type senderPacket struct {
data []byte
sentHash []byte
sentTime time.Time
confirmedHash []byte
confirmedTime time.Time
} // senderPacket
// IsDelivered returns true if this packet has been successfully
// delivered (by receiving a successful confirmation packet).
func (pk *senderPacket) IsDelivered() bool {
ret := pk.confirmedHash != nil &&
bytes.Equal(pk.sentHash, pk.confirmedHash)
return ret
} // IsDelivered
// Send encrypts and sends this packet through connection 'conn'.
func (pk *senderPacket) Send(conn netUDPConn, cipher SymmetricCipher) error {
if conn == nil {
return makeError(0xE4B1BA, "nil connection")
}
if cipher == nil {
return makeError(0xE44F2A, "nil cipher")
}
ciphertext, err := cipher.Encrypt(pk.data)
if err != nil {
return makeError(0xEB39C3, err)
}
pk.sentTime = time.Now()
_, err = io.Copy(conn, bytes.NewReader(ciphertext))
if err != nil {
return makeError(0xE93D1F, err)
}
return nil
} // Send
// end