From 14f186d8e1f36b38f6816dfa4d004dd023b882ce Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 9 Jul 2017 14:33:16 -0400 Subject: [PATCH] Readme change and file cleanup --- README.html | 184 +++++++++++++++++++++++++++++++++++++++++++ README.md | 2 + net/emailWIP.lua | 53 ------------- net/loggingWIP.lua | 7 -- net/p2pWIP.lua | 31 -------- net/settingsWIP.lua | 48 ----------- net/threadingWIP.lua | 37 --------- 7 files changed, 186 insertions(+), 176 deletions(-) create mode 100644 README.html delete mode 100644 net/emailWIP.lua delete mode 100644 net/loggingWIP.lua delete mode 100644 net/p2pWIP.lua delete mode 100644 net/settingsWIP.lua delete mode 100644 net/threadingWIP.lua diff --git a/README.html b/README.html new file mode 100644 index 0000000..bcd1469 --- /dev/null +++ b/README.html @@ -0,0 +1,184 @@ + + + + + README.html + + + + + + +

net (2.0.1)

Updated from 2.0.0 to 2.0.1
Added:

Discord

For real-time assistance with my libraries! A place where you can ask questions and get help with any of my libraries

https://discord.gg/U8UspuA

The net library was created to make servers and clients interact easily. This isn’t for webservers! (It could be, but you would need to code that part) The goal was to allow the creation for game servers!

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

Note

You will see a bunch of files inside of the net folder. All that is stable is the init.lua and sft.lua file. Everything else is a work in progress. Plus I am planning on rewritting all of the modules to take advantage of the new threading features that are found in the new multi updates. PRogress on this will be made soon. I have just been away from my PC for a while.

Usage

server.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") -- 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()
+

client.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") -- 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()
+

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)

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)

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:

Both TCP/UPD Clients and Servers contain the same methods:
Server Object:

General Server Methods

TCP Server Only Methods

General Client Methods

UDP Client Only Methods

TCP Client Only Methods

