forked from devsisters/goquic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.go
97 lines (83 loc) · 3.18 KB
/
dispatcher.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package goquic
// #include <stddef.h>
// #include "src/adaptor.h"
import "C"
import (
"net"
"unsafe"
)
type QuicDispatcher struct {
quicDispatcher unsafe.Pointer
quicServerSessions map[*QuicServerSession]bool
TaskRunner *TaskRunner
createQuicServerSession func() IncomingDataStreamCreator
}
type QuicServerSession struct {
quicServerSession unsafe.Pointer
quicServerStreams map[*QuicServerStream]bool
streamCreator IncomingDataStreamCreator // == session
remoteAddr *net.UDPAddr
}
type QuicEncryptedPacket struct {
encryptedPacket unsafe.Pointer
}
func CreateQuicDispatcher(writer *ServerWriter, createQuicServerSession func() IncomingDataStreamCreator, taskRunner *TaskRunner, cryptoConfig *ServerCryptoConfig) *QuicDispatcher {
dispatcher := &QuicDispatcher{
quicServerSessions: make(map[*QuicServerSession]bool),
TaskRunner: taskRunner,
createQuicServerSession: createQuicServerSession,
}
dispatcher.quicDispatcher = C.create_quic_dispatcher(
C.GoPtr(serverWriterPtr.Set(writer)), C.GoPtr(quicDispatcherPtr.Set(dispatcher)), C.GoPtr(taskRunnerPtr.Set(taskRunner)), cryptoConfig.serverCryptoConfig)
return dispatcher
}
func (d *QuicDispatcher) ProcessPacket(self_address *net.UDPAddr, peer_address *net.UDPAddr, buffer []byte) {
self_address_p := CreateIPEndPoint(self_address)
peer_address_p := CreateIPEndPoint(peer_address)
C.quic_dispatcher_process_packet(
d.quicDispatcher,
(*C.uint8_t)(unsafe.Pointer(&self_address_p.packed[0])),
C.size_t(len(self_address_p.packed)),
C.uint16_t(self_address_p.port),
(*C.uint8_t)(unsafe.Pointer(&peer_address_p.packed[0])),
C.size_t(len(peer_address_p.packed)),
C.uint16_t(peer_address_p.port),
(*C.char)(unsafe.Pointer(&buffer[0])), C.size_t(len(buffer)),
)
}
func (d *QuicDispatcher) Statistics() DispatcherStatistics {
stat := DispatcherStatistics{make([]SessionStatistics, 0)}
for session, _ := range d.quicServerSessions {
stat.SessionStatistics = append(stat.SessionStatistics, SessionStatistics{C.quic_server_session_connection_stat(session.quicServerSession)})
}
return stat
}
//export CreateGoSession
func CreateGoSession(dispatcher_key int64, session_c unsafe.Pointer) int64 {
dispatcher := quicDispatcherPtr.Get(dispatcher_key)
userSession := dispatcher.createQuicServerSession()
session := &QuicServerSession{
quicServerSession: session_c,
quicServerStreams: make(map[*QuicServerStream]bool),
streamCreator: userSession,
// TODO(serialx): Set remoteAddr here
}
// This is to prevent garbage collection. This is cleaned up on DeleteGoSession()
dispatcher.quicServerSessions[session] = true
return quicServerSessionPtr.Set(session)
}
//export DeleteGoSession
func DeleteGoSession(dispatcher_key int64, go_session_key int64) {
dispatcher := quicDispatcherPtr.Get(dispatcher_key)
go_session := quicServerSessionPtr.Get(go_session_key)
delete(dispatcher.quicServerSessions, go_session)
quicServerSessionPtr.Del(go_session_key)
}
//export ReleaseQuicDispatcher
func ReleaseQuicDispatcher(task_runner_key int64) {
quicDispatcherPtr.Del(task_runner_key)
}
//export ReleaseTaskRunner
func ReleaseTaskRunner(task_runner_key int64) {
taskRunnerPtr.Del(task_runner_key)
}