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()
end
function client:send(data)
local dat = {data = data}
if self.Type == "udp" then
local dat = {data = data}
self.OnPreSend:Fire(dat)
self.udp:send(dat.data)
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
self.OnClientDisconnected:Fire(self,err)
elseif err == "timeout" then

View File

@ -504,101 +504,101 @@ end
-- return c
-- end
--TCP Stuff
function net:newTCPClientObject(fd)
local c = {}
local client
c.Type = "tcp-ClientObj"
c.rMode = "*l"
c.sMode = "*l"
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
if fd then
client = socket.tcp()
client:setfd(fd)
_, port = client:getsockname()
c.handle = client
else
error("You need to enter a fd in order to be able to create a tcp client object like this!")
end
function c:setUpdateRate(n)
self.updaterRate = n
end
function c:setReceiveMode(mode)
self.rMode = mode
end
function c:setSendMode(mode)
self.rMode = mode
end
function c:send(data)
if self.autoNormalization then
data = net.normalize(data)
end
if self.sMode == "*l" then
self.handle:send(data .. "\n")
elseif self.sMode == "b" then
self.handle:send(self:packMsg(data))
else
self.handle:send(data)
end
end
multi:newThread("ServerClientHandler",function()
while true do
thread.skip(1)
local data, err, dat, len
if self.rMode == "b" then
thread.hold(function()
return client:receive(self.numspace)
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()
end
if data then
if self.autoNormalization then
data = net.denormalize(data)
end
if net.inList(self.bannedIPs, ip) then
--print("We will ingore data from a banned client!")
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)
c.OnClientsModulesList = multi:newConnection()
c.OnDataRecieved = multi:newConnection()
c.OnClientClosed = multi:newConnection()
c.OnClientConnected = multi:newConnection()
return c
end
-- function net:newTCPClientObject(fd)
-- local c = {}
-- local client
-- c.Type = "tcp-ClientObj"
-- c.rMode = "*l"
-- c.sMode = "*l"
-- 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
-- if fd then
-- client = socket.tcp()
-- client:setfd(fd)
-- _, port = client:getsockname()
-- c.handle = client
-- else
-- error("You need to enter a fd in order to be able to create a tcp client object like this!")
-- end
-- function c:setUpdateRate(n)
-- self.updaterRate = n
-- end
-- function c:setReceiveMode(mode)
-- self.rMode = mode
-- end
-- function c:setSendMode(mode)
-- self.rMode = mode
-- end
-- function c:send(data)
-- if self.autoNormalization then
-- data = net.normalize(data)
-- end
-- if self.sMode == "*l" then
-- self.handle:send(data .. "\n")
-- elseif self.sMode == "b" then
-- self.handle:send(self:packMsg(data))
-- else
-- self.handle:send(data)
-- end
-- end
-- multi:newThread("ServerClientHandler",function()
-- while true do
-- thread.skip(1)
-- local data, err, dat, len
-- if self.rMode == "b" then
-- thread.hold(function()
-- return client:receive(self.numspace)
-- 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()
-- end
-- if data then
-- if self.autoNormalization then
-- data = net.denormalize(data)
-- end
-- if net.inList(self.bannedIPs, ip) then
-- --print("We will ingore data from a banned client!")
-- 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)
-- c.OnClientsModulesList = multi:newConnection()
-- c.OnDataRecieved = multi:newConnection()
-- c.OnClientClosed = multi:newConnection()
-- c.OnClientConnected = multi:newConnection()
-- return c
-- end
function net:newTCPServer(port)
local c = {}
local port = port or 0

View File

@ -55,8 +55,10 @@ function server:broadcast(name)
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

View File

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

View File

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