Working on tcp

This commit is contained in:
Ryan Ward 2021-07-06 22:17:09 -04:00
parent 7633e87ab9
commit 8f8fd31e64
5 changed files with 121 additions and 112 deletions

View File

@ -17,12 +17,14 @@ function client:init(type)
self.process.Start() self.process.Start()
end end
function client:send(data) function client:send(data)
local dat = {data = data}
if self.Type == "udp" then if self.Type == "udp" then
local dat = {data = data}
self.OnPreSend:Fire(dat) self.OnPreSend:Fire(dat)
self.udp:send(dat.data) self.udp:send(dat.data)
elseif self.Type == "tcp" then elseif self.Type == "tcp" then
local ind, err = self.tcp:send(data) self.OnPreSend:Fire(dat)
self.udp:send(dat.data)
local ind, err = self.tcp:send(dat.data)
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

View File

@ -504,101 +504,101 @@ end
-- return c -- return c
-- end -- end
--TCP Stuff --TCP Stuff
function net:newTCPClientObject(fd) -- function net:newTCPClientObject(fd)
local c = {} -- local c = {}
local client -- local client
c.Type = "tcp-ClientObj" -- c.Type = "tcp-ClientObj"
c.rMode = "*l" -- c.rMode = "*l"
c.sMode = "*l" -- c.sMode = "*l"
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
if fd then -- if fd then
client = socket.tcp() -- client = socket.tcp()
client:setfd(fd) -- client:setfd(fd)
_, port = client:getsockname() -- _, port = client:getsockname()
c.handle = client -- c.handle = client
else -- else
error("You need to enter a fd in order to be able to create a tcp client object like this!") -- error("You need to enter a fd in order to be able to create a tcp client object like this!")
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.rMode = mode -- self.rMode = mode
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
self.handle:send(data .. "\n") -- self.handle:send(data .. "\n")
elseif self.sMode == "b" then -- elseif self.sMode == "b" then
self.handle:send(self:packMsg(data)) -- self.handle:send(self:packMsg(data))
else -- else
self.handle:send(data) -- self.handle:send(data)
end -- end
end -- end
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(function() -- thread.hold(function()
return client:receive(self.numspace) -- return client:receive(self.numspace)
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()
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
--print("We will ingore data from a banned client!") -- --print("We will ingore data from a banned client!")
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)
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()
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

View File

@ -55,8 +55,10 @@ function server:broadcast(name)
end) end)
end end
function server:send(data,cid) function server:send(data,cid)
local dat = {data = data, cid = cid}
if self.Type == "udp" then 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 elseif self.Type == "tcp" then
-- --
end end

View File

@ -17,11 +17,24 @@ function net:newTCPClient(host, port)
while true do while true do
thread.skip(c.updaterRate) thread.skip(c.updaterRate)
local data = thread.hold(function() local data = thread.hold(function()
return c.udp:receive() local d,err = c.tcp:receive(c.rMode)
end) if not(d) then
local dat = {data = data} if err == "closed" then
c.OnPreSend:Fire(dat) c.OnClientDisconnected:Fire(c,err)
c.OnDataRecieved:Fire(c,dat.data) elseif err == "timeout" then
c.OnClientDisconnected:Fire(c,err)
else
print(err)
end
else
return d
end
end)
if data then
local dat = {data = data}
c.OnPreRecieved:Fire(dat)
c.OnDataRecieved:Fire(c,dat.data)
end
end end
end).OnError(function(a,b,c) end).OnError(function(a,b,c)
print(a,b,c) print(a,b,c)

View File

@ -23,11 +23,6 @@ function net:newUDPServer(port)
else else
c.port = port c.port = port
end end
function c:send(data,cid)
local dat = {data = data, cid = cid}
self.OnPreSend:Fire(dat)
self.udp:sendto(dat.data,dat.cid.ip,dat.cid.port)
end
udpcount = udpcount + 1 udpcount = udpcount + 1
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()
@ -71,9 +66,6 @@ function net:newUDPServer(port)
end).OnError(function(...) end).OnError(function(...)
print(...) print(...)
end) end)
c.activityMonitor = c.process:newThread("Activity Monitor",function()
--
end)
return c return c
end end
function net:newUDPClient(host, port) function net:newUDPClient(host, port)
@ -91,7 +83,7 @@ function net:newUDPClient(host, port)
return c.udp:receive() return c.udp:receive()
end) end)
local dat = {data = data} local dat = {data = data}
c.OnPreSend:Fire(dat) c.OnPreRecieved:Fire(dat)
c.OnDataRecieved:Fire(c,dat.data) c.OnDataRecieved:Fire(c,dat.data)
end end
end) end)