mirror of
https://github.com/ShadowKatStudios/OC-Minitel.git
synced 2024-11-01 08:10:56 +11:00
implemented socket closing
This commit is contained in:
parent
688f8e0ff7
commit
22bed6dd9c
@ -47,7 +47,9 @@ end
|
|||||||
-- socket stuff, layer 5?
|
-- socket stuff, layer 5?
|
||||||
|
|
||||||
local function cwrite(self,data)
|
local function cwrite(self,data)
|
||||||
net.send(self.addr,self.port,data)
|
if self.state == "open" then
|
||||||
|
net.send(self.addr,self.port,data)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
local function cread(self,length)
|
local function cread(self,length)
|
||||||
local rdata = ""
|
local rdata = ""
|
||||||
@ -56,7 +58,7 @@ local function cread(self,length)
|
|||||||
return rdata
|
return rdata
|
||||||
end
|
end
|
||||||
|
|
||||||
local function socket(addr,port) -- todo, add remote closing of sockets
|
local function socket(addr,port,sclose) -- todo, add remote closing of sockets
|
||||||
local conn = {}
|
local conn = {}
|
||||||
conn.addr,conn.port = addr,tonumber(port)
|
conn.addr,conn.port = addr,tonumber(port)
|
||||||
conn.rbuffer = ""
|
conn.rbuffer = ""
|
||||||
@ -65,13 +67,18 @@ local function socket(addr,port) -- todo, add remote closing of sockets
|
|||||||
conn.state = "open"
|
conn.state = "open"
|
||||||
local function listener(_,f,p,d)
|
local function listener(_,f,p,d)
|
||||||
if f == conn.addr and p == conn.port then
|
if f == conn.addr and p == conn.port then
|
||||||
conn.rbuffer = conn.rbuffer .. d
|
if d == sclose then
|
||||||
|
conn:close()
|
||||||
|
else
|
||||||
|
conn.rbuffer = conn.rbuffer .. d
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
event.listen("net_msg",listener)
|
event.listen("net_msg",listener)
|
||||||
function conn.close(self)
|
function conn.close(self)
|
||||||
event.ignore("net_msg",listener)
|
event.ignore("net_msg",listener)
|
||||||
conn.state = "closed"
|
conn.state = "closed"
|
||||||
|
net.rsend(addr,port,sclose)
|
||||||
end
|
end
|
||||||
return conn
|
return conn
|
||||||
end
|
end
|
||||||
@ -95,7 +102,12 @@ function net.open(to,port)
|
|||||||
if not est then
|
if not est then
|
||||||
return nil, "refused"
|
return nil, "refused"
|
||||||
end
|
end
|
||||||
return socket(to,data,port)
|
data = tonumber(data)
|
||||||
|
sclose = ""
|
||||||
|
repeat
|
||||||
|
_,from,nport,sclose = event.pull("net_msg")
|
||||||
|
until from == to and nport == data
|
||||||
|
return socket(to,data,sclose)
|
||||||
end
|
end
|
||||||
|
|
||||||
function net.listen(port)
|
function net.listen(port)
|
||||||
@ -103,8 +115,10 @@ function net.listen(port)
|
|||||||
_, from, rport, data = event.pull("net_msg")
|
_, from, rport, data = event.pull("net_msg")
|
||||||
until rport == port
|
until rport == port
|
||||||
local nport = math.random(net.minport,net.maxport)
|
local nport = math.random(net.minport,net.maxport)
|
||||||
|
local sclose = net.genPacketID()
|
||||||
net.rsend(from,rport,tostring(nport))
|
net.rsend(from,rport,tostring(nport))
|
||||||
return socket(from,nport,port)
|
net.rsend(from,nport,sclose)
|
||||||
|
return socket(from,nport,sclose)
|
||||||
end
|
end
|
||||||
|
|
||||||
return net
|
return net
|
||||||
|
Loading…
Reference in New Issue
Block a user