/
sendbuf.lisp
159 lines (141 loc) · 4.31 KB
/
sendbuf.lisp
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
(in-package #:tpd2.io)
(deftype sendbuf-small-integer ()
'(unsigned-byte 24))
(declaim (inline make-sendbuf))
(defstruct sendbuf
(head nil :type list)
(tail nil :type list)
(num-bufs 0 :type sendbuf-small-integer)
(len 0 :type sendbuf-small-integer))
(my-defun sendbuf add (x)
(my-declare-fast-inline)
(cond ((sendbuf-p x)
(my merge x))
(x
(let ((buf (force-simple-byte-vector x)))
(declare (type simple-byte-vector buf))
(unless (zerop (length buf))
(incf (my num-bufs))
(incf (my len) (the sendbuf-small-integer (length buf)))
(let ((n (cons buf nil)))
(cond ((my head)
(setf (cdr (my tail)) n)
(setf (my tail) n))
(t (setf (my head) n
(my tail) n))))))))
(values))
(my-defun sendbuf merge (other)
(my-declare-fast-inline)
(declare (dynamic-extent other))
(cond
((my head)
(setf (cdr (my tail)) (sendbuf-head other))
(when (sendbuf-tail other)
(setf (my tail) (sendbuf-tail other))))
(t (setf (my head) (sendbuf-head other)
(my tail) (sendbuf-tail other))))
(incf (my len) (sendbuf-len other))
(incf (my num-bufs) (sendbuf-num-bufs other))
(setf (sendbuf-num-bufs other) 0
(sendbuf-head other) nil
(sendbuf-tail other) nil
(sendbuf-len other) 0)
(values))
(defmacro with-sendbuf-continue ((var) &body body &environment env)
`(progn
,@(loop for form in (merge-constant-arguments body :join 'byte-vector-cat :env env)
collect
`(sendbuf-add ,var
,form))
(values)))
(defmacro with-sendbuf ((&optional (var (gensym "sendbuf"))) &body body)
(check-symbols var)
`(let ((,var (make-sendbuf)))
(with-sendbuf-continue (,var)
,@body)
,var))
(my-defun sendbuf done ()
(my-declare-fast-inline)
(not (my head)))
(my-defun sendbuf check-done (con finished my-call)
(my-declare-fast-inline)
(cond
((my done)
(setf (my tail) nil)
(funcall finished))
(t
(con-when-ready-to-write con my-call))))
(my-defun sendbuf shift-up (s)
(my-declare-fast-inline)
(declare (type sendbuf-small-integer s))
(decf (my len) s)
(loop until (zerop s)
do
(debug-assert (my head))
(let ((buf (car (my head))))
(cond ((>= s (length buf))
(decf s (length buf))
(decf (my num-bufs))
(setf (my head)
(cdr (my head))))
(t
(setf (car (my head)) (make-displaced-vector buf :start s))
(setf s 0))))))
(my-defun sendbuf send-write-piece-by-piece (con done)
(loop for buf = (car (my head))
while
(let ((s (socket-write (con-socket con) buf)))
(declare (type (or null sendbuf-small-integer) s))
(when s
(decf (my len) s)
(cond ((> (length buf) s)
(setf (car (my head)) (make-displaced-vector buf :start s))
nil)
(t
(setf (my head) (cdr (my head)))
(decf (my num-bufs))
(my head))))))
(my check-done con done #'my-call))
(my-defun sendbuf send-write (con done)
(let ((buf (my to-byte-vector)))
(let ((s (socket-write (con-socket con) buf)))
(declare (type (or null sendbuf-small-integer) s))
(when s
(my shift-up s))))
(my check-done con done #'my-call))
(my-defun sendbuf send-writev (con done)
(unless (my done)
(let ((count (min +max-iovecs+ (my num-bufs))))
(cffi:with-foreign-object (vecs 'iovec count)
(loop for i below count
for buf in (my head)
do
(with-pointer-to-vector-data (ptr buf)
(cffi:with-foreign-slots ((base len) (cffi:mem-aref vecs 'iovec i) iovec)
(setf base ptr)
(setf len (length buf)))))
(let ((s (socket-writev (con-socket con) vecs count)))
(declare (type (or null sendbuf-small-integer) s))
(when s
(my shift-up s))))))
(my check-done con done #'my-call))
(my-defun sendbuf to-byte-vector ()
(my-declare-fast-inline)
(let ((result (make-byte-vector (my len))) (i 0))
(unless (zerop (my len))
(loop for s in (my head) do
(etypecase s
(simple-byte-vector
(loop for c across (the simple-byte-vector s) do
(setf (aref result i) c)
(incf i)))
(byte-vector
(loop for c across (the byte-vector s) do
(setf (aref result i) c)
(incf i))))))
result))
(my-defun sendbuf 'print-object (stream)
(cond (*print-readably* (call-next-method))
(t (write (force-string (my to-byte-vector)) :stream stream))))
(my-defun sendbuf empty ()
(not (my head)))