Skip to content

Commit 48b696d

Browse files
committed
backup temp
1 parent dcd0dab commit 48b696d

22 files changed

+1021
-1257
lines changed

channel.go

Lines changed: 135 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,154 @@
11
package enet
22

33
const channel_wnd_size = 256
4-
const enet_channel_id_none uint8 = 0xff
54

5+
type enet_channel_item struct {
6+
header enet_packet_header
7+
fragment enet_packet_fragment // if header.cmd == enet_packet_fragment
8+
payload []uint8 // not include packet-header
9+
retries int // sent times for outgoing packet, acked times for incoming packet
10+
acked int // acked times
11+
retrans *enet_timer_item // retrans timer
12+
}
13+
14+
// outgoing: ->end ..untransfered.. next ..transfered.. begin ->
15+
// incoming: <-begin ..acked.. next ..unacked.. end<-
616
type enet_channel struct {
7-
_out_sn uint16
8-
_out_usn uint16
9-
expected_sn uint16
10-
expected_usn uint16
11-
wnd [channel_wnd_size]uint16
12-
wnd_used int
13-
last_acked uint16
14-
last_intrans uint16
17+
next_sn uint32 // next reliable packet number for sent
18+
next_usn uint32 // next unsequenced packet number for sent
19+
outgoing [channel_wnd_size]*enet_channel_item
20+
outgoing_used uint32 // in trans packets not acked
21+
outgoing_begin uint32 // the first one is not acked yet
22+
outgoing_end uint32 // the last one is not acked yet
23+
outgoing_next uint32 // the next one would be transfered
24+
incoming [channel_wnd_size]*enet_channel_item
25+
incoming_used uint32 // rcvd packet count in incoming window
26+
incoming_begin uint32 // the first one has be received
27+
incoming_end uint32 // the last one has been received
28+
// incoming_next uint32 // the next one will be acked
1529
}
1630

