-
Notifications
You must be signed in to change notification settings - Fork 100
/
stream.go
175 lines (143 loc) · 5.73 KB
/
stream.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package goquic
// #include <stddef.h>
// #include "src/adaptor.h"
import "C"
import (
"bytes"
"fmt"
"net/http"
"strings"
"unsafe"
)
// (~= QuicSpdy(Server|Client)Stream)
type DataStreamProcessor interface {
OnInitialHeadersComplete(header http.Header, peerAddress string)
OnTrailingHeadersComplete(header http.Header)
OnDataAvailable(data []byte, isClosed bool)
OnClose()
}
// (~= QuicServerSession)
type IncomingDataStreamCreator interface {
CreateIncomingDynamicStream(quicServerStream *QuicServerStream, streamId uint32) DataStreamProcessor
}
// (~= QuicClientSession)
type OutgoingDataStreamCreator interface {
CreateOutgoingDynamicStream() DataStreamProcessor
}
type QuicStream interface {
UserStream() DataStreamProcessor
WriteHeader(header http.Header, is_body_empty bool)
WriteOrBufferData(body []byte, fin bool)
WriteTrailers(header http.Header)
}
/*
(Incoming/Outgoing)DataStreamCreator (a.k.a Session)
|
| creates domain-specific stream (i.e. spdy, ...)
v
QuicStream -- owns --> DataStreamProcessor
*/
func createHeader(headers_c *C.struct_GoSpdyHeader) http.Header {
h := make(http.Header)
N := int(headers_c.N)
keysArray := (*[1 << 30](*C.char))(unsafe.Pointer(headers_c.Keys))[:N:N]
valuesArray := (*[1 << 30](*C.char))(unsafe.Pointer(headers_c.Values))[:N:N]
keysLen := (*[1 << 30]C.int)(unsafe.Pointer(headers_c.Keys_len))[:N:N]
valuesLen := (*[1 << 30]C.int)(unsafe.Pointer(headers_c.Values_len))[:N:N]
for i := 0; i < N; i++ {
key := C.GoStringN(keysArray[i], keysLen[i])
value := C.GoStringN(valuesArray[i], valuesLen[i])
if v, ok := h[key]; !ok {
h[key] = []string{value}
} else {
h[key] = append(v, value)
}
}
return h
}
func digSpdyHeader(header http.Header) ([]byte, []C.int, []byte, []C.int) {
var keys, values bytes.Buffer
var keylen, valuelen []C.int
keylen = make([]C.int, 0, len(header))
valuelen = make([]C.int, 0, len(header))
for key, mvalue := range header {
for index := range mvalue {
// Due to spdy_utils.cc, all trailer headers key should be lower-case (why?)
nk, errk := keys.WriteString(strings.ToLower(key))
nv, errv := values.WriteString(mvalue[index])
keylen = append(keylen, C.int(nk))
valuelen = append(valuelen, C.int(nv))
if errk != nil || errv != nil {
fmt.Println("buffer write failed", errk, errv)
break
}
}
}
return keys.Bytes(), keylen, values.Bytes(), valuelen
}
//export CreateIncomingDynamicStream
func CreateIncomingDynamicStream(session_key int64, stream_id uint32, wrapper_c unsafe.Pointer) int64 {
session := quicServerSessionPtr.Get(session_key)
stream := &QuicServerStream{
session: session,
wrapper: wrapper_c,
}
userStream := session.streamCreator.CreateIncomingDynamicStream(stream, stream_id)
stream.userStream = userStream
// This is to prevent garbage collection. This is cleaned up on QuicServerStream.OnClose()
session.quicServerStreams[stream] = true
return quicServerStreamPtr.Set(stream)
}
//export GoQuicSimpleServerStreamOnInitialHeadersComplete
func GoQuicSimpleServerStreamOnInitialHeadersComplete(quic_server_stream_key int64, headers_c *C.struct_GoSpdyHeader, peer_addr unsafe.Pointer, peer_addr_len uint32) {
stream := quicServerStreamPtr.Get(quic_server_stream_key)
header := createHeader(headers_c)
peerAddr := C.GoStringN((*C.char)(peer_addr), (C.int)(peer_addr_len))
stream.UserStream().OnInitialHeadersComplete(header, peerAddr)
}
//export GoQuicSimpleServerStreamOnDataAvailable
func GoQuicSimpleServerStreamOnDataAvailable(quic_server_stream_key int64, data unsafe.Pointer, data_len uint32, is_closed C.int) {
stream := quicServerStreamPtr.Get(quic_server_stream_key)
buf := C.GoBytes(data, C.int(data_len))
stream.UserStream().OnDataAvailable(buf, (is_closed > 0))
}
//export GoQuicSimpleServerStreamOnClose
func GoQuicSimpleServerStreamOnClose(quic_server_stream_key int64) {
stream := quicServerStreamPtr.Get(quic_server_stream_key)
stream.UserStream().OnClose()
}
//export GoQuicSpdyClientStreamOnInitialHeadersComplete
func GoQuicSpdyClientStreamOnInitialHeadersComplete(quic_client_stream_key int64, headers_c *C.struct_GoSpdyHeader) {
stream := quicClientStreamPtr.Get(quic_client_stream_key)
header := createHeader(headers_c)
stream.UserStream().OnInitialHeadersComplete(header, "")
}
//export GoQuicSpdyClientStreamOnTrailingHeadersComplete
func GoQuicSpdyClientStreamOnTrailingHeadersComplete(quic_client_stream_key int64, headers_c *C.struct_GoSpdyHeader) {
stream := quicClientStreamPtr.Get(quic_client_stream_key)
header := createHeader(headers_c)
stream.UserStream().OnTrailingHeadersComplete(header)
}
//export GoQuicSpdyClientStreamOnDataAvailable
func GoQuicSpdyClientStreamOnDataAvailable(quic_client_stream_key int64, data unsafe.Pointer, data_len uint32, is_closed C.int) {
stream := quicClientStreamPtr.Get(quic_client_stream_key)
buf := C.GoBytes(data, C.int(data_len))
stream.UserStream().OnDataAvailable(buf, (is_closed > 0))
}
//export GoQuicSpdyClientStreamOnClose
func GoQuicSpdyClientStreamOnClose(quic_client_stream_key int64) {
stream := quicClientStreamPtr.Get(quic_client_stream_key)
stream.UserStream().OnClose()
}
//export UnregisterQuicServerStreamFromSession
func UnregisterQuicServerStreamFromSession(quic_server_stream_key int64) {
stream := quicServerStreamPtr.Get(quic_server_stream_key)
delete(stream.session.quicServerStreams, stream)
quicServerStreamPtr.Del(quic_server_stream_key)
}
//export UnregisterQuicClientStreamFromSession
func UnregisterQuicClientStreamFromSession(quic_client_stream_key int64) {
stream := quicClientStreamPtr.Get(quic_client_stream_key)
delete(stream.session.quicClientStreams, stream)
quicClientStreamPtr.Del(quic_client_stream_key)
}