@@ -115,8 +115,17 @@ local function dispatch_by_session(self)
115
115
end
116
116
end
117
117
118
+ local wait_response
119
+
118
120
local function pop_response (self )
119
- return table.remove (self .__request , 1 ), table.remove (self .__thread , 1 )
121
+ while true do
122
+ local func ,co = table.remove (self .__request , 1 ), table.remove (self .__thread , 1 )
123
+ if func then
124
+ return func , co
125
+ end
126
+ wait_response = coroutine.running ()
127
+ skynet .wait (wait_response )
128
+ end
120
129
end
121
130
122
131
local function push_response (self , response , co )
@@ -127,46 +136,43 @@ local function push_response(self, response, co)
127
136
-- response is a function, push it to __request
128
137
table.insert (self .__request , response )
129
138
table.insert (self .__thread , co )
139
+ if wait_response then
140
+ skynet .wakeup (wait_response )
141
+ wait_response = nil
142
+ end
130
143
end
131
144
end
132
145
133
146
local function dispatch_by_order (self )
134
147
while self .__sock do
135
148
local func , co = pop_response (self )
136
- if func == nil then
137
- if not socket .block (self .__sock [1 ]) then
138
- close_channel_socket (self )
139
- wakeup_all (self )
140
- end
141
- else
142
- local ok , result_ok , result_data , padding = pcall (func , self .__sock )
143
- if ok then
144
- if padding and result_ok then
145
- -- if padding is true, wait for next result_data
146
- -- self.__result_data[co] is a table
147
- local result = self .__result_data [co ] or {}
148
- self .__result_data [co ] = result
149
- table.insert (result , result_data )
150
- else
151
- self .__result [co ] = result_ok
152
- if result_ok and self .__result_data [co ] then
153
- table.insert (self .__result_data [co ], result_data )
154
- else
155
- self .__result_data [co ] = result_data
156
- end
157
- skynet .wakeup (co )
158
- end
149
+ local ok , result_ok , result_data , padding = pcall (func , self .__sock )
150
+ if ok then
151
+ if padding and result_ok then
152
+ -- if padding is true, wait for next result_data
153
+ -- self.__result_data[co] is a table
154
+ local result = self .__result_data [co ] or {}
155
+ self .__result_data [co ] = result
156
+ table.insert (result , result_data )
159
157
else
160
- close_channel_socket (self )
161
- local errmsg
162
- if result_ok ~= socket_error then
163
- errmsg = result_ok
158
+ self .__result [co ] = result_ok
159
+ if result_ok and self .__result_data [co ] then
160
+ table.insert (self .__result_data [co ], result_data )
161
+ else
162
+ self .__result_data [co ] = result_data
164
163
end
165
- self .__result [co ] = socket_error
166
- self .__result_data [co ] = errmsg
167
164
skynet .wakeup (co )
168
- wakeup_all (self , errmsg )
169
165
end
166
+ else
167
+ close_channel_socket (self )
168
+ local errmsg
169
+ if result_ok ~= socket_error then
170
+ errmsg = result_ok
171
+ end
172
+ self .__result [co ] = socket_error
173
+ self .__result_data [co ] = errmsg
174
+ skynet .wakeup (co )
175
+ wakeup_all (self , errmsg )
170
176
end
171
177
end
172
178
end
@@ -288,7 +294,7 @@ local function block_connect(self, once)
288
294
-- connecting in other coroutine
289
295
local co = coroutine.running ()
290
296
table.insert (self .__connecting , co )
291
- skynet .wait ()
297
+ skynet .wait (co )
292
298
else
293
299
self .__connecting [1 ] = true
294
300
try_connect (self , once )
319
325
local function wait_for_response (self , response )
320
326
local co = coroutine.running ()
321
327
push_response (self , response , co )
322
- skynet .wait ()
328
+ skynet .wait (co )
323
329
324
330
local result = self .__result [co ]
325
331
self .__result [co ] = nil
0 commit comments