-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver-call.rkt
167 lines (140 loc) · 5.1 KB
/
server-call.rkt
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
#lang racket/base
(require
"timestamp.rkt"
"return-box.rkt"
"status.rkt"
"malloc-util.rkt"
"ffi/byte-buffer.rkt"
(submod "ffi/server.rkt" unsafe)
"ffi/slice.rkt"
"ffi/metadata-array.rkt"
"ffi/call.rkt"
ffi/unsafe
racket/promise
racket/async-channel
racket/port
racket/match)
(provide
request-server-call
server-call-wait
server-call-method
server-call-recv-message-evt
server-call-send)
(struct send-data (initial-metadata message status trailing-metadata rb))
(define-cstruct _server-context
([call _pointer]
[payload _pointer]
[details _grpc-call-details]
[cancelled _int]))
(define (request-server-call server cq)
(define ctx (malloc-struct _server-context))
(define call-pointer (error 'pointer "Not implemented") #;(server-context-call-pointer ctx))
(set-server-context-call! ctx #f)
(define payload (error 'pointer "Not implemented") #;(server-context-payload-pointer ctx))
(set-server-context-payload! ctx #f)
(define details (server-context-details ctx))
(set-grpc-call-details-method! details #f)
(set-grpc-call-details-method-capacity! details 0)
(set-grpc-call-details-host! details #f)
(set-grpc-call-details-host-capacity! details 0)
(define deadline (grpc-call-details-deadline details))
(define metadata (error 'broken)) ;(make-immobile-grpc-metadata-array))
(define sema (make-semaphore))
(grpc-server-request-call
server
call-pointer
details
metadata
cq
cq
(malloc-immobile-cell sema))
(sync sema)
(define method (cast (grpc-call-details-method details) _pointer _string))
(define call (ptr-ref call-pointer _pointer))
(free ctx)
(create-server-call (timestamp 0 0) call method cq))
(define (create-server-call deadline grpc-call method cq)
(define cancelled-sema (make-semaphore))
(define recv-message-channel (make-async-channel))
(define send-message-channel (make-async-channel))
(define read-thread
(thread
(lambda ()
(call-with-malloc _pointer
(λ (payload-pointer)
(let loop ()
(sync
(error 'broken)
#;
(grpc-call-start-batch
grpc-call
(grpc-op-batch #:recv-message payload-pointer)))
(define payload (ptr-ref payload-pointer _pointer))
(when payload
(async-channel-put
recv-message-channel
(port->bytes (error 'broken)));(grpc-byte-buffer->input-port payload)))
(loop))))))))
(define write-thread
(thread
(lambda ()
(let loop ([state 'before-metadata])
(match (sync send-message-channel)
[(send-data initial-metadata message status trailing-metadata rb)
(unless (or (not initial-metadata) (hash-empty? initial-metadata))
(error 'nyi))
(unless (or (not status) (equal? status ok-status))
(error 'nyi))
(unless (or (not trailing-metadata) (hash-empty? trailing-metadata))
(error 'nyi))
(define send-initial-metadata
(or initial-metadata
(and (eq? state 'before-metadata) (hash))))
(define send-message-buffer
(and message (error 'broken))) ;(-grpc-byte-buffer message)))
(set-return-box! rb
(delay/strict
(error 'broken)
#;
(grpc-call-start-batch
grpc-call
(grpc-op-batch
#:cond send-initial-metadata #:send-initial-metadata 0 #f
#:cond send-message-buffer #:send-message send-message-buffer
#:cond status #:send-status-from-server 0 #f 0 #f))))
(unless status
(loop 'after-metadata))])))))
(server-call (hash)
deadline
method
grpc-call
(semaphore-peek-evt cancelled-sema)
(guard-evt (lambda () recv-message-channel))
send-message-channel
(thread-dead-evt read-thread)
(thread-dead-evt write-thread)))
(struct server-call (client-metadata
deadline
method
grpc-call
cancelled-evt
recv-message-evt
send-channel
read-thread-finished-evt
write-thread-finished-evt))
(define (server-call-is-cancelled? call)
(sync/timeout 0 (server-call-cancelled-evt call)))
(define (server-call-send call
#:initial-metadata [initial-metadata #f]
#:message [message #f]
#:status [status #f]
#:trailing-metadata [trailing-metadata #f])
(define rb (make-return-box))
(async-channel-put
(server-call-send-channel call)
(send-data initial-metadata message status trailing-metadata rb))
(sync rb))
(define (server-call-wait call)
(sync (server-call-read-thread-finished-evt call))
(sync (server-call-write-thread-finished-evt call))
(grpc-call-unref (server-call-grpc-call call)))