working on tcp, not working yet
This commit is contained in:
parent
8f8fd31e64
commit
c61ee0537d
@ -1,8 +1,10 @@
|
||||
package.path = "./?/init.lua;./?.lua;"..package.path
|
||||
local net = require("net.udp")
|
||||
local client = net:newUDPClient("localhost",12345)
|
||||
local net = require("net.tcp")
|
||||
local client = net:newTCPClient("localhost",12345)
|
||||
|
||||
client:send("Test!")
|
||||
multi:newAlarm(1):OnRing(function()
|
||||
client:send("Test!")
|
||||
end)
|
||||
|
||||
client.OnDataRecieved(function(c,data)
|
||||
print("Response: ",data)
|
||||
|
||||
@ -17,23 +17,10 @@ function client:init(type)
|
||||
self.process.Start()
|
||||
end
|
||||
function client:send(data)
|
||||
local dat = {data = data}
|
||||
if self.Type == "udp" then
|
||||
self.OnPreSend:Fire(dat)
|
||||
self.udp:send(dat.data)
|
||||
elseif self.Type == "tcp" then
|
||||
self.OnPreSend:Fire(dat)
|
||||
self.udp:send(dat.data)
|
||||
local ind, err = self.tcp:send(dat.data)
|
||||
if err == "closed" then
|
||||
self.OnClientDisconnected:Fire(self,err)
|
||||
elseif err == "timeout" then
|
||||
self.OnClientDisconnected:Fire(self,err)
|
||||
end
|
||||
end
|
||||
-- Override this function
|
||||
end
|
||||
function client:close()
|
||||
--
|
||||
-- Override this function
|
||||
end
|
||||
function client:setUpdateRate(n)
|
||||
self.updaterRate = n
|
||||
|
||||
662
net/init.lua
662
net/init.lua
@ -599,335 +599,335 @@ end
|
||||
-- c.OnClientConnected = multi:newConnection()
|
||||
-- return c
|
||||
-- end
|
||||
function net:newTCPServer(port)
|
||||
local c = {}
|
||||
local port = port or 0
|
||||
c.tcp = assert(socket.bind("*", port))
|
||||
c.tcp:settimeout(0)
|
||||
c.ip, c.port = c.tcp:getsockname()
|
||||
c.ips = {}
|
||||
if port == 0 then
|
||||
_, c.port = c.tcp:getsockname()
|
||||
end
|
||||
c.ids = {}
|
||||
c.bannedIPs = {}
|
||||
c.Type = "tcp"
|
||||
c.rMode = "*l"
|
||||
c.sMode = "*l"
|
||||
c.updaterRate = 1
|
||||
c.autoNormalization = false
|
||||
c.updates = {}
|
||||
c.links = {}
|
||||
c.numspace = 4
|
||||
c.broad = socket.udp()
|
||||
c.hostip = net.getLocalIP()
|
||||
function c:packMsg(data)
|
||||
local temp = bin.new()
|
||||
temp:addBlock(#data, self.numspace, "n")
|
||||
temp:addBlock(data)
|
||||
return temp:getData()
|
||||
end
|
||||
function c:enableBinaryMode()
|
||||
self.rMode = "b"
|
||||
self.sMode = "b"
|
||||
end
|
||||
function c:broadcast(name)
|
||||
table.insert(
|
||||
net.BroadcastDriver,
|
||||
function(loop, dt)
|
||||
self.broad:setoption("broadcast", true)
|
||||
self.broad:sendto(name .. "|" .. self.Type .. "|" .. self.hostip .. ":" .. self.port, "255.255.255.255", 11111)
|
||||
self.broad:setoption("broadcast", false)
|
||||
end
|
||||
)
|
||||
end
|
||||
function c:setUpdateRate(n)
|
||||
self.updaterRate = n
|
||||
end
|
||||
function c:setReceiveMode(mode)
|
||||
self.rMode = mode
|
||||
end
|
||||
function c:setSendMode(mode)
|
||||
self.sMode = mode
|
||||
end
|
||||
function c:banCID(cid)
|
||||
--print("Function not supported on a tcp server!")
|
||||
end
|
||||
function c:banIP(ip)
|
||||
table.insert(self.bannedIPs, cid)
|
||||
end
|
||||
function c:send(handle, data)
|
||||
if self.autoNormalization then
|
||||
data = net.normalize(data)
|
||||
end
|
||||
if self.sMode == "*l" then
|
||||
handle:send(data .. "\n")
|
||||
elseif self.sMode == "b" then
|
||||
handle:send(self:packMsg(data))
|
||||
else
|
||||
handle:send(data)
|
||||
end
|
||||
end
|
||||
function c:sendAllData(handle, data)
|
||||
if self.autoNormalization then
|
||||
data = net.normalize(data)
|
||||
end
|
||||
handle:send(data)
|
||||
end
|
||||
function c:pollClientModules(ip, port)
|
||||
self:send(ip, "L!", port)
|
||||
end
|
||||
function c:CIDFrom(ip, port)
|
||||
--print("Method not supported when using a TCP Server!")
|
||||
return "CIDs in TCP work differently!"
|
||||
end
|
||||
function c:sendAll(data)
|
||||
for i, v in pairs(self.ips) do
|
||||
self:send(v, data)
|
||||
end
|
||||
end
|
||||
function c:sendAllBut(data, cid)
|
||||
for i, v in pairs(self.ips) do
|
||||
if not (cid == i) then
|
||||
self:send(v, data)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:clientRegistered(cid)
|
||||
return self.ips[cid]
|
||||
end
|
||||
function c:clientLoggedIn(cid)
|
||||
return self.ips[cid]
|
||||
end
|
||||
function c:getUpdater(cid)
|
||||
return self.updates[cid]
|
||||
end
|
||||
function c:update()
|
||||
local client = self.tcp:accept(self.rMode)
|
||||
if not client then
|
||||
return
|
||||
end
|
||||
table.insert(self.ips, client)
|
||||
client:settimeout(0)
|
||||
client:setoption("keepalive", true)
|
||||
ip, port = client:getpeername()
|
||||
if ip and port then
|
||||
self.OnClientConnected:Fire(self, client, client, ip)
|
||||
multi:newThread("ServerClientHandler",function()
|
||||
while true do
|
||||
thread.skip(1)
|
||||
local data, err, dat, len
|
||||
if self.rMode == "b" then
|
||||
thread.hold(
|
||||
function()
|
||||
dat = client:receive(self.numspace)
|
||||
return dat
|
||||
end
|
||||
)
|
||||
len = bin.new(dat):getBlock("n", self.numspace)
|
||||
data, err = client:receive(len)
|
||||
else
|
||||
data, err = client:receive(self.rMode)
|
||||
end
|
||||
if err == "closed" then
|
||||
for i = 1, #self.ips do
|
||||
if self.ips[i] == client then
|
||||
table.remove(self.ips, i)
|
||||
end
|
||||
end
|
||||
self.OnClientClosed:Fire(self, "Client Closed Connection!", client, client, ip)
|
||||
self.links[client] = nil -- lets clean up
|
||||
self:Destroy()
|
||||
thread.kill()
|
||||
end
|
||||
if data then
|
||||
if self.autoNormalization then
|
||||
data = net.denormalize(data)
|
||||
end
|
||||
if net.inList(self.bannedIPs, ip) then
|
||||
return
|
||||
end
|
||||
local hook = data:match("!(.-)!")
|
||||
self.OnDataRecieved:getConnection(hook):Fire(self, data, client, client, ip, self)
|
||||
if data:sub(1, 2) == "L!" then
|
||||
cList = data
|
||||
local list = {}
|
||||
for m, v in cList:gmatch("(%S-):(%S-)|") do
|
||||
list[m] = v
|
||||
end
|
||||
self.OnClientsModulesList:Fire(list, client, client, ip)
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
c.OnClientsModulesList = multi:newConnection()
|
||||
c.OnDataRecieved = multi:newConnection()
|
||||
c.OnClientClosed = multi:newConnection()
|
||||
c.OnClientConnected = multi:newConnection()
|
||||
table.insert(net.ConnectionDriver, c)
|
||||
net.OnServerCreated:Fire(c)
|
||||
return c
|
||||
end
|
||||
function net:newTCPClient(host, port)
|
||||
local c = {}
|
||||
c.ip = assert(socket.dns.toip(host))
|
||||
c.tcp = socket.connect(c.ip, port)
|
||||
if not c.tcp then
|
||||
--print("Can't connect to the server: no response from server")
|
||||
return false
|
||||
end
|
||||
c.tcp:settimeout(0)
|
||||
--c.tcp:setoption('tcp-nodelay', true)
|
||||
c.tcp:setoption("keepalive", true)
|
||||
c.Type = "tcp"
|
||||
c.autoReconnect = true
|
||||
c.rMode = "*l"
|
||||
c.sMode = "*l"
|
||||
c.autoNormalization = false
|
||||
c.numspace = 4
|
||||
function c:enableBinaryMode()
|
||||
self.rMode = "b"
|
||||
self.sMode = "b"
|
||||
end
|
||||
function c:setReceiveMode(mode)
|
||||
self.rMode = mode
|
||||
end
|
||||
function c:setSendMode(mode)
|
||||
self.sMode = mode
|
||||
end
|
||||
function c:packMsg(data)
|
||||
local temp = bin.new()
|
||||
temp:addBlock(#data, self.numspace)
|
||||
temp:addBlock(data)
|
||||
return temp:getData()
|
||||
end
|
||||
function c:send(data)
|
||||
if self.autoNormalization then
|
||||
data = net.normalize(data)
|
||||
end
|
||||
if self.sMode == "*l" then
|
||||
ind, err = self.tcp:send(data .. "\n")
|
||||
elseif self.sMode == "b" then
|
||||
ind, err = self.tcp:send(self:packMsg(data))
|
||||
else
|
||||
ind, err = self.tcp:send(data)
|
||||
end
|
||||
if err == "closed" then
|
||||
self.OnClientDisconnected:Fire(self, err)
|
||||
elseif err == "timeout" then
|
||||
self.OnClientDisconnected:Fire(self, err)
|
||||
elseif err then
|
||||
--print(err)
|
||||
end
|
||||
end
|
||||
function c:sendRaw(data)
|
||||
if self.autoNormalization then
|
||||
data = net.normalize(data)
|
||||
end
|
||||
self.tcp:send(data)
|
||||
end
|
||||
function c:getCID()
|
||||
return "No Cid on a tcp client!"
|
||||
end
|
||||
function c:close()
|
||||
self.tcp:close()
|
||||
end
|
||||
function c:IDAssigned()
|
||||
return true
|
||||
end
|
||||
function c:update()
|
||||
if not self.tcp then
|
||||
return
|
||||
end
|
||||
local data, err, dat
|
||||
if self.rMode == "b" then
|
||||
thread.hold(
|
||||
function()
|
||||
dat = self.tcp:receive(self.numspace)
|
||||
return dat
|
||||
end
|
||||
)
|
||||
len = bin.new(dat):getBlock("n", self.numspace)
|
||||
data, err = self.tcp:receive(len)
|
||||
else
|
||||
data, err = self.tcp:receive()
|
||||
end
|
||||
if err == "closed" then
|
||||
self.OnClientDisconnected:Fire(self, err)
|
||||
elseif err == "timeout" then
|
||||
self.OnClientDisconnected:Fire(self, err)
|
||||
elseif err then
|
||||
--print(err)
|
||||
end
|
||||
if data then
|
||||
if self.autoNormalization then
|
||||
data = net.denormalize(data)
|
||||
end
|
||||
local hook = data:match("!(.-)!")
|
||||
self.OnDataRecieved:getConnection(hook):Fire(self, data)
|
||||
end
|
||||
end
|
||||
function c:reconnect()
|
||||
multi:newFunction(
|
||||
function(func)
|
||||
self.tcp = socket.connect(self.ip, self.port)
|
||||
if self.tcp == nil then
|
||||
--print("Can't connect to the server: No response from server!")
|
||||
multi:newAlarm(3):OnRing(
|
||||
function(alarm)
|
||||
self:reconnect()
|
||||
alarm:Destroy()
|
||||
return
|
||||
end
|
||||
):setName("net.timeoutTask")
|
||||
end
|
||||
self.OnConnectionRegained:Fire(self)
|
||||
self.tcp:settimeout(0)
|
||||
--self.tcp:setoption('tcp-nodelay', true)
|
||||
self.tcp:setoption("keepalive", true)
|
||||
end
|
||||
)
|
||||
end
|
||||
c.event =
|
||||
multi:newEvent(
|
||||
function(event)
|
||||
return event.link:IDAssigned()
|
||||
end
|
||||
):OnEvent(
|
||||
function(event)
|
||||
event.link.OnClientReady:Fire(event.link)
|
||||
event:Destroy()
|
||||
end
|
||||
)
|
||||
c.event:setName("net.handshakeTask")
|
||||
c.event.link = c
|
||||
c.OnClientReady = multi:newConnection()
|
||||
c.OnClientDisconnected = multi:newConnection()
|
||||
c.OnDataRecieved = multi:newConnection()
|
||||
c.OnConnectionRegained = multi:newConnection()
|
||||
table.insert(net.ConnectionDriver, c)
|
||||
net.OnClientCreated:Fire(c)
|
||||
return c
|
||||
end
|
||||
net.timer = multi:newTimer():Start()
|
||||
multi:newThread(
|
||||
"ClientServerHandler",
|
||||
function()
|
||||
while true do
|
||||
thread.skip()
|
||||
for i = 1, #net.ConnectionDriver do
|
||||
thread.skip()
|
||||
net.ConnectionDriver[i]:update()
|
||||
end
|
||||
if net.timer:Get() >= 1 then
|
||||
for i = 1, #net.BroadcastDriver do
|
||||
net.BroadcastDriver[i]()
|
||||
end
|
||||
net.timer:Reset()
|
||||
end
|
||||
end
|
||||
end
|
||||
)
|
||||
-- function net:newTCPServer(port)
|
||||
-- local c = {}
|
||||
-- local port = port or 0
|
||||
-- c.tcp = assert(socket.bind("*", port))
|
||||
-- c.tcp:settimeout(0)
|
||||
-- c.ip, c.port = c.tcp:getsockname()
|
||||
-- c.ips = {}
|
||||
-- if port == 0 then
|
||||
-- _, c.port = c.tcp:getsockname()
|
||||
-- end
|
||||
-- c.ids = {}
|
||||
-- c.bannedIPs = {}
|
||||
-- c.Type = "tcp"
|
||||
-- c.rMode = "*l"
|
||||
-- c.sMode = "*l"
|
||||
-- c.updaterRate = 1
|
||||
-- c.autoNormalization = false
|
||||
-- c.updates = {}
|
||||
-- c.links = {}
|
||||
-- c.numspace = 4
|
||||
-- c.broad = socket.udp()
|
||||
-- c.hostip = net.getLocalIP()
|
||||
-- function c:packMsg(data)
|
||||
-- local temp = bin.new()
|
||||
-- temp:addBlock(#data, self.numspace, "n")
|
||||
-- temp:addBlock(data)
|
||||
-- return temp:getData()
|
||||
-- end
|
||||
-- function c:enableBinaryMode()
|
||||
-- self.rMode = "b"
|
||||
-- self.sMode = "b"
|
||||
-- end
|
||||
-- function c:broadcast(name)
|
||||
-- table.insert(
|
||||
-- net.BroadcastDriver,
|
||||
-- function(loop, dt)
|
||||
-- self.broad:setoption("broadcast", true)
|
||||
-- self.broad:sendto(name .. "|" .. self.Type .. "|" .. self.hostip .. ":" .. self.port, "255.255.255.255", 11111)
|
||||
-- self.broad:setoption("broadcast", false)
|
||||
-- end
|
||||
-- )
|
||||
-- end
|
||||
-- function c:setUpdateRate(n)
|
||||
-- self.updaterRate = n
|
||||
-- end
|
||||
-- function c:setReceiveMode(mode)
|
||||
-- self.rMode = mode
|
||||
-- end
|
||||
-- function c:setSendMode(mode)
|
||||
-- self.sMode = mode
|
||||
-- end
|
||||
-- function c:banCID(cid)
|
||||
-- --print("Function not supported on a tcp server!")
|
||||
-- end
|
||||
-- function c:banIP(ip)
|
||||
-- table.insert(self.bannedIPs, cid)
|
||||
-- end
|
||||
-- function c:send(handle, data)
|
||||
-- if self.autoNormalization then
|
||||
-- data = net.normalize(data)
|
||||
-- end
|
||||
-- if self.sMode == "*l" then
|
||||
-- handle:send(data .. "\n")
|
||||
-- elseif self.sMode == "b" then
|
||||
-- handle:send(self:packMsg(data))
|
||||
-- else
|
||||
-- handle:send(data)
|
||||
-- end
|
||||
-- end
|
||||
-- function c:sendAllData(handle, data)
|
||||
-- if self.autoNormalization then
|
||||
-- data = net.normalize(data)
|
||||
-- end
|
||||
-- handle:send(data)
|
||||
-- end
|
||||
-- function c:pollClientModules(ip, port)
|
||||
-- self:send(ip, "L!", port)
|
||||
-- end
|
||||
-- function c:CIDFrom(ip, port)
|
||||
-- --print("Method not supported when using a TCP Server!")
|
||||
-- return "CIDs in TCP work differently!"
|
||||
-- end
|
||||
-- function c:sendAll(data)
|
||||
-- for i, v in pairs(self.ips) do
|
||||
-- self:send(v, data)
|
||||
-- end
|
||||
-- end
|
||||
-- function c:sendAllBut(data, cid)
|
||||
-- for i, v in pairs(self.ips) do
|
||||
-- if not (cid == i) then
|
||||
-- self:send(v, data)
|
||||
-- end
|
||||
-- end
|
||||
-- end
|
||||
-- function c:clientRegistered(cid)
|
||||
-- return self.ips[cid]
|
||||
-- end
|
||||
-- function c:clientLoggedIn(cid)
|
||||
-- return self.ips[cid]
|
||||
-- end
|
||||
-- function c:getUpdater(cid)
|
||||
-- return self.updates[cid]
|
||||
-- end
|
||||
-- function c:update()
|
||||
-- local client = self.tcp:accept(self.rMode)
|
||||
-- if not client then
|
||||
-- return
|
||||
-- end
|
||||
-- table.insert(self.ips, client)
|
||||
-- client:settimeout(0)
|
||||
-- client:setoption("keepalive", true)
|
||||
-- ip, port = client:getpeername()
|
||||
-- if ip and port then
|
||||
-- self.OnClientConnected:Fire(self, client, client, ip)
|
||||
-- multi:newThread("ServerClientHandler",function()
|
||||
-- while true do
|
||||
-- thread.skip(1)
|
||||
-- local data, err, dat, len
|
||||
-- if self.rMode == "b" then
|
||||
-- thread.hold(
|
||||
-- function()
|
||||
-- dat = client:receive(self.numspace)
|
||||
-- return dat
|
||||
-- end
|
||||
-- )
|
||||
-- len = bin.new(dat):getBlock("n", self.numspace)
|
||||
-- data, err = client:receive(len)
|
||||
-- else
|
||||
-- data, err = client:receive(self.rMode)
|
||||
-- end
|
||||
-- if err == "closed" then
|
||||
-- for i = 1, #self.ips do
|
||||
-- if self.ips[i] == client then
|
||||
-- table.remove(self.ips, i)
|
||||
-- end
|
||||
-- end
|
||||
-- self.OnClientClosed:Fire(self, "Client Closed Connection!", client, client, ip)
|
||||
-- self.links[client] = nil -- lets clean up
|
||||
-- self:Destroy()
|
||||
-- thread.kill()
|
||||
-- end
|
||||
-- if data then
|
||||
-- if self.autoNormalization then
|
||||
-- data = net.denormalize(data)
|
||||
-- end
|
||||
-- if net.inList(self.bannedIPs, ip) then
|
||||
-- return
|
||||
-- end
|
||||
-- local hook = data:match("!(.-)!")
|
||||
-- self.OnDataRecieved:getConnection(hook):Fire(self, data, client, client, ip, self)
|
||||
-- if data:sub(1, 2) == "L!" then
|
||||
-- cList = data
|
||||
-- local list = {}
|
||||
-- for m, v in cList:gmatch("(%S-):(%S-)|") do
|
||||
-- list[m] = v
|
||||
-- end
|
||||
-- self.OnClientsModulesList:Fire(list, client, client, ip)
|
||||
-- end
|
||||
-- end
|
||||
-- end
|
||||
-- end)
|
||||
-- end
|
||||
-- end
|
||||
-- c.OnClientsModulesList = multi:newConnection()
|
||||
-- c.OnDataRecieved = multi:newConnection()
|
||||
-- c.OnClientClosed = multi:newConnection()
|
||||
-- c.OnClientConnected = multi:newConnection()
|
||||
-- table.insert(net.ConnectionDriver, c)
|
||||
-- net.OnServerCreated:Fire(c)
|
||||
-- return c
|
||||
-- end
|
||||
-- function net:newTCPClient(host, port)
|
||||
-- local c = {}
|
||||
-- c.ip = assert(socket.dns.toip(host))
|
||||
-- c.tcp = socket.connect(c.ip, port)
|
||||
-- if not c.tcp then
|
||||
-- --print("Can't connect to the server: no response from server")
|
||||
-- return false
|
||||
-- end
|
||||
-- c.tcp:settimeout(0)
|
||||
-- --c.tcp:setoption('tcp-nodelay', true)
|
||||
-- c.tcp:setoption("keepalive", true)
|
||||
-- c.Type = "tcp"
|
||||
-- c.autoReconnect = true
|
||||
-- c.rMode = "*l"
|
||||
-- c.sMode = "*l"
|
||||
-- c.autoNormalization = false
|
||||
-- c.numspace = 4
|
||||
-- function c:enableBinaryMode()
|
||||
-- self.rMode = "b"
|
||||
-- self.sMode = "b"
|
||||
-- end
|
||||
-- function c:setReceiveMode(mode)
|
||||
-- self.rMode = mode
|
||||
-- end
|
||||
-- function c:setSendMode(mode)
|
||||
-- self.sMode = mode
|
||||
-- end
|
||||
-- function c:packMsg(data)
|
||||
-- local temp = bin.new()
|
||||
-- temp:addBlock(#data, self.numspace)
|
||||
-- temp:addBlock(data)
|
||||
-- return temp:getData()
|
||||
-- end
|
||||
-- function c:send(data)
|
||||
-- if self.autoNormalization then
|
||||
-- data = net.normalize(data)
|
||||
-- end
|
||||
-- if self.sMode == "*l" then
|
||||
-- ind, err = self.tcp:send(data .. "\n")
|
||||
-- elseif self.sMode == "b" then
|
||||
-- ind, err = self.tcp:send(self:packMsg(data))
|
||||
-- else
|
||||
-- ind, err = self.tcp:send(data)
|
||||
-- end
|
||||
-- if err == "closed" then
|
||||
-- self.OnClientDisconnected:Fire(self, err)
|
||||
-- elseif err == "timeout" then
|
||||
-- self.OnClientDisconnected:Fire(self, err)
|
||||
-- elseif err then
|
||||
-- --print(err)
|
||||
-- end
|
||||
-- end
|
||||
-- function c:sendRaw(data)
|
||||
-- if self.autoNormalization then
|
||||
-- data = net.normalize(data)
|
||||
-- end
|
||||
-- self.tcp:send(data)
|
||||
-- end
|
||||
-- function c:getCID()
|
||||
-- return "No Cid on a tcp client!"
|
||||
-- end
|
||||
-- function c:close()
|
||||
-- self.tcp:close()
|
||||
-- end
|
||||
-- function c:IDAssigned()
|
||||
-- return true
|
||||
-- end
|
||||
-- function c:update()
|
||||
-- if not self.tcp then
|
||||
-- return
|
||||
-- end
|
||||
-- local data, err, dat
|
||||
-- if self.rMode == "b" then
|
||||
-- thread.hold(
|
||||
-- function()
|
||||
-- dat = self.tcp:receive(self.numspace)
|
||||
-- return dat
|
||||
-- end
|
||||
-- )
|
||||
-- len = bin.new(dat):getBlock("n", self.numspace)
|
||||
-- data, err = self.tcp:receive(len)
|
||||
-- else
|
||||
-- data, err = self.tcp:receive()
|
||||
-- end
|
||||
-- if err == "closed" then
|
||||
-- self.OnClientDisconnected:Fire(self, err)
|
||||
-- elseif err == "timeout" then
|
||||
-- self.OnClientDisconnected:Fire(self, err)
|
||||
-- elseif err then
|
||||
-- --print(err)
|
||||
-- end
|
||||
-- if data then
|
||||
-- if self.autoNormalization then
|
||||
-- data = net.denormalize(data)
|
||||
-- end
|
||||
-- local hook = data:match("!(.-)!")
|
||||
-- self.OnDataRecieved:getConnection(hook):Fire(self, data)
|
||||
-- end
|
||||
-- end
|
||||
-- function c:reconnect()
|
||||
-- multi:newFunction(
|
||||
-- function(func)
|
||||
-- self.tcp = socket.connect(self.ip, self.port)
|
||||
-- if self.tcp == nil then
|
||||
-- --print("Can't connect to the server: No response from server!")
|
||||
-- multi:newAlarm(3):OnRing(
|
||||
-- function(alarm)
|
||||
-- self:reconnect()
|
||||
-- alarm:Destroy()
|
||||
-- return
|
||||
-- end
|
||||
-- ):setName("net.timeoutTask")
|
||||
-- end
|
||||
-- self.OnConnectionRegained:Fire(self)
|
||||
-- self.tcp:settimeout(0)
|
||||
-- --self.tcp:setoption('tcp-nodelay', true)
|
||||
-- self.tcp:setoption("keepalive", true)
|
||||
-- end
|
||||
-- )
|
||||
-- end
|
||||
-- c.event =
|
||||
-- multi:newEvent(
|
||||
-- function(event)
|
||||
-- return event.link:IDAssigned()
|
||||
-- end
|
||||
-- ):OnEvent(
|
||||
-- function(event)
|
||||
-- event.link.OnClientReady:Fire(event.link)
|
||||
-- event:Destroy()
|
||||
-- end
|
||||
-- )
|
||||
-- c.event:setName("net.handshakeTask")
|
||||
-- c.event.link = c
|
||||
-- c.OnClientReady = multi:newConnection()
|
||||
-- c.OnClientDisconnected = multi:newConnection()
|
||||
-- c.OnDataRecieved = multi:newConnection()
|
||||
-- c.OnConnectionRegained = multi:newConnection()
|
||||
-- table.insert(net.ConnectionDriver, c)
|
||||
-- net.OnClientCreated:Fire(c)
|
||||
-- return c
|
||||
-- end
|
||||
-- net.timer = multi:newTimer():Start()
|
||||
-- multi:newThread(
|
||||
-- "ClientServerHandler",
|
||||
-- function()
|
||||
-- while true do
|
||||
-- thread.skip()
|
||||
-- for i = 1, #net.ConnectionDriver do
|
||||
-- thread.skip()
|
||||
-- net.ConnectionDriver[i]:update()
|
||||
-- end
|
||||
-- if net.timer:Get() >= 1 then
|
||||
-- for i = 1, #net.BroadcastDriver do
|
||||
-- net.BroadcastDriver[i]()
|
||||
-- end
|
||||
-- net.timer:Reset()
|
||||
-- end
|
||||
-- end
|
||||
-- end
|
||||
-- )
|
||||
return net
|
||||
|
||||
@ -21,6 +21,7 @@ function server:init(type)
|
||||
self.localIP = net.getLocalIP()
|
||||
self.Type = type
|
||||
self.ips = {}
|
||||
self.links = {}
|
||||
self.cids = {}
|
||||
self.process = multi:newProcessor()
|
||||
self.process.Start()
|
||||
@ -44,25 +45,22 @@ function server:setSendMode(mode)
|
||||
self.sMode = mode
|
||||
end
|
||||
function server:broadcast(name)
|
||||
bCaster = bCaster + 1
|
||||
self.process:newThread("Broadcast Handler<"..bCaster..">",function()
|
||||
while true do
|
||||
thread.yield()
|
||||
self.broad:setoption("broadcast",true)
|
||||
self.broad:sendto(table.concat({name,self.Type,self.localIP},"|")..":"..self.port, "255.255.255.255", 11111)
|
||||
self.broad:setoption("broadcast", false)
|
||||
end
|
||||
end)
|
||||
end
|
||||
function server:send(data,cid)
|
||||
local dat = {data = data, cid = cid}
|
||||
if self.Type == "udp" then
|
||||
self.OnPreSend:Fire(dat)
|
||||
self.udp:sendto(dat.data,dat.cid.ip,dat.cid.port)
|
||||
elseif self.Type == "tcp" then
|
||||
--
|
||||
if not self.isBroadcasting then
|
||||
bCaster = bCaster + 1
|
||||
self.isBroadcasting = true
|
||||
self.process:newThread("Broadcast Handler<"..bCaster..">",function()
|
||||
while true do
|
||||
thread.yield()
|
||||
self.broad:setoption("broadcast",true)
|
||||
self.broad:sendto(table.concat({name,self.Type,self.localIP},"|")..":"..self.port, "255.255.255.255", 11111)
|
||||
self.broad:setoption("broadcast", false)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
function server:send(cid,data)
|
||||
-- Override this
|
||||
end
|
||||
function server:getCid(ip,port)
|
||||
if self.cids[ip .. port] then
|
||||
return self.cids[ip .. port]
|
||||
|
||||
102
net/tcp/init.lua
102
net/tcp/init.lua
@ -3,7 +3,81 @@ local clientbase = require("net.clientbase")
|
||||
local serverbase = require("net.serverbase")
|
||||
local multi, thread = require("multi"):init()
|
||||
local GLOBAL, THREAD = require("multi.integration.threading"):init()
|
||||
|
||||
local tcpcount = 0
|
||||
function net:newTCPServer(port)
|
||||
local c = {}
|
||||
setmetatable(c,serverbase)
|
||||
c:init("tcp")
|
||||
c.tcp = assert(socket.bind("*", port or 0))
|
||||
c.tcp:settimeout(0)
|
||||
c.ip, c.port = c.tcp:getsockname()
|
||||
if port and port == 0 then
|
||||
_, c.port = c.tcp:getsockname()
|
||||
end
|
||||
function c:send(cid,data)
|
||||
local dat = {data = data, cid = cid}
|
||||
self.OnPreSend:Fire(dat)
|
||||
if self.sMode == "*l" then
|
||||
cid:send(data .. "\n")
|
||||
else
|
||||
cid:send(data)
|
||||
end
|
||||
end
|
||||
tcpcount = tcpcount + 1
|
||||
c.updateThread = c.process:newThread("TCPServer Thread<"..tcpcount..">",function()
|
||||
while true do
|
||||
thread.skip(c.updaterRate)
|
||||
local client = c.tcp:accept(c.rMode)
|
||||
if client then
|
||||
print("Got Client!")
|
||||
table.insert(c.ips, client)
|
||||
client:settimeout(0)
|
||||
client:setoption("keepalive", true)
|
||||
ip, port = client:getpeername()
|
||||
if ip and port then
|
||||
c.OnClientConnected:Fire(c, client, ip, port)
|
||||
multi:newThread("ServerClientHandler",function()
|
||||
local cli = client
|
||||
while true do
|
||||
thread.yield()
|
||||
local data, err, dat, len
|
||||
data, err = thread.hold(function()
|
||||
data, err = cli:receive(c.rMode)
|
||||
if data then print(data) end
|
||||
if data~=nil and err then
|
||||
print(err)
|
||||
return multi.NIL, err
|
||||
end
|
||||
return data
|
||||
end)
|
||||
if err == "closed" then
|
||||
for i = 1, #c.ips do
|
||||
if c.ips[i] == cli then
|
||||
table.remove(c.ips, i)
|
||||
end
|
||||
end
|
||||
c.OnClientClosed:Fire(c, "Client Closed Connection!", cli, ip)
|
||||
c.links[cli] = nil -- lets clean up
|
||||
thread.kill()
|
||||
end
|
||||
if data then
|
||||
if net.inList(c.bannedIPs, ip) then
|
||||
return
|
||||
end
|
||||
c.OnDataRecieved:Fire(c, data, cli, ip, port)
|
||||
end
|
||||
end
|
||||
end).OnError(function(...)
|
||||
print(...)
|
||||
end)
|
||||
end
|
||||
end
|
||||
end
|
||||
end).OnError(function(...)
|
||||
print(...)
|
||||
end)
|
||||
return c
|
||||
end
|
||||
|
||||
function net:newTCPClient(host, port)
|
||||
local c = {}
|
||||
@ -13,7 +87,20 @@ function net:newTCPClient(host, port)
|
||||
c.tcp = socket.connect(c.ip,port)
|
||||
c.tcp:settimeout(0)
|
||||
c.tcp:setoption("keepalive",true)
|
||||
c.updateThread = c.process:newThread("TCPServer Thread<"..udpcount..">",function()
|
||||
function c:send(data)
|
||||
print("Sending:",data)
|
||||
local dat = {data = data}
|
||||
self.OnPreSend:Fire(dat)
|
||||
local ind, err = self.tcp:send(dat.data)
|
||||
print(ind,err)
|
||||
print("Data Sent!")
|
||||
if err == "closed" then
|
||||
self.OnClientDisconnected:Fire(self,err)
|
||||
elseif err == "timeout" then
|
||||
self.OnClientDisconnected:Fire(self,err)
|
||||
end
|
||||
end
|
||||
c.updateThread = c.process:newThread("TCPServer Thread<"..tcpcount..">",function()
|
||||
while true do
|
||||
thread.skip(c.updaterRate)
|
||||
local data = thread.hold(function()
|
||||
@ -29,15 +116,18 @@ function net:newTCPClient(host, port)
|
||||
else
|
||||
return d
|
||||
end
|
||||
end)
|
||||
end).OnError(function(...)
|
||||
print(...)
|
||||
end)
|
||||
if data then
|
||||
local dat = {data = data}
|
||||
c.OnPreRecieved:Fire(dat)
|
||||
c.OnDataRecieved:Fire(c,dat.data)
|
||||
end
|
||||
end
|
||||
end).OnError(function(a,b,c)
|
||||
print(a,b,c)
|
||||
end).OnError(function(...)
|
||||
print(...)
|
||||
end)
|
||||
return c
|
||||
end
|
||||
end
|
||||
return net
|
||||
@ -15,8 +15,6 @@ function net:newUDPServer(port)
|
||||
c.udp = assert(socket.udp())
|
||||
c.udp:settimeout(0)
|
||||
c.udp:setsockname("*",port)
|
||||
c.bannedIPs = {}
|
||||
c.bannedCIDs = {}
|
||||
local inactivity = {}
|
||||
if port == 0 then
|
||||
_,c.port = c.udp:getsockname()
|
||||
@ -24,6 +22,11 @@ function net:newUDPServer(port)
|
||||
c.port = port
|
||||
end
|
||||
udpcount = udpcount + 1
|
||||
function c:send(cid,data)
|
||||
local dat = {data = data, cid = cid}
|
||||
self.OnPreSend:Fire(dat)
|
||||
self.udp:sendto(dat.data,dat.cid.ip,dat.cid.port)
|
||||
end
|
||||
c.updateThread = c.process:newThread("UDPServer Thread<"..udpcount..">",function()
|
||||
local sideJob = thread:newFunction(function()
|
||||
thread.sleep(60*c.idleRate)
|
||||
@ -55,12 +58,13 @@ function net:newUDPServer(port)
|
||||
cd.activity = os.clock()
|
||||
c.cids[ip .. port] = cd
|
||||
cid = cd
|
||||
c.OnClientConnected:Fire(c, cd, ip, port)
|
||||
end
|
||||
print("Refreshing CID: ",cid," Activity!")
|
||||
cid.activity = os.clock()
|
||||
local dat = {data = data,cid = cid}
|
||||
c.OnPreRecieved:Fire(dat)
|
||||
c.OnDataRecieved:Fire(c,dat.data,dat.cid)
|
||||
c.OnDataRecieved:Fire(c,dat.data,dat.cid,cid.ip,cid.port)
|
||||
end
|
||||
end
|
||||
end).OnError(function(...)
|
||||
@ -76,6 +80,11 @@ function net:newUDPClient(host, port)
|
||||
c.udp = assert(socket.udp())
|
||||
c.udp:settimeout(0)
|
||||
c.udp:setpeername(c.ip,port)
|
||||
function c:send(data)
|
||||
local dat = {data = data}
|
||||
self.OnPreSend:Fire(dat)
|
||||
self.udp:send(dat.data)
|
||||
end
|
||||
c.updateThread = c.process:newThread("UDPServer Thread<"..udpcount..">",function()
|
||||
while true do
|
||||
thread.skip(c.updaterRate)
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
package.path = "./?/init.lua;./?.lua;"..package.path
|
||||
local net = require("net.udp")
|
||||
local server = net:newUDPServer(12345)
|
||||
local net = require("net.tcp")
|
||||
local server = net:newTCPServer(12345)
|
||||
|
||||
server.OnDataRecieved(function(serv, data,cid)
|
||||
print("Response: ",data)
|
||||
server:send("Hello!",cid)
|
||||
server:send(cid,"Hello!")
|
||||
end)
|
||||
multi:mainloop()
|
||||
Loading…
x
Reference in New Issue
Block a user