17-
func (ch enet_channel) wnd_is_full() bool {
18-
return ch.wnd_used >= channel_wnd_size
31+
func (ch *enet_channel) outgoing_trans(item *enet_channel_item) {
32+
idx := item.header.sn % channel_wnd_size
33+
v := ch.outgoing[idx]
34+
assert(v == nil && item.header.sn == ch.outgoing_end)
35+
ch.outgoing[idx] = item
36+
if ch.outgoing_end <= item.header.sn {
37+
ch.outgoing_end = item.header.sn + 1
38+
}
39+
item.retries++
40+
ch.outgoing_used++
1941
}
2042

21-
func (ch *enet_channel) wnd_put_in(sn uint16) {
43+
func (ch *enet_channel) outgoing_ack(sn uint32) {
44+
if sn < ch.outgoing_begin || sn >= ch.outgoing_end { // already acked or error
45+
return
46+
}
2247
idx := sn % channel_wnd_size
23-
v := ch.wnd[idx]
24-
if v == 0 {
25-
ch.wnd_used++
26-
ch.last_intrans = sn
48+
v := ch.outgoing[idx]
49+
assert(v != nil && v.header.sn == sn)
50+
51+
v.acked++
52+
}
53+
54+
func (ch *enet_channel) outgoing_do_trans() (item *enet_channel_item) {
55+
assert(ch.outgoing_next <= ch.outgoing_end)
56+
if ch.outgoing_next >= ch.outgoing_end {
57+
return
2758
}
28-
ch.wnd[idx]++
59+
idx := ch.outgoing_next % channel_wnd_size
60+
item = ch.outgoing[idx]
61+
assert(item != nil)
62+
ch.outgoing_next++
63+
return
2964
}
3065

31-
func (ch *enet_channel) wnd_pop_out(sn uint16) {
32-
idx := sn % channel_wnd_size
33-
v := ch.wnd[idx]
34-
if v <= 0 {
66+
// may be retransed packet
67+
func (ch *enet_channel) incoming_trans(item *enet_channel_item) {
68+
if item.header.sn < ch.incoming_begin {
69+
return
70+
}
71+
idx := item.header.sn % channel_wnd_size
72+
v := ch.incoming[idx]
73+
// duplicated packet
74+
if v != nil {
3575
return
3676
}
37-
ch.wnd_used--
38-
ch.wnd[idx] = 0
39-
ch.last_acked = sn
77+
assert(v == nil || v.header.sn == item.header.sn)
78+
79+
ch.incoming[idx] = item
80+
ch.incoming_used++
81+
if ch.incoming_end <= item.header.sn {
82+
ch.incoming_end = item.header.sn + 1
83+
}
4084
}
41-
func (ch *enet_channel) out_sn() uint16 {
42-
v := ch._out_sn
43-
ch._out_sn++
44-
return v
85+
86+
// when do ack incoming packets
87+
func (ch *enet_channel) incoming_ack(sn uint32) {
88+
if sn < ch.incoming_begin || sn >= ch.incoming_end { // reack packet not in wnd
89+
return
90+
}
91+
idx := sn % channel_wnd_size
92+
v := ch.incoming[idx]
93+
assert(v != nil && v.header.sn == sn)
94+
v.acked++
95+
}
96+
97+
// called after incoming-ack
98+
func (ch *enet_channel) incoming_slide() (item *enet_channel_item) { // return value may be ignored
99+
if ch.incoming_begin >= ch.incoming_end {
100+
return
101+
}
102+
idx := ch.incoming_begin % channel_wnd_size
103+
v := ch.incoming[idx]
104+
if v == nil || v.acked <= 0 { // not received yet
105+
return
106+
}
107+
assert(v.header.sn == ch.incoming_begin)
108+
109+
if v.header.cmd == enet_packet_type_fragment {
110+
all := true
111+
for i := uint32(1); i < v.fragment.count; i++ {
112+
n := ch.incoming[idx+i]
113+
if n == nil || n.header.sn != v.header.sn+i || n.fragment.sn != v.header.sn {
114+
all = false
115+
break
116+
}
117+
}
118+
if !all {
119+
return
120+
}
121+
122+
item = v
123+
ch.incoming_begin += v.fragment.count
124+
ch.incoming_used -= v.fragment.count
125+
for i := uint32(1); i < v.fragment.count; i++ {
126+
item.payload = append(item.payload, ch.incoming[idx+1].payload...)
127+
ch.incoming[idx+i] = nil
128+
}
129+
ch.incoming[idx] = nil
130+
131+
return
132+
}
133+
item = v
134+
ch.incoming_begin++
135+
ch.incoming_used--
136+
ch.incoming[idx] = nil
137+
return
45138
}
46139

47-
func (ch *enet_channel) out_usn() uint16 {
48-
v := ch._out_usn
49-
ch._out_usn++
50-
return v
140+
func (ch enet_channel) is_outgoing_full() bool {
141+
return ch.outgoing_end-ch.outgoing_begin >= channel_wnd_size
142+
}
143+
func (ch enet_channel) is_incoming_full() bool {
144+
return ch.incoming_end-ch.incoming_begin >= channel_wnd_size
145+
}
146+
func (ch enet_channel) is_outgoing_null() bool {
147+
return ch.outgoing_end <= ch.outgoing_begin
148+
}
149+
func (ch enet_channel) is_incoming_null() bool {
150+
return ch.incoming_end <= ch.incoming_begin
151+
}
152+
func (ch enet_channel) outgoing_intrans() int {
153+
return int(ch.outgoing_next - ch.outgoing_begin)
51154
}

command.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

enet.go

Lines changed: 74 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,76 @@
11
package enet
22

3-
import "net"
4-
5-
const (
6-
enet_cmd_none uint8 = iota
7-
enet_cmd_ack
8-
enet_cmd_syn // with ack flag, connect
9-
enet_cmd_synack //
10-
enet_cmd_fin // ack when under connected or disconnect-later state, else use unseq
11-
enet_cmd_ping // ack
12-
enet_cmd_reliable // ack
13-
enet_cmd_unreliable
14-
enet_cmd_fragment // ack
15-
enet_cmd_unsequenced // +unseq flag
16-
enet_cmd_bandwidthlimit // ack flag
17-
enet_cmd_throttle // ack flag
18-
enet_cmd_unreliable_fragment
19-
enet_cmds_count
20-
)
21-
const (
22-
enet_cmd_mask uint8 = 0x0f
23-
enet_cmd_flag_ack = 1 << 7
24-
enet_cmd_flag_unseq = 1 << 6
25-
)
26-
27-
const (
28-
enet_peerid_flag_compressed uint16 = 1 << 14
29-
enet_peerid_flag_time = 1 << 15
30-
enet_peerid_session_mask = 3 << 12
31-
enet_peerid_session_shift = 12
32-
enet_peerid_mask = 0x0fff
33-
)
34-
35-
const (
36-
enet_wnd_size_min uint32 = 4096
37-
enet_wnd_size_max = 65536
38-
enet_wnd_size_default = 65536
39-
enet_mtu_min = 576
40-
enet_mtu_max = 4096
41-
enet_mtu_default = 1400
42-
enet_channel_count_min = 1
43-
enet_channel_count_max = 255
44-
enet_channel_count_default = 16
45-
)
46-
const (
47-
enet_wnd_count uint16 = 64
48-
enet_rtt_default int64 = 500
49-
enet_throttle_default uint32 = 32
50-
enet_throttle_scale = 32
51-
enet_throttle_acce_default = 2
52-
enet_throttle_dece_default = 2
53-
enet_throttle_interval_default = 5000
54-
)
55-
56-
const (
57-
enet_timeout_limit int64 = 32
58-
enet_timeout_min = 5000
59-
enet_timeout_max = 30000
60-
)
61-
62-
type enet_address *net.UDPAddr
3+
/*
4+
udp_header | protocol_header | crc32_header? | ( packet_header | payload )+
5+
*/
6+
type enet_protocol_header struct {
7+
peerid uint16 // target peerid, not used
8+
flags uint8 // 0xcc if crc or 0
9+
packet_n uint8
10+
time uint32 // milli-second, sent-time
11+
clientid uint32 // client-id?
12+
}
13+
type enet_crc32_header struct {
14+
crc32 uint32
15+
}
16+
type enet_packet_header struct {
17+
cmd uint8
18+
flags uint8 // _needack, _forcefin_
19+
chanid uint8
20+
rsv uint8
21+
size uint32 // including packet_header
22+
sn uint32
23+
}
24+
25+
//cmd_type_ack = 1
26+
// flags must be zero
27+
type enet_packet_ack struct {
28+
sn uint32 // rcvd-sn // not the next sn
29+
tm uint32 // rcvd sent time
30+
}
31+
32+
//cmd_type_syn = 2
33+
// flags = enet_packet_needack
34+
type enet_packet_syn struct { // ack by conack
35+
peerid uint16 // zero, whose id write the packet
36+
mtu uint16 // default = 1200
37+
wnd_size uint32 // local recv window size, 0x8000
38+
channel_n uint32 // channels count, default = 2
39+
rcv_bandwidth uint32 // local receiving bandwith bps, 0 means no limit
40+
snd_bandwidth uint32 // local sending bandwidth , 0 means no limit
41+
throttle_interval uint32 // = 0x1388 = 5000ms
42+
throttle_acce uint32 // = 2
43+
throttle_dece uint32 // = 2
44+
}
45+
46+
//cmd_type_synack = 3
47+
// flags = enet_packet_needack
48+
type enet_packet_synack enet_packet_syn
49+
50+
//cmd_type_fin = 4
51+
// flags = enet_packet_flags_forcefin_ if disconnect unconnected peer
52+
type enet_packet_fin struct{}
53+
54+
// cmd_type = 5
55+
type enet_packet_ping struct{}
56+
57+
//cmd_type_reliable = 6
58+
// flags= enet_packet_header_flags_needack
59+
type enet_packet_reliable struct{}
60+
61+
//cmd_type_unreliable = 7
62+
// flags = enet_packet_header_flags_none
63+
type enet_packet_unreliable struct {
64+
usn uint32 // unreliable sequence number
65+
}
66+
67+
//cmd_type_fragment = 8
68+
// [offset, length) of the packet sn
69+
// packet was splitted into fragment_count parts
70+
type enet_packet_fragment struct {
71+
sn uint32 // start sequence number
72+
count uint32 // fragment counts
73+
index uint32 // index of current fragment
74+
length uint32 // total length of all fragments
75+
offset uint32
76+
}

enetapi.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package enet
2+
3+
import (
4+
"os"
5+
)
6+
7+
type PeerEventHandler func(Peer, ret int)
8+
type DataEventHandler func(Peer, chanid uint8, payload []uint8)
9+
10+
type Host interface {
11+
SetConnectionHandler(PeerEventHandler)
12+
SetDisconnectionHandler(PeerEventHandler)
13+
SetDataHandler(DataEventHandler)
14+
Connect(dest string)
15+
Run(chan os.Signal)
16+
}
17+
18+
type Peer interface {
19+
Disconnect()
20+
Write(chanid uint8, dat []byte)
21+
}

enetclient/enetclient.exe

2.81 MB
Binary file not shown.

0 commit comments

Comments
 (0)