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