From 4f40bc3dfc86387bbf3a58e06706dfb3c854f449 Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 27 Jun 2017 11:45:34 -0400 Subject: [PATCH] Fixed for new multi (2.0.1) Added Fix for multi 1.7.x Requires luasocket 3.0+ Added Examples --- README.md | 121 +++++++++++++++++++++++-- examples/basicClient.lua | 13 +++ examples/basicServer.lua | 11 +++ examples/broadcastingExampleClient.lua | 15 +++ examples/broadcastingExampleServer.lua | 15 +++ net/init.lua | 12 +-- 6 files changed, 173 insertions(+), 14 deletions(-) create mode 100644 examples/basicClient.lua create mode 100644 examples/basicServer.lua create mode 100644 examples/broadcastingExampleClient.lua create mode 100644 examples/broadcastingExampleServer.lua diff --git a/README.md b/README.md index b059c2d..2354c04 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,10 @@ -# net +# net (2.0.1) +Updated from 2.0.0 to 2.0.1 +Added: +- Examples +- Support for latest multi version +- Thats about it + # Discord For real-time assistance with my libraries! A place where you can ask questions and get help with any of my libraries
@@ -9,12 +15,25 @@ The net library was created to make servers and clients interact easily. This is This library depends on luasocket and you should use luajit! It will work on a standard lua interperter, but wont be as fast. # Goal A simple and powerful way to make servers and clients +# Todo +- [ ] Write the wiki +- [x] Make Example folder +- [ ] Document stable modules +- [ ] IPV6 Support +- [ ] Multicast support +- [ ] Clean up modules +- [ ] Improve 'stable' modules +- [ ] AUDP - advance udp. Ensures packets arrive and handles late packets. +- [ ] P2P - peer to peer (Server to set up initial connection) +- [ ] Relay - offput server load (locally) +- [ ] Threading - Simple threading ~~(UDP/AUDP Only)~~ Thanks to an updated multi library we can thread with ease +- [ ] Priority handling # Usage server.lua ```lua require("bin") -- this library needs a lot of work it has a bunch of old useless code, but also has many nice things as well that are really useful -require("multi.all") -- you need this to handle multiple connections and such +require("multi") -- you need this to handle multiple connections and such require("net") -- That requires the main library server=net:newTCPServer(12345) -- create a server that listens on port 12345 server.OnDataRecieved(function(self,data,CID_OR_HANDLE,IP_OR_HANDLE,PORT_OR_IP,UPDATER_OR_NIL) -- a bit confusing, but dont worry you will hardly ever need more then the first 5 arguments, unless you are writing modules! @@ -28,7 +47,7 @@ multi:mainloop() client.lua ```lua require("bin") -- this library needs a lot of work it has a bunch of old useless code, but also has many nice things as well that are really useful -require("multi.all") -- you need this to handle multiple connections and such +require("multi") -- you need this to handle multiple connections and such require("net") -- That requires the main library client=net:newTCPClient("localhost",12345) -- connect to the server client.OnClientReady(function(self) @@ -41,8 +60,94 @@ client.OnDataRecieved(function(self,data) -- thats it clients only have to worry end) multi:mainloop() ``` -The bin and multi library can be found on my github page. They are all 100% pure lua so it should be easy to add to your project. -# Todo -- Write the wiki -- put examples on main page -- + +There is support for broadcasting, multicasting will be added soon requires luasocker 3.0+.
+Here is a broadcasting example:
+broadcastingExampleServer.lua (included in example folders) +```lua +package.path="?/init.lua;"..package.path +-- Note: you need 2 computers to test this! Broadcast does not on local host! +-- I have tested this code and it works, but only on seperate PC's within the same LAN network +require("bin") +require("multi") +require("net") +server=net:newServer(12345) +server.OnDataRecieved(function(self,data,CID_OR_HANDLE,IP_OR_HANDLE,PORT_OR_IP,UPDATER_OR_NIL) + if data=="Hello!" then -- copy from other example + print("Got response from client sending back data!") + self:send(IP_OR_HANDLE,"Hello Client!",PORT_OR_IP) -- doing it like this makes this code work for both udp and tcp + end +end) +server:broadcast("Lua_Server") +multi:mainloop() +``` +broadcastingExampleClient.lua (included in example folders) +```lua +package.path="?/init.lua;"..package.path +require("bin") +require("multi") +require("net.aft") +client=net:newCastedClient("Lua_Server") -- searches the lan for this server name +-- Both udp and tcp clients can be broadcasted +client.OnClientReady(function(self) + self:send("Hello!") +end) -- For a tcp client the client is already ready, with udp a small handshake is done and the client is not instantly ready +client.OnDataRecieved(function(self,data) -- thats it clients only have to worry about itself and the server + if data=="Hello Client!" then + print("Server Responded Back!") + end +end) +multi:mainloop() +``` +The net library also provides a powerful module creation interface. You have all of the modules in the net folder as examples, however I will show you how you could go about creating your own!
+ +**All functions include:
** +- net.OnServerCreated() -- Refer to the multi library example on connections: https://github.com/rayaman/multi#connections +- net.OnClientCreated() -- ^^ +- net.normalize(input) -- formats data in base 64 +- net.denormalize(input) -- takes base 64 data and turns it back into what it was +- net.getLocalIP() -- returns your loacl ip +- net.getExternalIP() -- If this ever stops working let me know... Ill have to update the service I am using +- net:registerModule(mod,version) -- registers a module. Checks out the example below +- net.getModuleVersion(ext) -- returns the version of a module +- net.resolveID(obj) -- an internal method for generating unique IDs obj is a table with an ID key +- net.inList(list,dat) -- checks if dat is a value in list +- net.setTrigger(funcW,funcE) -- Currently does nothing... I forgot where I was going with this +- net:newCastedClient(name) -- connects to a server that is being broadcasted see above example +- net:newServer(port,servercode) -- creates a UDP Server +- net:newClient(host,port,servercode,nonluaServer) -- creates a UDP Client +- net:newTCPServer(port) -- creates a TCP Server +- net:newTCPClient(host,port) -- creates a TCP Client + +When using the module creation support here is the shell that you can use: +```lua +require("net") -- what do you need? other modules or the core? always require the core so users can require your module without having to require the core themself +local MODULENAME="EXAMPLE" +net:registerModule(MODULENAME,{1,0,0}) +if not io.dirExists(string.upper(MODULENAME)) then -- do you need a directory to store stuff for your module? + io.mkDir(string.upper(MODULENAME)) +end +net.OnServerCreated:connect(function(s) + s.OnDataRecieved(function(self,data,CID_OR_HANDLE,IP_OR_HANDLE,PORT_OR_IP) + local cmd,arg1,arg2=data:match("!"..MODULENAME.."! (%S+) (%S+) (%S+)") -- change to fit your needs + if cmd=="SEND" then + -- + elseif cmd=="GET" then + -- + end + end,MODULENAME) +end) +net.OnClientCreated:connect(function(c) + c.OnDataRecieved(function(self,data) + local cmd,arg1,arg2=data:match("!"..MODULENAME.."! (%S+) (%S+) (%S+)") -- change to fit your needs + if cmd=="SEND" then + -- + elseif cmd=="GET" then + -- + end + end,MODULENAME) +end) +``` + +The bin and multi library can be found on my github page. They are all '100%'(If you ingore intergrations) pure lua so it should be easy to add to your project. +https://github.com/rayaman/multi
https://github.com/rayaman/bin diff --git a/examples/basicClient.lua b/examples/basicClient.lua new file mode 100644 index 0000000..cfbbfbb --- /dev/null +++ b/examples/basicClient.lua @@ -0,0 +1,13 @@ +require("bin") -- this library needs a lot of work it has a bunch of old useless code, but also has many nice things as well that are really useful +require("multi") -- you need this to handle multiple connections and such +require("net") -- That requires the main library +client=net:newTCPClient("localhost",12345) -- connect to the server +client.OnClientReady(function(self) + self:send("Hello!") +end) -- For a tcp client the client is already ready, with udp a small handshake is done and the client is not instantly ready +client.OnDataRecieved(function(self,data) -- thats it clients only have to worry about itself and the server + if data=="Hello Client!" then + print("Server Responded Back!") + end +end) +multi:mainloop() diff --git a/examples/basicServer.lua b/examples/basicServer.lua new file mode 100644 index 0000000..33b208c --- /dev/null +++ b/examples/basicServer.lua @@ -0,0 +1,11 @@ +require("bin") -- this library needs a lot of work it has a bunch of old useless code, but also has many nice things as well that are really useful +require("multi") -- you need this to handle multiple connections and such +require("net") -- That requires the main library +server=net:newTCPServer(12345) -- create a server that listens on port 12345 +server.OnDataRecieved(function(self,data,CID_OR_HANDLE,IP_OR_HANDLE,PORT_OR_IP,UPDATER_OR_NIL) -- a bit confusing, but dont worry you will hardly ever need more then the first 5 arguments, unless you are writing modules! + if data=="Hello!" then + print("Got response from client sending back data!") + self:send(IP_OR_HANDLE,"Hello Client!",PORT_OR_IP) -- doing it like this makes this code work for both udp and tcp + end +end) +multi:mainloop() diff --git a/examples/broadcastingExampleClient.lua b/examples/broadcastingExampleClient.lua new file mode 100644 index 0000000..2c1be0e --- /dev/null +++ b/examples/broadcastingExampleClient.lua @@ -0,0 +1,15 @@ +package.path="?/init.lua;"..package.path +require("bin") +require("multi") +require("net.aft") +client=net:newCastedClient("Lua_Server") -- searches the lan for this server name +-- Both udp and tcp clients can be broadcasted +client.OnClientReady(function(self) + self:send("Hello!") +end) -- For a tcp client the client is already ready, with udp a small handshake is done and the client is not instantly ready +client.OnDataRecieved(function(self,data) -- thats it clients only have to worry about itself and the server + if data=="Hello Client!" then + print("Server Responded Back!") + end +end) +multi:mainloop() diff --git a/examples/broadcastingExampleServer.lua b/examples/broadcastingExampleServer.lua new file mode 100644 index 0000000..4a265e0 --- /dev/null +++ b/examples/broadcastingExampleServer.lua @@ -0,0 +1,15 @@ +package.path="?/init.lua;"..package.path +-- Note: you need 2 computers to test this! Broadcast does not on local host! +-- I have tested this code and it works, but only on seperate PC's within the same LAN network +require("bin") +require("multi") +require("net") +server=net:newServer(12345) +server.OnDataRecieved(function(self,data,CID_OR_HANDLE,IP_OR_HANDLE,PORT_OR_IP,UPDATER_OR_NIL) + if data=="Hello!" then -- copy from other example + print("Got response from client sending back data!") + self:send(IP_OR_HANDLE,"Hello Client!",PORT_OR_IP) -- doing it like this makes this code work for both udp and tcp + end +end) +server:broadcast("Lua_Server") +multi:mainloop() diff --git a/net/init.lua b/net/init.lua index 21f3db9..05ed88d 100644 --- a/net/init.lua +++ b/net/init.lua @@ -1,9 +1,9 @@ --[[ UPCOMMING ADDITIONS - AUDP - advance udp/ Ensures packets arrive and handles late packets. + AUDP - advance udp. Ensures packets arrive and handles late packets. P2P - peer to peer (Server to set up initial connection) Relay - offput server load (locally) - Threading - Simple threading (UDP/AUDP Only) + Threading - Simple threading ~~(UDP/AUDP Only)~~ Thanks to an updated multi library we can thread with ease Priority handling ]] --[[ @@ -31,8 +31,8 @@ socket=require("socket") http=require("socket.http") mime=require("mime") net={} -net.Version={2,0,0} -- This will probably stay this version for quite a while... The modules on the otherhand will be more inconsistant -net._VERSION="2.0.0" +net.Version={2,0,1} -- This will probably stay this version for quite a while... The modules on the otherhand will be more inconsistant +net._VERSION="2.0.1" net.OnServerCreated=multi:newConnection() net.OnClientCreated=multi:newConnection() net.loadedModules={} @@ -149,7 +149,7 @@ function net:newServer(port,servercode) c.broad=socket.udp() c.hostip=net.getLocalIP() function c:broadcast(name) - local loop=multi:newTLoop(function(dt,loop) + local loop=multi:newTLoop(function(loop,dt) self.broad:setoption('broadcast',true) self.broad:sendto(name.."|"..self.Type.."|"..self.hostip..":"..self.port, "255.255.255.255", 11111) self.broad:setoption('broadcast',false) @@ -428,7 +428,7 @@ function net:newTCPServer(port) c.broad=socket.udp() c.hostip=net.getLocalIP() function c:broadcast(name) - local loop=multi:newTLoop(function(dt,loop) + local loop=multi:newTLoop(function(loop,dt) self.broad:setoption('broadcast',true) self.broad:sendto(name.."|"..self.Type.."|"..self.hostip..":"..self.port, "255.255.255.255", 11111) self.broad:setoption('broadcast',false)