Advertisement
Tatantyler

CRFv3.1 File Transfer Server

Aug 17th, 2013
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.87 KB | None | 0 0
  1. -- File Transfer Server over CRFv3.1
  2.  
  3. -- Packet structure:
  4. -- [1] : Ident string
  5. -- [2] : Action
  6. -- [3] : Target
  7. -- [4] : Parameter 1
  8. -- [5] : Parameter 2
  9.  
  10. local args = {...}
  11.  
  12. local filePath = "server/"
  13.  
  14. -- Standard error messages:
  15. local e_noexist = textutils.serialize({"FileTransfer", "Error", "File does not exist"})
  16. local e_exist = textutils.serialize({"FileTransfer", "Error", "File already exists"})
  17. local e_isdir = textutils.serialize({"FileTransfer", "Error", "File is a directory"})
  18. local e_hasdata = textutils.serialize({"FileTransfer", "Error", "Directory contains files"})
  19. -- Not an error message:
  20. local m_okay = textutils.serialize({"FileTransfer", "Okay"})
  21.  
  22. if not LANClient then
  23.     error("LAN Client API not loaded.")
  24. end
  25.  
  26. while true do
  27.     local sender, data = LANClient.receive()
  28.     data = textutils.unserialize(data)
  29.     if type(data) == "table" then
  30.         if data[1] == "FileTransfer" then
  31.             if data[2] == "read" then
  32.                 if not fs.exists(data[3]) then
  33.                     LANClient.send(sender, e_noexist)
  34.                 else
  35.                     if data[4] then -- Binary flag
  36.                         local f = fs.open(data[3], "rb")
  37.                         local bytes = {}
  38.                         local lastPause = os.clock()
  39.                         while true do
  40.                             local byte = f.read()
  41.                             if not byte then
  42.                                 break
  43.                             else
  44.                                 table.insert(bytes, byte)
  45.                             end
  46.                             if (os.clock() - lastPause) >= 2.90 then
  47.                                 os.queueEvent("")
  48.                                 os.pullEvent("")
  49.                                 lastPause = os.clock()
  50.                             end
  51.                         end
  52.                         f.close()
  53.                         LANClient.send(sender, textutils.serialize({"FileTransfer", "data", bytes}))
  54.                     else
  55.                         local f = fs.open(data[3], "r")
  56.                         local text = f.readAll()
  57.                         f.close()
  58.                         LANClient.send(sender, textutils.serialize({"FileTransfer", "data", text}))
  59.                     end
  60.                 end
  61.             elseif data[2] == "write" then
  62.                 if data[5] then
  63.                     local f = fs.open(data[3], "wb")
  64.                     local lastPause = os.clock()
  65.                     for i=1, #data[4] do
  66.                         f.write(data[4][i])
  67.                         if (os.clock() - lastPause) >= 2.90 then
  68.                             os.queueEvent("")
  69.                             os.pullEvent("")
  70.                             lastPause = os.clock()
  71.                         end
  72.                     end
  73.                     f.close()
  74.                     LANClient.send(sender, m_okay)
  75.                 else
  76.                     local f = fs.open(data[3], "w")
  77.                     f.write(data[4])
  78.                     f.close()
  79.                     LANClient.send(sender, m_okay)
  80.                 end
  81.             elseif data[2] == "mkdir" then
  82.                 if fs.exists(data[3]) then
  83.                     LANClient.send(sender, e_exist)
  84.                 else
  85.                     fs.makeDir(data[3])
  86.                     LANClient.send(sender, m_okay)
  87.                 end
  88.             elseif data[2] == "delete" then
  89.                 if not fs.exists(data[3]) then
  90.                     LANClient.send(sender, e_noexist)
  91.                 else
  92.                     fs.delete(data[3])
  93.                     LANClient.send(sender, m_okay)
  94.                 end
  95.             elseif data[2] == "list" then
  96.                 if not fs.exists(data[3]) then
  97.                     LANClient.send(sender, e_noexist)
  98.                 else
  99.                     LANClient.send(sender, textutils.serialize({"FileTransfer", "list", fs.list(data[3])}))
  100.                 end
  101.             end
  102.         end
  103.     end
  104. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement