Working on rewrite
This commit is contained in:
parent
56981f5b83
commit
7633e87ab9
25
client.lua
25
client.lua
@ -1,17 +1,12 @@
|
|||||||
package.path="?/init.lua;"..package.path
|
package.path = "./?/init.lua;./?.lua;"..package.path
|
||||||
local multi = require("multi")
|
local net = require("net.udp")
|
||||||
local net = require("net")
|
local client = net:newUDPClient("localhost",12345)
|
||||||
client = net:newTCPClient("localhost",12345)
|
|
||||||
client:enableBinaryMode()
|
client:send("Test!")
|
||||||
local file = bin.new()
|
|
||||||
client.OnDataRecieved(function(self,data)
|
client.OnDataRecieved(function(c,data)
|
||||||
if data == "END" then
|
print("Response: ",data)
|
||||||
file:tofile("test2.mp3")
|
--c:send("Testing again!")
|
||||||
print("File transfered!")
|
|
||||||
else
|
|
||||||
file:tackE(data)
|
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
client.OnClientReady:holdUT() -- waits until the client is ready... You can also connect to this event as well and have code do stuff too
|
|
||||||
client:send("Hello Server!")
|
|
||||||
multi:mainloop()
|
multi:mainloop()
|
||||||
45
net/clientbase.lua
Normal file
45
net/clientbase.lua
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
local multi, thread = require("multi"):init()
|
||||||
|
local client = {}
|
||||||
|
client.__index = client
|
||||||
|
client.OnDataRecieved = multi:newConnection()
|
||||||
|
client.OnServerNotAvailable = multi:newConnection()
|
||||||
|
client.OnClientReady = multi:newConnection()
|
||||||
|
client.OnClientDisconnected = multi:newConnection()
|
||||||
|
client.OnConnectionRegained = multi:newConnection()
|
||||||
|
client.OnPreSend = multi:newConnection()
|
||||||
|
client.OnPreRecieved = multi:newConnection()
|
||||||
|
client.updaterRate = 1
|
||||||
|
client.sMode = "*l"
|
||||||
|
client.rMode = "*l"
|
||||||
|
function client:init(type)
|
||||||
|
self.Type = type
|
||||||
|
self.process = multi:newProcessor()
|
||||||
|
self.process.Start()
|
||||||
|
end
|
||||||
|
function client:send(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)
|
||||||
|
if err == "closed" then
|
||||||
|
self.OnClientDisconnected:Fire(self,err)
|
||||||
|
elseif err == "timeout" then
|
||||||
|
self.OnClientDisconnected:Fire(self,err)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
function client:close()
|
||||||
|
--
|
||||||
|
end
|
||||||
|
function client:setUpdateRate(n)
|
||||||
|
self.updaterRate = n
|
||||||
|
end
|
||||||
|
function client:setReceiveMode(mode)
|
||||||
|
self.rMode = mode
|
||||||
|
end
|
||||||
|
function client:setSendMode(mode)
|
||||||
|
self.sMode = mode
|
||||||
|
end
|
||||||
|
return client
|
||||||
1190
net/init.lua
1190
net/init.lua
File diff suppressed because it is too large
Load Diff
84
net/serverbase.lua
Normal file
84
net/serverbase.lua
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
local multi, thread = require("multi"):init()
|
||||||
|
local socket = require("socket")
|
||||||
|
local net = require("net")
|
||||||
|
local server = {}
|
||||||
|
local bCaster = 0
|
||||||
|
server.__index = server
|
||||||
|
server.OnClientsModulesList = multi:newConnection()
|
||||||
|
server.OnPreRecieved = multi:newConnection()
|
||||||
|
server.OnDataRecieved = multi:newConnection()
|
||||||
|
server.OnClientClosed = multi:newConnection()
|
||||||
|
server.OnClientConnected = multi:newConnection()
|
||||||
|
server.OnPreSend = multi:newConnection()
|
||||||
|
server.updaterRate = 1
|
||||||
|
server.rMode = "*l"
|
||||||
|
server.sMode = "*l"
|
||||||
|
function server:init(type)
|
||||||
|
self.idleRate = 5
|
||||||
|
self.bannedCIDs = {}
|
||||||
|
self.bannedIPs = {}
|
||||||
|
self.broad = socket.udp()
|
||||||
|
self.localIP = net.getLocalIP()
|
||||||
|
self.Type = type
|
||||||
|
self.ips = {}
|
||||||
|
self.cids = {}
|
||||||
|
self.process = multi:newProcessor()
|
||||||
|
self.process.Start()
|
||||||
|
end
|
||||||
|
function server:setIdleRate(minutes)
|
||||||
|
self.idleRate = minutes
|
||||||
|
end
|
||||||
|
function server:setUpdateRate(n)
|
||||||
|
self.updaterRate = n
|
||||||
|
end
|
||||||
|
function server:banCID(cid)
|
||||||
|
table.insert(self.bannedCIDs,cid)
|
||||||
|
end
|
||||||
|
function server:banIP(ip)
|
||||||
|
table.insert(self.bannedIPs)
|
||||||
|
end
|
||||||
|
function server:setRecieveMode(mode)
|
||||||
|
self.rMode = mode
|
||||||
|
end
|
||||||
|
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)
|
||||||
|
if self.Type == "udp" then
|
||||||
|
---
|
||||||
|
elseif self.Type == "tcp" then
|
||||||
|
--
|
||||||
|
end
|
||||||
|
end
|
||||||
|
function server:getCid(ip,port)
|
||||||
|
if self.cids[ip .. port] then
|
||||||
|
return self.cids[ip .. port]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
function server:sendAll(data)
|
||||||
|
for i,v in pairs(self.cids) do
|
||||||
|
self:send(data,cid)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
function server:sendAllBut(data,cid)
|
||||||
|
for i,v in pairs(self.cids) do
|
||||||
|
if v~=cid then
|
||||||
|
self:send(data,cid)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
function server:clientRegistered(cid)
|
||||||
|
return self.cids[cid]
|
||||||
|
end
|
||||||
|
return server
|
||||||
30
net/tcp/init.lua
Normal file
30
net/tcp/init.lua
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
local net = require("net")
|
||||||
|
local clientbase = require("net.clientbase")
|
||||||
|
local serverbase = require("net.serverbase")
|
||||||
|
local multi, thread = require("multi"):init()
|
||||||
|
local GLOBAL, THREAD = require("multi.integration.threading"):init()
|
||||||
|
|
||||||
|
|
||||||
|
function net:newTCPClient(host, port)
|
||||||
|
local c = {}
|
||||||
|
setmetatable(c,clientbase)
|
||||||
|
c:init("tcp")
|
||||||
|
c.ip = assert(socket.dns.toip(host))
|
||||||
|
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()
|
||||||
|
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)
|
||||||
|
end
|
||||||
|
end).OnError(function(a,b,c)
|
||||||
|
print(a,b,c)
|
||||||
|
end)
|
||||||
|
return c
|
||||||
|
end
|
||||||
100
net/udp/init.lua
Normal file
100
net/udp/init.lua
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
local net = require("net")
|
||||||
|
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 CID = {}
|
||||||
|
CID.__index = cid
|
||||||
|
local udpcount = 0
|
||||||
|
CID.ip = "0.0.0.0"
|
||||||
|
CID.port = 0
|
||||||
|
function net:newUDPServer(port)
|
||||||
|
local c = {}
|
||||||
|
setmetatable(c,serverbase)
|
||||||
|
c:init("udp")
|
||||||
|
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()
|
||||||
|
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()
|
||||||
|
thread.sleep(60*c.idleRate)
|
||||||
|
for i,v in pairs(c.cids) do
|
||||||
|
thread.skip(1)
|
||||||
|
if os.clock() - v.activity >= 60*c.idleRate then
|
||||||
|
c.OnClientClosed:Fire(v)
|
||||||
|
c.cids[i] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end)
|
||||||
|
while true do
|
||||||
|
thread.skip(c.updaterRate)
|
||||||
|
local data, ip, port = c.udp:receivefrom()
|
||||||
|
sideJob().connect(function(yes,a,b,c)
|
||||||
|
if yes then
|
||||||
|
sideJob:Resume()
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
sideJob:Pause()
|
||||||
|
if data then
|
||||||
|
local cid = c:getCid(ip,port)
|
||||||
|
if not cid then
|
||||||
|
local cd = {}
|
||||||
|
setmetatable(cd,CID)
|
||||||
|
cd.ip = ip
|
||||||
|
cd.port = port
|
||||||
|
cd.activity = os.clock()
|
||||||
|
c.cids[ip .. port] = cd
|
||||||
|
cid = cd
|
||||||
|
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)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end).OnError(function(...)
|
||||||
|
print(...)
|
||||||
|
end)
|
||||||
|
c.activityMonitor = c.process:newThread("Activity Monitor",function()
|
||||||
|
--
|
||||||
|
end)
|
||||||
|
return c
|
||||||
|
end
|
||||||
|
function net:newUDPClient(host, port)
|
||||||
|
local c = {}
|
||||||
|
setmetatable(c,clientbase)
|
||||||
|
c:init("udp")
|
||||||
|
c.ip = assert(socket.dns.toip(host))
|
||||||
|
c.udp = assert(socket.udp())
|
||||||
|
c.udp:settimeout(0)
|
||||||
|
c.udp:setpeername(c.ip,port)
|
||||||
|
c.updateThread = c.process:newThread("UDPServer Thread<"..udpcount..">",function()
|
||||||
|
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)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
return c
|
||||||
|
end
|
||||||
|
return net
|
||||||
25
server.lua
25
server.lua
@ -1,20 +1,9 @@
|
|||||||
package.path="?/init.lua;"..package.path
|
package.path = "./?/init.lua;./?.lua;"..package.path
|
||||||
local multi = require("multi")
|
local net = require("net.udp")
|
||||||
local net = require("net")
|
local server = net:newUDPServer(12345)
|
||||||
local GLOBAL, THREAD = require("multi.integration.lanesManager").init()
|
|
||||||
server = net:newTCPServer(12345)
|
server.OnDataRecieved(function(serv, data,cid)
|
||||||
server:enableBinaryMode()
|
print("Response: ",data)
|
||||||
print("Server hosted on "..net.getExternalIP().." listening on port: 12345")
|
server:send("Hello!",cid)
|
||||||
server.OnDataRecieved(function(self,data,cid,ip,port)
|
|
||||||
print(data)
|
|
||||||
local file = bin.load("test.mp3")
|
|
||||||
local dat = file:read(1024)
|
|
||||||
while dat do
|
|
||||||
thread.sleep(.002)
|
|
||||||
self:send(ip,dat,port,cid)
|
|
||||||
dat = file:read(1024)
|
|
||||||
end
|
|
||||||
self:send(ip,dat or "",port,cid)
|
|
||||||
self:send(ip,"END",port,cid)
|
|
||||||
end)
|
end)
|
||||||
multi:mainloop()
|
multi:mainloop()
|
||||||
Loading…
x
Reference in New Issue
Block a user