@@ -22,7 +22,7 @@ local tcp_stop = tcp.stop
2222local tcp_free_ssl = tcp .free_ssl
2323local tcp_close = tcp .close
2424local tcp_connect = tcp .connect
25- local tcp_ssl_do_handshak = tcp .ssl_connect
25+ local tcp_ssl_connect = tcp .ssl_connect
2626local tcp_read = tcp .read
2727local tcp_sslread = tcp .ssl_read
2828local tcp_write = tcp .write
@@ -154,94 +154,90 @@ function TCP:recv(bytes)
154154 if self .ssl then
155155 return Log :ERROR (" Please use ssl_recv method :)" )
156156 end
157- local buf , len = tcp_read (self .fd , bytes )
158- if buf then
159- return buf , len
160- end
161157 self .READ_IO = tcp_pop ()
162158 local co = co_self ()
163159 self .read_current_co = co_self ()
164160 self .read_co = co_new (function ( ... )
165- local buf , len = tcp_read (self .fd , bytes )
166- if self .timer then
167- self .timer :stop ()
168- self .timer = nil
169- end
170- tcp_push (self .READ_IO )
171- tcp_stop (self .READ_IO )
172- self .READ_IO = nil
173- self .read_co = nil
174- self .read_current_co = nil
175- if not buf then
176- return co_wakeup (co )
177- end
178- return co_wakeup (co , buf , len )
179- end )
180- self .timer = ti .timeout (self ._timeout , function ( ... )
181- tcp_push (self .READ_IO )
182- tcp_stop (self .READ_IO )
183- self .timer = nil
184- self .read_co = nil
185- self .READ_IO = nil
186- self .read_current_co = nil
187- return co_wakeup (co , nil , " read timeout" )
188- end )
189- tcp_start (self .READ_IO , self .fd , EVENT_READ , self .read_co )
190- return co_wait ()
191- end
192-
193- function TCP :ssl_recv (bytes )
194- if not self .ssl then
195- return Log :ERROR (" Please use recv method :)" )
196- end
197- local buf , len = tcp_sslread (self .ssl , bytes )
198- if buf then
199- return buf , len
200- end
201- local co = co_self ()
202- self .read_current_co = co_self ()
203- self .READ_IO = tcp_pop ()
204- self .read_co = co_new (function ( ... )
205- while 1 do
206- local buf , len = tcp_sslread (self .ssl , bytes )
207- if not buf and not len then
161+ local buf , len = tcp_read (self .fd , bytes )
208162 if self .timer then
209- self .timer :stop ()
210- self .timer = nil
163+ self .timer :stop ()
164+ self .timer = nil
211165 end
212166 tcp_push (self .READ_IO )
213167 tcp_stop (self .READ_IO )
214168 self .READ_IO = nil
215169 self .read_co = nil
216- self .read_current_co = nil
217- return co_wakeup (co )
218- end
219- if buf and len then
220- if self .timer then
221- self .timer :stop ()
222- self .timer = nil
170+ self .read_current_co = nil
171+ if not buf then
172+ return co_wakeup (co )
223173 end
174+ return co_wakeup (co , buf , len )
175+ end )
176+ self .timer = ti .timeout (self ._timeout , function ( ... )
224177 tcp_push (self .READ_IO )
225178 tcp_stop (self .READ_IO )
226- self .READ_IO = nil
179+ self .timer = nil
227180 self .read_co = nil
181+ self .READ_IO = nil
228182 self .read_current_co = nil
229- return co_wakeup (co , buf , len )
230- end
231- co_wait ()
183+ return co_wakeup (co , nil , " read timeout" )
184+ end )
185+ tcp_start (self .READ_IO , self .fd , EVENT_READ , self .read_co )
186+ return co_wait ()
187+ end
188+
189+ function TCP :ssl_recv (bytes )
190+ if not self .ssl then
191+ return Log :ERROR (" Please use recv method :)" )
232192 end
233- end )
234- self .timer = ti .timeout (self ._timeout , function ( ... )
235- tcp_push (self .READ_IO )
236- tcp_stop (self .READ_IO )
237- self .timer = nil
238- self .READ_IO = nil
239- self .read_co = nil
240- self .read_current_co = nil
241- return co_wakeup (co , nil , " read timeout" )
242- end )
243- tcp_start (self .READ_IO , self .fd , EVENT_READ , self .read_co )
244- return co_wait ()
193+ local buf , len = tcp_sslread (self .ssl , bytes )
194+ if not buf then
195+ local co = co_self ()
196+ self .read_current_co = co_self ()
197+ self .READ_IO = tcp_pop ()
198+ self .read_co = co_new (function ( ... )
199+ while 1 do
200+ local buf , len = tcp_sslread (self .ssl , bytes )
201+ if not buf and not len then
202+ if self .timer then
203+ self .timer :stop ()
204+ self .timer = nil
205+ end
206+ tcp_push (self .READ_IO )
207+ tcp_stop (self .READ_IO )
208+ self .READ_IO = nil
209+ self .read_co = nil
210+ self .read_current_co = nil
211+ return co_wakeup (co )
212+ end
213+ if buf and len then
214+ if self .timer then
215+ self .timer :stop ()
216+ self .timer = nil
217+ end
218+ tcp_push (self .READ_IO )
219+ tcp_stop (self .READ_IO )
220+ self .READ_IO = nil
221+ self .read_co = nil
222+ self .read_current_co = nil
223+ return co_wakeup (co , buf , len )
224+ end
225+ co_wait ()
226+ end
227+ end )
228+ self .timer = ti .timeout (self ._timeout , function ( ... )
229+ tcp_push (self .READ_IO )
230+ tcp_stop (self .READ_IO )
231+ self .timer = nil
232+ self .READ_IO = nil
233+ self .read_co = nil
234+ self .read_current_co = nil
235+ return co_wakeup (co , nil , " read timeout" )
236+ end )
237+ tcp_start (self .READ_IO , self .fd , EVENT_READ , self .read_co )
238+ return co_wait ()
239+ end
240+ return buf , len
245241end
246242
247243function TCP :listen (ip , port , cb )
@@ -287,7 +283,7 @@ function TCP:connect(domain, port)
287283 if connected then
288284 return co_wakeup (co , true )
289285 end
290- return co_wakeup (co , false , ' connect refused ' )
286+ return co_wakeup (co , false , ' 连接失败 ' )
291287 end )
292288 self .timer = ti .timeout (self ._timeout , function ( ... )
293289 tcp_push (self .CONNECT_IO )
@@ -311,13 +307,14 @@ function TCP:ssl_connect(domain, port)
311307 if not self .ssl_ctx or not self .ssl then
312308 return Log :ERROR (" Create a SSL Error! :) " )
313309 end
310+
314311 local co = co_self ()
315312 self .CONNECT_IO = tcp_pop ()
316313 self .connect_current_co = co
317314 self .connect_co = co_new (function ()
318315 local EVENTS = EVENT_WRITE
319316 while 1 do
320- local ok , EVENT = tcp_ssl_do_handshak (self .ssl )
317+ local ok , EVENT = tcp_ssl_connect (self .ssl )
321318 if ok or not EVENT then
322319 if self .timer then
323320 self .timer :stop ()
@@ -328,7 +325,7 @@ function TCP:ssl_connect(domain, port)
328325 self .CONNECT_IO = nil
329326 self .connect_co = nil
330327 self .connect_current_co = nil
331- return co_wakeup (co , ok , not ok and ' ssl_connect refused ' )
328+ return co_wakeup (co , ok )
332329 end
333330 if EVENTS ~= EVENT then
334331 EVENTS = EVENT
0 commit comments