@@ -8,6 +8,9 @@ local define = require 'proto.define'
88local json = require ' json'
99local inspect = require ' inspect'
1010local thread = require ' bee.thread'
11+ local fs = require ' bee.filesystem'
12+ local net = require ' service.net'
13+ local timer = require ' timer'
1114
1215local reqCounter = util .counter ()
1316
@@ -32,8 +35,7 @@ m.ability = {}
3235m .waiting = {}
3336m .holdon = {}
3437m .mode = ' stdio'
35- --- @type bee.socket.fd
36- m .fd = nil
38+ m .client = nil
3739
3840function m .getMethodName (proto )
3941 if proto .method :sub (1 , 2 ) == ' $/' then
@@ -54,7 +56,8 @@ function m.send(data)
5456 if m .mode == ' stdio' then
5557 io.write (buf )
5658 elseif m .mode == ' socket' then
57- m .fd :send (buf )
59+ m .client :write (buf )
60+ m .client :update ()
5861 end
5962end
6063
@@ -237,13 +240,37 @@ function m.listen(mode, socketPort)
237240 io.stdout :setvbuf ' no'
238241 pub .task (' loadProtoByStdio' )
239242 elseif mode == ' socket' then
240- local rfd = assert (socket (' tcp' ))
241- rfd :connect (' 127.0.0.1' , socketPort )
242- local wfd1 , wfd2 = socket .pair ()
243- m .fd = wfd1
243+ local unixFolder = LOGPATH .. ' /unix'
244+ fs .create_directories (fs .path (unixFolder ))
245+ local unixPath = unixFolder .. ' /' .. tostring (socketPort )
246+
247+ local server = net .listen (' unix' , unixPath )
248+
249+ assert (server )
250+
251+ local dummyClient = {
252+ buf = ' ' ,
253+ write = function (self , data )
254+ self .buf = self .buf .. data
255+ end ,
256+ update = function () end ,
257+ }
258+ m .client = dummyClient
259+
260+ local t = timer .loop (0.1 , function ()
261+ net .update ()
262+ end )
263+
264+ function server :on_accept (client )
265+ t :remove ()
266+ m .client = client
267+ client :write (dummyClient .buf )
268+ client :update ()
269+ end
270+
244271 pub .task (' loadProtoBySocket' , {
245- wfd = wfd2 : detach () ,
246- rfd = rfd : detach () ,
272+ port = socketPort ,
273+ unixPath = unixPath ,
247274 })
248275 end
249276end
0 commit comments