Advertisement
HawkPB

Untitled

Aug 6th, 2024 (edited)
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. --server client made easy [[SERVER]]
  2.  
  3. local serverport=1
  4. local useStrictClientAddresses=false
  5. local clientAddresses={}
  6. local unnessecaryPrints=true
  7.  
  8. --/requires/--
  9. local modem=require"component".modem
  10. local event=require"event"
  11. local thread=require"thread"
  12.  
  13. --/core variables/--
  14. local runningThreads={}
  15. local registeredClients={
  16. byIP={},
  17. byIdent={}
  18. }
  19.  
  20. --/core functions/--
  21. function containsValue(table, value)
  22. for _, v in pairs(table) do
  23. if v == value then
  24. return true
  25. end
  26. end
  27. return false
  28. end
  29. local function splitString(input, delimiter)
  30. local result = {}
  31. local pattern = string.format("([^%s]+)", delimiter)
  32.  
  33. for match in string.gmatch(input, pattern) do
  34. table.insert(result, match)
  35. end
  36.  
  37. return result
  38. end
  39. local function startsWith(str, prefix)
  40. return string.sub(str, 1, string.len(prefix)) == prefix
  41. end
  42. local function encrypt(args)
  43. local retString=""
  44. if args then
  45. if #args>0 then
  46. for _, arg in ipairs(args) do
  47. retString=retString..tostring(arg).."�"
  48. end
  49. end
  50. end
  51. if string.sub(retString,-1)=="�" then
  52. retString=string.sub(retString,-1)
  53. end
  54. return retString
  55. end
  56.  
  57. if unnessecaryPrints then print("server booted") end
  58.  
  59. --/core init logic/--
  60. if not modem.isOpen(serverport) then modem.open(serverport) end -- opens the port on the modem
  61. thread.create(function() -- registers clients
  62.  
  63. if unnessecaryPrints then print("{Client Register Thread}: Booted") end
  64. while true do
  65. local _, _, from, port, _, message = event.pull("modem_message")
  66. if port==serverport and startsWith(message,"/#@-REGISTER") then
  67. if unnessecaryPrints then print("{Client Register Thread}: Client registered") print("ID: "..splitString(message,"&")[2]) print("Port: "..splitString(message,"&")[3]) end
  68. registeredClients.byIP[from]=splitString(message,"&")[3]
  69. registeredClients.byIdent[splitString(message,"&")[2]]=splitString(message,"&")[3]
  70. end
  71. end
  72. end)
  73.  
  74.  
  75. --/api functions/--
  76. function addRequestHandler(id,callback,detailed)
  77. if detailed==nil then
  78. detailed=false
  79. end
  80. local newThread=thread.create(function()
  81. if unnessecaryPrints then print("{Request Handler Thread ["..id.."]}: Booted") end
  82. while true do
  83. local _, _, from, port, _, message = event.pull("modem_message")
  84. if port==serverport and splitString(message,"�")[1]==id then
  85. valid=true
  86. if useStrictClientAddresses then
  87. if not containsValue(clientAddresses,message) then
  88. valid=false
  89. end
  90. end
  91. if valid then
  92. if unnessecaryPrints then print("{Request Handler Thread ["..id.."]}: Fired") end
  93. local args=splitString(message,"�")
  94. table.remove(args,1)
  95. local details={from=from,port=port,message=message}
  96. if detailed then
  97. callback(details,args)
  98. else
  99. callback(args)
  100. end
  101.  
  102. end
  103. end
  104.  
  105. end
  106. end)
  107. table.insert(runningThreads,newThread)
  108. return newThread
  109. end
  110.  
  111. function sendRequest(client,id,args)
  112. local clientPort=-1
  113. if registeredClients.byIP[client] then
  114. clientPort=registeredClients.byIP[client]
  115. elseif registeredClients.byIdent[client] then
  116. clientPort=registeredClients.byIdent[client]
  117. end
  118. if unnessecaryPrints then print("Send Request Process: Suspected Client Port: "..tostring(clientPort)) end
  119. if unnessecaryPrints then print("Send Request Process: Packet Data: "..id.."�"..encrypt(args)) end
  120. if clientPort~=-1 then
  121. if unnessecaryPrints then print("Modem Broadcast Successful") end
  122. modem.broadcast(clientPort,tostring(id).."�"..encrypt(args))
  123. return true
  124. end
  125. return false
  126. end
  127.  
  128. --/test code/--
  129. addRequestHandler("ping",function(details)
  130. print("pong")
  131. sendRequest(details.from,"pong")
  132. end,true)
  133. while true do os.sleep(.1) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement