Advertisement
xerpi

a

Jun 30th, 2011
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.55 KB | None | 0 0
  1.  
  2. -- @SOCKETS MULTIPLAYER  
  3. -- PROTOCOL:
  4. --   Transfer between 'packets' of data, each packed separated by '/'.
  5. --   Each packet, also, starts with '(' and ends with ')'. These are arbitrarily
  6. --   selected at design time, and will let us know if a packed arrived entirely.
  7. --   If arrived entirely, process it, else, discard, unless it is the last received,
  8. --   cause the last received probably been cutted, and receive the next part in the next
  9. --   read. If this happens, should be executed correctly, else, discard.
  10.  
  11. coop = {
  12.     status      = 0,
  13.     connect     = false,
  14.     socket      = nil,
  15.     host        = "www.gcrew.es",
  16.     port        = 8080,
  17.     queue       = { },                                                                      -- Write queue.
  18.     lastcmd     = os.clock(),
  19.     linked      = false,
  20.     linkmode    = 0,
  21.     playing     = false,
  22.     delay       = 1;
  23.     send        = function(t)                                                               -- Enqueue data to send.
  24.                 if t then local s = coop.queue; table.insert(s,"("..t..")");
  25.                 coop.queue = s;
  26.                 end end,
  27.     read        = function()                                                                -- Get data from socket.
  28.                 if coop.socket then local a,b = coop.socket:recv();                         --    Read data
  29.                 if a != "" then coop.lastcmd = os.clock(); return a, b; end                 --    Data and length if data found.
  30.                 return false, 0; end return false, 0;                                       --    false and 0 if error.
  31.                 end,
  32.     flush       = function() coop.queue = { }; coop.playing = false;    -- Flush connection.
  33.                 coop.delay = 1;
  34.                 coop.linked = false; if coop.socket then                                    --    Clear vars, disconnect...
  35.                 coop.socket:send("QUIT\n"); coop.socket:free(); coop.socket = nil; end      --    ... prepare all for next
  36.                 coop.status = 0;                                                            --    connection.
  37.                 coop.lastcmd = os.clock(); end;                                             --    ....
  38.     checkconn   = function()                                                                -- Check connection sanity.
  39.     if not coop.connect then return true; end;                                              -- If no need of connection. get back.
  40.    
  41.     if coop.connect and not wlan.connected() then                                           -- If required connection and not wlan..
  42.         coop.flush();                                                                       -- ... init wlan. retry.
  43.         local c = wlan.init(0);
  44.         if not c or c == 0 then coop.connect = false; end
  45.         return true;                                           
  46.     end
  47.     if coop.linked and not coop.connect then                                                -- If linked, but not asked to..
  48.         coop.flush(); wlan.term(); return true;                                             -- flush(). retry
  49.     end
  50.     if not coop.socket then                                                                 -- If no socket...
  51.         local a,b = socket.udp(coop.host,coop.port);                                        -- create socket...
  52.         if a and b then coop.socket = a; return true; end                                   -- ... and return if ok.
  53.     end
  54.     if wlan.strength() < 40 then return true; end                                           -- Houston... signal... too weak...
  55.     if not coop.socket then return true; end                                                -- If no socket. retry.
  56.     coop.status = math.max(coop.status,1);
  57.     return false;
  58.     end,   
  59.     getall      = function()                                                                -- Get all avaiable data.
  60.     local data, len = coop.read();                                                          -- Read from socket...
  61.     if len > 0 then
  62.         for cmd in data:gmatch("%b()") do
  63.             coop.cmd(cmd:sub(2,-2));
  64.         end
  65.     end
  66.     end,
  67.     write       = function()                                                                -- Send queued data.
  68.     if coop.queue[1] then                                                                   -- If pending data...
  69.         local len;
  70.         len = coop.socket:send(coop.queue[1].."\n",coop.host,coop.port);
  71.         if (len > 0) then                                                                   -- If sent...
  72.             table.remove(coop.queue,1);                                                     --    Remove from queue.
  73.             return true;
  74.         end
  75.         return false;
  76.     end
  77.     return false; end,
  78.     cmd         = function(rcvd)                                                            -- Process one command:
  79.         local cmd = rcvd:explode(" ");                                                      -- Split by words.
  80.         if (cmd[1] == "FLY") then
  81.             local r = cmd[2]:explode(",");
  82.             pla.x2, pla.y2, pla.z2, pla.RZ2 = tonumber(r[1]), tonumber(r[2]), tonumber(r[3]), tonumber(r[4]);
  83.             coop.linked = true;
  84.             coop.status = 2;
  85.         end
  86.     end,
  87.     exec        = function()                                                                    -- For every cycle:
  88.     if coop.checkconn() then return; end                                                        -- Check connection / connect.
  89.     if os.clock() - coop.lastcmd > 10 and coop.linked then coop.connect = false; coop.flush(); end
  90.     if os.clock() - coop.lastcmd > 60 and not coop.linked then coop.connect = false; coop.flush(); end
  91.     coop.getall();                                                                              -- Check for avail. data.
  92.     local written = coop.write();                                                               -- Send avail. data.
  93.     if (os.clock() - coop.lastcmd) > coop.delay then                                                    -- While foreva' and waiting a bit...
  94.         if coop.status == 0 then return end;
  95.         if coop.status == 1 then coop.send("WPLAY"); return; end;
  96.         if coop.status == 2 then
  97.             coop.send(string.format("FLY %.2f,%.2f,%.2f,%.4f",pla.x,pla.y,pla.z,pla.RZ));       --   Refresh my position...
  98.             return;
  99.         end
  100.     end
  101.     screen.print(10,50,"Status: "..coop.status);
  102.     end,
  103. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement