network.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. local BASE = (...):match("(.-)[^%.]+$")
  2. local BASE_SLASH = BASE:sub(1,#BASE-1) .. "/"
  3. local Server = require( BASE .. "server" )
  4. local Client = require( BASE .. "client" )
  5. -- Load advertising (serverlist) submodule
  6. local advertise = require( BASE .. "advertise" )
  7. local network = {}
  8. network.advertise = advertise
  9. local conn = nil
  10. local connectionType = ""
  11. local connected = false
  12. local users = {}
  13. local PORT = 3410 -- port used to send data (TCP)
  14. local server = nil
  15. local client = nil
  16. function network:startServer( maxNumberOfPlayers, port, pingTime )
  17. local createServer = function()
  18. return Server:new( maxNumberOfPlayers, port or PORT, pingTime, UDP_BROADCAST_PORT )
  19. end
  20. success, server = pcall( createServer )
  21. local err = ""
  22. if not success then
  23. err = server
  24. server = nil
  25. end
  26. return server, err
  27. end
  28. function network:startClient( address, playername, port, authMsg )
  29. if not address or #address == 0 then
  30. print("[NET] No address found. Using default: 'localhost'")
  31. address = "localhost"
  32. end
  33. print( "[NET] Connecting to:", address, port, authMsg)
  34. local createClient = function()
  35. return Client:new( address, port or PORT, playername, authMsg )
  36. end
  37. success, client = pcall( createClient )
  38. local err = ""
  39. if not success then
  40. err = client
  41. client = nil
  42. end
  43. assert(client, "Could not connect." )
  44. return client, err
  45. end
  46. function network:closeConnection()
  47. print("[NET] Closing all connections.")
  48. if client then
  49. client:close()
  50. end
  51. if server then
  52. server:close()
  53. end
  54. end
  55. function network:update( dt )
  56. if server then
  57. -- If updating the server returns false, then
  58. -- the connection has been closed.
  59. if not server:update( dt ) then
  60. server = nil
  61. end
  62. end
  63. if client then
  64. -- If updating the client returns false, then
  65. -- the connection has been closed.
  66. if not client:update( dt ) then
  67. client = nil
  68. end
  69. end
  70. advertise:update( dt )
  71. end
  72. function network:getUsers()
  73. if server then
  74. return server:getUsers(), server:getNumUsers()
  75. end
  76. if client then
  77. return client:getUsers(), client:getNumUsers()
  78. end
  79. end
  80. --[[function network:send( command, msg )
  81. if client then
  82. client:send( command, msg )
  83. end
  84. end]]
  85. function stringToType( value, goalType )
  86. if goalType == "number" then
  87. return tonumber(value)
  88. elseif goalType == "boolean" then
  89. return value == "true" and true or false
  90. end
  91. -- if it was meant to be a string, return it as such:
  92. return value
  93. end
  94. return network