@@ -74,6 +74,23 @@ def data_received(self, data):
74
74
self .transport .write (data )
75
75
76
76
77
+ class EchoBufferedProtocol (asyncio .BufferedProtocol ):
78
+ def connection_made (self , transport ):
79
+ self .transport = transport
80
+ # Here the buffer is intended to be copied, so that the outgoing buffer
81
+ # won't be wrongly updated by next read
82
+ self .buffer = bytearray (256 * 1024 )
83
+
84
+ def connection_lost (self , exc ):
85
+ self .transport = None
86
+
87
+ def get_buffer (self , sizehint ):
88
+ return self .buffer
89
+
90
+ def buffer_updated (self , nbytes ):
91
+ self .transport .write (self .buffer [:nbytes ])
92
+
93
+
77
94
async def print_debug (loop ):
78
95
while True :
79
96
print (chr (27 ) + "[2J" ) # clear screen
@@ -89,6 +106,7 @@ async def print_debug(loop):
89
106
parser .add_argument ('--addr' , default = '127.0.0.1:25000' , type = str )
90
107
parser .add_argument ('--print' , default = False , action = 'store_true' )
91
108
parser .add_argument ('--ssl' , default = False , action = 'store_true' )
109
+ parser .add_argument ('--buffered' , default = False , action = 'store_true' )
92
110
args = parser .parse_args ()
93
111
94
112
if args .uvloop :
@@ -140,6 +158,10 @@ async def print_debug(loop):
140
158
print ('cannot use --stream and --proto simultaneously' )
141
159
exit (1 )
142
160
161
+ if args .buffered :
162
+ print ('cannot use --stream and --buffered simultaneously' )
163
+ exit (1 )
164
+
143
165
print ('using asyncio/streams' )
144
166
if unix :
145
167
coro = asyncio .start_unix_server (echo_client_streams ,
@@ -155,12 +177,18 @@ async def print_debug(loop):
155
177
print ('cannot use --stream and --proto simultaneously' )
156
178
exit (1 )
157
179
158
- print ('using simple protocol' )
180
+ if args .buffered :
181
+ print ('using buffered protocol' )
182
+ protocol = EchoBufferedProtocol
183
+ else :
184
+ print ('using simple protocol' )
185
+ protocol = EchoProtocol
186
+
159
187
if unix :
160
- coro = loop .create_unix_server (EchoProtocol , addr ,
188
+ coro = loop .create_unix_server (protocol , addr ,
161
189
ssl = server_context )
162
190
else :
163
- coro = loop .create_server (EchoProtocol , * addr ,
191
+ coro = loop .create_server (protocol , * addr ,
164
192
ssl = server_context )
165
193
srv = loop .run_until_complete (coro )
166
194
else :
0 commit comments