diff --git a/client.lua b/client.lua index abe5169..405e5bd 100644 --- a/client.lua +++ b/client.lua @@ -1,7 +1,7 @@ package.path = "./?/init.lua;./?.lua;"..package.path local net = require("lnet.tcp") local multi, thread = require("multi"):init() -local client = net:newCastedClient("Test")--net:newTCPClient("localhost",12345) +local client = net.newCastedClient("Test")--net:newTCPClient("localhost",12345) client:send("Test!") diff --git a/lnet/core/clientbase.lua b/lnet/base/client.lua similarity index 98% rename from lnet/core/clientbase.lua rename to lnet/base/client.lua index 9cb7532..4ac8dda 100644 --- a/lnet/core/clientbase.lua +++ b/lnet/base/client.lua @@ -5,6 +5,7 @@ client.updaterRate = 1 client.sMode = "*l" client.rMode = "*l" function client:init(type) + self.Type = type self.OnDataRecieved = multi:newConnection() self.OnServerNotAvailable = multi:newConnection() self.OnClientReady = multi:newConnection() @@ -13,7 +14,6 @@ function client:init(type) self.OnPreSend = multi:newConnection() self.OnPreRecieved = multi:newConnection() self.OnError = multi:newConnection() - self.Type = type self.process = multi:newProcessor() self.process.Start() end diff --git a/lnet/core/serverbase.lua b/lnet/base/server.lua similarity index 91% rename from lnet/core/serverbase.lua rename to lnet/base/server.lua index aea8f7e..446dc13 100644 --- a/lnet/core/serverbase.lua +++ b/lnet/base/server.lua @@ -8,6 +8,7 @@ server.updaterRate = 1 server.rMode = "*l" server.sMode = "*l" function server:init(type) + self.Type = type self.OnClientsModulesList = multi:newConnection() self.OnPreRecieved = multi:newConnection() self.OnDataRecieved = multi:newConnection() @@ -15,11 +16,11 @@ function server:init(type) self.OnClientConnected = multi:newConnection() self.OnPreSend = multi:newConnection() self.idleRate = 5 + self.clientHandlers = {} self.bannedCIDs = {} self.bannedIPs = {} self.broad = socket.udp() self.localIP = net.getLocalIP() - self.Type = type self.ips = {} self.links = {} self.cids = {} @@ -49,11 +50,12 @@ function server:broadcast(name) bCaster = bCaster + 1 self.isBroadcasting = true self.process:newThread("Broadcast Handler<"..bCaster..">",function() - print(table.concat({name,self.Type,self.localIP},"|")..":"..self.port) 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) + -- Send to localhost as well + self.broad:sendto(table.concat({name,self.Type,self.localIP},"|")..":"..self.port, self.localIP, 11111) self.broad:setoption("broadcast", false) end end) diff --git a/lnet/init.lua b/lnet/init.lua index ae7ef52..cf45e29 100644 --- a/lnet/init.lua +++ b/lnet/init.lua @@ -38,7 +38,7 @@ end for i = 97, 122 do char[#char + 1] = string.char(i) end -local isHyphen = {[9] = 1, [14] = 1, [19] = 1, [24] = 1} + math.random() math.random() math.random() @@ -48,8 +48,8 @@ local http = require("socket.http") --ssl=require("ssl") --https=require("ssl.https") local net = {} -net.Version = {4, 0, 0} -- This will probably stay this version for quite a while... The modules on the otherhand will change more often -net._VERSION = "4.0.0" +net.Version = {5, 0, 0} -- This will probably stay this version for quite a while... The modules on the otherhand will change more often +net._VERSION = "5.0.0" net.ClientCache = {} net.OnServerCreated = multi:newConnection() net.OnClientCreated = multi:newConnection() @@ -59,6 +59,7 @@ net.autoInit = true net.ConnectionDriver = {} net.BroadcastDriver = {} math.randomseed(math.ceil(os.time()+(os.clock()*1000))) +local isHyphen = {[9] = 1, [14] = 1, [19] = 1, [24] = 1} net.generateGUID = function(t) local pass = {} local a = 0 @@ -102,23 +103,20 @@ function net.getExternalIP() local data = http.request("http://www.myipnumber.com/my-ip-address.asp") return data:match("(%d+%.%d+%.%d+%.%d+)") end -function net:registerModule(mod, version) +function net.registerModule(mod, version) if net[mod] then - error( - "Module by the name: " .. - mod .. " has already been registered! Remember some modules are internal and use certain names!" - ) + error("Module by the name: " .. mod .. " has already been registered! Remember some modules are internal and use certain names!") end - table.insert(self.loadedModules, mod) + table.insert(net.loadedModules, mod) net[mod] = {} if version then net[mod].Version = version - net[mod]._VERSION = version[1] .. "." .. version[2] .. "." .. version[3] + net[mod]._VERSION = table.concat(version,".") else net[mod].Version = {1, 0, 0} net[mod]._VERSION = {1, 0, 0} end - return {Version = version, _VERSION = version[1] .. "." .. version[2] .. "." .. version[3]} + return {Version = version, _VERSION = table.concat(version,".")} end function net.getModuleVersion(ext) if not ext then @@ -145,33 +143,31 @@ end function net.setTrigger(funcW, funcE) multi:newTrigger(func) end -net:registerModule("net", net.Version) +net.registerModule("net", net.Version) -- Client broadcast -function net:newCastedClient(name) -- connects to the broadcasted server +function net.newCastedClient(name) -- connects to the broadcasted server local listen = socket.udp() -- make a new socket listen:setsockname(net.getLocalIP(), 11111) listen:settimeout(0) local timer = multi:newTimer() while true do local data, ip, port = listen:receivefrom() - -- if timer:Get() > 3 then - -- error("Timeout! Server by the name: " .. name .. " has not been found!") - -- end + if timer:Get() > 3 then + error("Timeout! Server by the name: " .. name .. " has not been found!") + end if data then - print("Found:", data) local n, tp, ip, port = data:match("(%S-)|(%S-)|(%S-):(%d+)") if n:match(name) then - --print("Found Server!", n, tp, ip, port) if tp == "tcp" then - return net:newTCPClient(ip, tonumber(port)) + return net.newTCPClient(ip, tonumber(port)) else - return net:newClient(ip, tonumber(port)) + return net.newClient(ip, tonumber(port)) end end end end end -function net:newCastedClients(name) -- connects to the broadcasted server +function net.newCastedClients(name) -- connects to the broadcasted server local listen = socket.udp() -- make a new socket listen:setsockname(net.getLocalIP(), 11111) listen:settimeout(0) diff --git a/lnet/tcp/init.lua b/lnet/tcp/init.lua index 0ac691c..8c9cdab 100644 --- a/lnet/tcp/init.lua +++ b/lnet/tcp/init.lua @@ -1,9 +1,10 @@ local net = require("lnet") -local clientbase = require("net.core.clientbase") -local serverbase = require("net.core.serverbase") +local clientbase = require("lnet.base.client") +local serverbase = require("lnet.base.server") local multi, thread = require("multi"):init() +local socket = require("socket") local tcpcount = 0 -function net:newTCPServer(port) +net.newTCPServer = thread:newFunction(function(port) local c = {} setmetatable(c,serverbase) c:init("tcp") @@ -35,7 +36,8 @@ function net:newTCPServer(port) ip, port = client:getpeername() if ip and port then c.OnClientConnected:Fire(c, client, ip, port) - multi:newThread("ServerClientHandler",function() + -- We need to cache the client handler so we can work with it + c.clientHandlers[client] = multi:newThread("ServerClientHandler<".. tostring(client):match(": (.+)") ..">",function() local cli = client while true do thread.yield() @@ -66,8 +68,6 @@ function net:newTCPServer(port) c.OnDataRecieved:Fire(c, data, cli, ip, port) end end - end).OnError(function(...) - print(...) end) end end @@ -75,10 +75,11 @@ function net:newTCPServer(port) end).OnError(function(...) print(...) end) + net.OnServerCreated:Fire(c) return c -end +end,true) -function net:newTCPClient(host, port) +net.newTCPClient = thread:newFunction(function(host, port) local c = {} setmetatable(c,clientbase) c:init("tcp") @@ -128,6 +129,7 @@ function net:newTCPClient(host, port) end).OnError(function(...) print(...) end) + net.OnClientCreated:Fire(c) return c -end +end,true) return net \ No newline at end of file diff --git a/lnet/udp/init.lua b/lnet/udp/init.lua index 817dbbc..d8fdc28 100644 --- a/lnet/udp/init.lua +++ b/lnet/udp/init.lua @@ -1,13 +1,13 @@ local net = require("lnet") -local clientbase = require("net.core.clientbase") -local serverbase = require("net.core.serverbase") +local clientbase = require("lnet.base.client") +local serverbase = require("lnet.base.server") local multi, thread = require("multi"):init() local CID = {} CID.__index = cid local udpcount = 0 CID.ip = "0.0.0.0" CID.port = 0 -function net:newUDPServer(port) +net.newUDPServer = thread:newFunction(function(port) local c = {} setmetatable(c,serverbase) c:init("udp") @@ -27,6 +27,7 @@ function net:newUDPServer(port) self.udp:sendto(dat.data,dat.cid.ip,dat.cid.port) end c.updateThread = c.process:newThread("UDPServer Thread<"..udpcount..">",function() + -- Every now and then we are going to check to see if a client has been inactive local sideJob = thread:newFunction(function() thread.sleep(60*c.idleRate) for i,v in pairs(c.cids) do @@ -41,7 +42,7 @@ function net:newUDPServer(port) while true do thread.skip(c.updaterRate) local data, ip, port = c.udp:receivefrom() - sideJob().connect(function(yes,a,b,c) + sideJob().connect(function(yes) if yes then sideJob:Resume() end @@ -59,7 +60,6 @@ function net:newUDPServer(port) 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) @@ -69,9 +69,10 @@ function net:newUDPServer(port) end).OnError(function(...) print(...) end) + net.OnServerCreated:Fire(c) return c -end -function net:newUDPClient(host, port) +end,true) +net.newUDPClient = thread:newFunction(function(host, port) local c = {} setmetatable(c,clientbase) c:init("udp") @@ -95,6 +96,7 @@ function net:newUDPClient(host, port) c.OnDataRecieved:Fire(c,dat.data) end end) + net.OnClientCreated:Fire(c) return c -end +end,true) return net \ No newline at end of file diff --git a/netold/version.lua b/netold/version.lua index 258bb4b..c4a3aad 100644 --- a/netold/version.lua +++ b/netold/version.lua @@ -18,7 +18,7 @@ net.OnServerCreated:connect(function(s) local cmd,arg1,arg2=data:match("!version! ") end,"version") s.OnClientConnected(function(self,CID_OR_HANDLE,IP_OR_HANDLE,PORT_OR_IP) - multi:newFunction(function(func) -- anom func, allows for fancy multitasking + multi:newFunction(function(func) -- anon func, allows for fancy multitasking multi:newFunction(function(self) local range=self:newRange() for i in range(1,#self.loadedModules) do diff --git a/rockspec/lnet-5.0-0.rockspec b/rockspec/lnet-5.0-0.rockspec index d134395..07a1a5b 100644 --- a/rockspec/lnet-5.0-0.rockspec +++ b/rockspec/lnet-5.0-0.rockspec @@ -15,6 +15,7 @@ description = { dependencies = { "lua >= 5.1", "luasocket", + "luasec", "multi", } build = { @@ -23,8 +24,8 @@ build = { ["lnet.init"] = "lnet/init.lua", ["lnet.tcp.init"] = "lnet/tcp/init.lua", ["lnet.udp.init"] = "lnet/udp/init.lua", - ["lnet.core.clientbase"] = "lnet/core/clientbase.lua", - ["lnet.core.serverbase"] = "lnet/core/serverbase.lua", + ["lnet.base.client"] = "lnet/base/client.lua", + ["lnet.base.server"] = "lnet/base/server.lua", ["lnet.http"] = "lnet/http.lua", ["lnet.https"] = "lnet/https.lua" } diff --git a/server.lua b/server.lua index 2b8e834..ee0a568 100644 --- a/server.lua +++ b/server.lua @@ -1,7 +1,7 @@ package.path = "./?/init.lua;./?.lua;"..package.path local net = require("lnet.tcp") local multi, thread = require("multi"):init() -local server = net:newTCPServer(12345) +server = net.newTCPServer(12345) server:broadcast("Test") print("Server has been broadcasted!") server.OnDataRecieved(function(serv, data,cid)