Python1320

Python1320

Sep 27th, 2010
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.48 KB | None | 0 0
  1. if ( !OOSock ) then require( "oosocks" ) end  
  2.  if ( !OOSock ) then error"no oosocks module found" end
  3.  
  4.  
  5. -- Useful (I hope) snippet for debugging
  6. -- Bit lua magic and one could generalize this for any enum debugging
  7.  
  8. local SCKCALL=
  9. [[SCKCALL_CONNECT
  10. SCKCALL_LISTEN
  11. SCKCALL_BIND
  12. SCKCALL_ACCEPT
  13. SCKCALL_REC_LINE
  14. SCKCALL_REC_SIZE
  15. SCKCALL_REC_DATAGRAM
  16. SCKCALL_SEND]]
  17. SCKCALL=string.Explode("\n",SCKCALL)
  18.  
  19. local Numbers={}
  20.  
  21. for k,v in pairs(SCKCALL) do
  22.     v=string.Trim(v)
  23.     Numbers[v]=_G[v]
  24.     if Numbers[v]==nil then ErrorNoHalt("Could not find "..tostring(v).."!\n") end
  25. end
  26. print("Loaded ",table.Count(Numbers)," SCKCALL_ enums")
  27.  
  28. function OOSocks_GetSCKCALL(num)
  29.     for name,enum in pairs(Numbers) do
  30.         if enum==num then
  31.             return name
  32.         end
  33.     end
  34. end
  35.  
  36. local SCKERR=
  37. [[SCKERR_OK
  38. SCKERR_BAD
  39. SCKERR_CONNECTION_RESET
  40. SCKERR_NOT_CONNECTED
  41. SCKERR_TIMED_OUT]]
  42. SCKERR=string.Explode("\n",SCKERR)
  43.  
  44. local Numbers={}
  45.  
  46. for k,v in pairs(SCKERR) do
  47.     v=string.Trim(v)
  48.     Numbers[v]=_G[v]
  49.     if Numbers[v]==nil then ErrorNoHalt("Could not find "..tostring(v).."!\n") end
  50. end
  51. print("Loaded ",table.Count(Numbers)," SCKERR_ enums")
  52.  
  53.  
  54. function OOSocks_GetSCKERR(num)
  55.     for name,enum in pairs(Numbers) do
  56.         if enum==num then
  57.             return name
  58.         end
  59.     end
  60. end
  61.  
  62.  
  63.  
  64.  
  65.  
  66. clients = {};
  67.  
  68. local function ClientSockCallback(socket, callType, callId, err, data, peer, peerPort)
  69.     if(err != SCKERR_OK) then
  70.         Error("sckerr",err);
  71.         socket:Close()
  72.     end
  73.  
  74.     if(callType == SCKCALL_REC_LINE) then
  75.         print(peer,peerPort,data);
  76.         if data=="" then data="!NO DATA!" end
  77.         if(data != "END") then
  78.             socket:ReceiveLine(); -- If the client hasnt told us they are done, read annother line!
  79.         end
  80.     end
  81. end
  82.  
  83.  
  84. -- This is our listen socket. It will just accept every socket into clients.
  85.  
  86. ServerSock = OOSock(IPPROTO_TCP);
  87.  
  88. ServerSock:SetCallback(function(socket, callType, callId, err, data, peer, peerPort)
  89.     if(err != SCKERR_OK) then
  90.         print("ERRROR",OOSocks_GetSCKCALL(callType), OOSocks_GetSCKERR(err) )  
  91.     end
  92.  
  93.     if(callType == SCKCALL_BIND) then
  94.         socket:Listen(1231);
  95.         print"listening"
  96.     end
  97.  
  98.     if(callType == SCKCALL_LISTEN) then
  99.         socket:Accept();
  100.     end
  101.  
  102.     if(callType == SCKCALL_ACCEPT) then
  103.         print("Got client " .. peer .. "\n");
  104.                
  105.         data:SetCallback(ClientSockCallback);
  106.         data:ReceiveLine() -- Lets for some reason, read 1 line.
  107.  
  108.         table.insert(clients, data); -- I dunno, we might need em.
  109.  
  110.         -- Accept annother socket.
  111.         socket:Accept();
  112.     end
  113. end)
  114.  
  115. ServerSock:Bind("", 1231);
Add Comment
Please, Sign In to add comment