Advertisement
kryptanyte

client

Apr 13th, 2019 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.13 KB | None | 0 0
  1. local network = peripheral.wrap("back");
  2.  
  3. local s_netChannel = os.getComputerID();
  4.  
  5. --[[
  6.  
  7.   Network Functions
  8.  
  9. ]]--
  10.  
  11. function netTcpReceiveResponse(target)
  12.  
  13.   local timeout = os.startTimer(5);
  14.  
  15.   while true do
  16.  
  17.     local e, v1, sChannel, rChannel, resp = os.pullEvent();
  18.    
  19.     if( e == "modem_message" ) then
  20.    
  21.       if( resp == "tcpreceived:"..target ) then
  22.    
  23.         return true;
  24.    
  25.       end --if
  26.      
  27.     elseif( e == "timer" and v1 == timeout ) then
  28.    
  29.       return false;
  30.    
  31.     end --if
  32.  
  33.   end --while
  34.  
  35. end --netTcpReceiveResponse
  36.  
  37. function netSend(target, message, tcp)
  38.  
  39.   network.transmit(target, s_netChannel, message);
  40.  
  41.   if(tcp ~= nil and tcp == true) then
  42.  
  43.     netTcpReceiveResponse(target);
  44.  
  45.   end --if
  46.  
  47. end --netSend
  48.  
  49. function netPackage(act, dt)
  50.  
  51.   local pack = { action = act, data = dt }
  52.  
  53.   return textutils.serialise(pack);
  54.  
  55. end --netPackage
  56.  
  57. function netReceive(target, act)
  58.  
  59.   local timer = os.startTimer(5);
  60.  
  61.   while true do
  62.  
  63.     local e, v1, sChannel, rChannel, resp = os.pullEvent();
  64.    
  65.     if( e == "modem_message" ) then
  66.    
  67.       local data = textutils.unserialise(resp);
  68.      
  69.       if( data.action == act ) then
  70.      
  71.         return data.data;
  72.      
  73.       end
  74.    
  75.     end --if
  76.  
  77.   end --while
  78.  
  79. end --netReceive
  80.  
  81. function netTcpSendResponse(target)
  82.  
  83.   network.transmit(target, s_netChannel, "tcpreceived:"..os.getComputerID());
  84.  
  85.   print("tcp response sent");
  86.  
  87. end --netTcpSendResponse
  88.  
  89. --[[
  90.  
  91.   Main
  92.  
  93. ]]--
  94.  
  95. function main()
  96.  
  97.   network.open(os.getComputerID());
  98.  
  99.   while true do
  100.  
  101.     local _, _, sChannel, rChannel, resp = os.pullEvent("modem_message");
  102.    
  103.     print("modem message received");
  104.    
  105.     if( resp ~= nil ) then
  106.    
  107.       local data = textutils.unserialise(resp);
  108.      
  109.       if( data.action == "requestName" ) then
  110.        
  111.         netTcpSendResponse(rChannel);
  112.        
  113.         local pack = netPackage("requestName", { name = os.getComputerLabel() });
  114.        
  115.         netSend(rChannel, pack, false);
  116.      
  117.       end --if
  118.    
  119.     end --if
  120.  
  121.   end --while
  122.  
  123. end --main
  124.  
  125. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement