2018-03-18 16:21:04 +11:00
|
|
|
function net.socket(address, port, sclose)
|
2018-03-29 23:40:06 +11:00
|
|
|
local conn, rb = {}, ""
|
|
|
|
conn.state, conn.buffer, conn.port, conn.address = "o", "", tonumber(port), address
|
2018-05-19 13:36:32 +10:00
|
|
|
function conn.r(self,l)
|
2018-03-18 16:21:04 +11:00
|
|
|
rb=self.buffer:sub(1,l)
|
|
|
|
self.buffer=self.buffer:sub(l+1)
|
|
|
|
return rb
|
|
|
|
end
|
|
|
|
function conn.w(self,data)
|
|
|
|
net.lsend(self.address,self.port,data)
|
|
|
|
end
|
|
|
|
function conn.c(s)
|
|
|
|
net.send(conn.address,conn.port,sclose)
|
|
|
|
end
|
2018-03-29 23:40:06 +11:00
|
|
|
function h(etype, from, port, data)
|
2018-03-18 16:21:04 +11:00
|
|
|
if from == conn.address and port == conn.port then
|
|
|
|
if data == sclose then
|
|
|
|
net.hook[sclose] = nil
|
|
|
|
conn.state = "c"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
conn.buffer = conn.buffer..data
|
|
|
|
end
|
|
|
|
end
|
2018-03-29 23:40:06 +11:00
|
|
|
net.hook[sclose] = h
|
2018-03-18 16:21:04 +11:00
|
|
|
return conn
|
|
|
|
end
|