When using the module creation support here is the shell that you can use:

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/README.md b/README.md index 1b760ae..c6ad1f9 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ A simple and powerful way to make servers and clients - [ ] Threading - Simple threading ~~(UDP/AUDP Only)~~ Thanks to an updated multi library we can thread with ease - [ ] Priority handling +# Note +You will see a bunch of files inside of the net folder. All that is stable is the init.lua and sft.lua file. Everything else is a work in progress. Plus I am planning on rewritting all of the modules to take advantage of the new threading features that are found in the new multi updates. PRogress on this will be made soon. I have just been away from my PC for a while. # Usage server.lua ```lua diff --git a/net/emailWIP.lua b/net/emailWIP.lua deleted file mode 100644 index dbea43a..0000000 --- a/net/emailWIP.lua +++ /dev/null @@ -1,53 +0,0 @@ -require("net.identity") -net:registerModule("email",{1,0,0}) -smtp = require 'socket.smtp' -ssl = require 'ssl' - -function net.email.init(from,user,pass) - net.OnServerCreated:connect(function(s) - s.from=from - s.user=user - s.pass=pass - function s:sendMessage(subject, body, dTable) - local msg = { - headers = { - from = '<'..dTable.email..'>' - to = dTable.nick..' <'..dTable.email..'>', - subject = subject - }, - body = body - } - local ok, err = smtp.send { - from = '<'..self.from..'>', - rcpt = '<'..dTable.email..'>', - source = smtp.message(msg), - user = self.user, - password = self.pass, - server = 'smtp.gmail.com', - port = 465, - create = net.sslCreate - } - if not ok then - print("Mail send failed", err) -- better error handling required - end - end - end) -end -function net.sslCreate() - local sock = socket.tcp() - return setmetatable({ - connect = function(_, host, port) - local r, e = sock:connect(host, port) - if not r then return r, e end - sock = ssl.wrap(sock, {mode='client', protocol='tlsv1'}) - return sock:dohandshake() - end - }, { - __index = function(t,n) - return function(_, ...) - return sock[n](sock, ...) - end - end - }) -end - diff --git a/net/loggingWIP.lua b/net/loggingWIP.lua deleted file mode 100644 index be82728..0000000 --- a/net/loggingWIP.lua +++ /dev/null @@ -1,7 +0,0 @@ -net.OnServerCreated:connect(function(s) - print("The logging Module has been loaded onto the server!") - s.OnDataRecieved:fConnect(function(self,data,cid,ip,port) - log(tostring(ip)..":"..tostring(port),"Server-log.log") - log(data,"Server-log.log") - end) -end) diff --git a/net/p2pWIP.lua b/net/p2pWIP.lua deleted file mode 100644 index a1ece36..0000000 --- a/net/p2pWIP.lua +++ /dev/null @@ -1,31 +0,0 @@ -net:registerModule("p2p",{1,0,0}) -net.p2p.peerdata={} ---[[ -PID(peerID)= - -]] -function net.newP2PClient(host,port) - -- -end -function net.aft:init() -- calling this initilizes the library and binds it to the servers and clients created - --Server Stuff - net.OnServerCreated:connect(function(s) - print("The aft(Advance File Transfer) Module has been loaded onto the server!") - if s.Type~="udp" then - print("As of right now p2p is only avaliable using udp!") - return "ERR_NOT_UDP" - end - s.OnDataRecieved(function(self,data,cid,ip,port) -- when the server recieves data this method is triggered - -- - end,"p2p") -- some new stuff - end) - --Client Stuff - net.OnClientCreated:connect(function(c) - c.OnDataRecieved(function(self,data) -- when the client recieves data this method is triggered - -- - end,"p2p") - end) -end -if net.autoInit then - net.aft.init() -end diff --git a/net/settingsWIP.lua b/net/settingsWIP.lua deleted file mode 100644 index de9c190..0000000 --- a/net/settingsWIP.lua +++ /dev/null @@ -1,48 +0,0 @@ -require("net") ---General Stuff ---[[ What this module does! -Adds -net.settings:init() -server:regSetting(namespace,setting) -]] -net:registerModule("settings",{1,0,0}) -net.settings.config={} -function net.settings:init() -- calling this initilizes the library and binds it to the servers and clients created - --Server Stuff - net.OnServerCreated:connect(function(s) - print("The Settings Module has been loaded onto the server!") - s.OnDataRecieved(function(self,data,cid,ip,port) -- when the server recieves data this method is triggered - local namespace,args=data:match("!settings! (%s+) (.+)") - local args - if namespace then - for i,v in pairs(net.settings.config) do - args={data:match(v[1])} - if #args~=0 then - v[2]:Fire(self,data,cid,ip,port,unpack(args)) - break - end - end - end - end,"settings") - function s:regSetting(namespace,settings) - if not net.settings.config[namespace] then - net.settings.config[namespace]={} - end - local connection=multi:newConnection() - table.insert(net.settings.config[namespace],{"!settings! "..namespace.." "..settings,connection}) - return connection - end - end) - --Client Stuff - net.OnClientCreated:connect(function(c) - c.OnDataRecieved:(function(self,data) -- when the client recieves data this method is triggered - --First Lets make sure we are getting Setting data - end,"setings") - function sendSetting(namespace,args) - self:send("!settings! "..namespace.." "..args) - end - end) -end -if net.autoInit then - net.settings:init() -end diff --git a/net/threadingWIP.lua b/net/threadingWIP.lua deleted file mode 100644 index c83b2c2..0000000 --- a/net/threadingWIP.lua +++ /dev/null @@ -1,37 +0,0 @@ ---[=[ About This Module! - This module is server side only! (Might add client side if requested) - Aim is to make each lane (thread) have no more than 'n' number of connections - This module hyjacks the multi:newConnection() function to seemlessly add support for threads without you having to change much - As long as each server-client connection is isolated you should be fine - The chatting module however IS NOT an isolated module, so take a look at how data was handled in that module to allow for both - threaded and non threaded use - - How? - When this module is loaded all server creation is altered by passing a proxyServer instead of an actual server object - for example: - proxy=net:newTCPServer(12345) - proxy:OnDataRecieved(function(self,data,cid,ip,port) - self:send("My data!") - end) - the real server instance could be on any of the threads. Careful! While using this is seemless becareful of IO opperations! -]=] ---~ net:registerModule("threading",{1,0,0}) ---~ if not(lanes) then error("Require the lanes module!") end ---~ local serverlinda = lanes.linda() ---~ net.threading.newServer=net.newServer -- store the original method ---~ net.threading.newTCPServer=net.newTCPServer -- store the original method ---~ net.threading.proxy={} -- namespace for the proxy stuff. Because of the desgin intention of both UDP/TCP servers Only one proxy is needed -lanes=require("lanes") -serverlinda = lanes.linda() -mt={ - __index=function(t,k) print("IND",t,k) end, - __newindex=function(t,k,v) print("NewIND",t,k,v) end, -} -test={} -setmetatable(test,mt) -test.a="hi" -test.a=true -g=test['a'] -print(test.b) ---~ setmetatable(net.threading.proxy,mt) -- set the proxies metatable, to prevent bleeding only create one server. -