1
0
mirror of https://github.com/LDDestroier/CC/ synced 2025-05-04 16:04:05 +00:00

Fixed receive, added ID check, send removes old messages

This commit is contained in:
LDDestroier 2019-05-08 16:56:36 -04:00 committed by GitHub
parent 9262842835
commit ff9afd4ac8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -101,11 +101,12 @@ disknet.send = function(channel, message, recipient)
fs.open(fs.combine(disknet.mainPath, tostring(channel)), "w").close() fs.open(fs.combine(disknet.mainPath, tostring(channel)), "w").close()
end end
local contents = textutils.unserialize(readFile(fs.combine(disknet.mainPath, tostring(channel)))) local contents = textutils.unserialize(readFile(fs.combine(disknet.mainPath, tostring(channel))))
local cTime = getTime()
if disknet.isOpen(channel) then if disknet.isOpen(channel) then
local file = fs.open(fs.combine(disknet.mainPath, tostring(channel)), "w") local file = fs.open(fs.combine(disknet.mainPath, tostring(channel)), "w")
if contents then if contents then
contents[#contents + 1] = { contents[#contents + 1] = {
time = getTime(), time = cTime,
id = yourID, id = yourID,
uniqueID = uniqueID, uniqueID = uniqueID,
messageID = math.random(1, 2^31 - 1), messageID = math.random(1, 2^31 - 1),
@ -113,13 +114,18 @@ disknet.send = function(channel, message, recipient)
recipient = recipient, recipient = recipient,
message = message, message = message,
} }
for i = #contents, 1, -1 do
if cTime - (contents[i].time or 0) > ageToToss then
table.remove(contents, i)
end
end
if #contents > maximumBufferSize then if #contents > maximumBufferSize then
table.remove(contents, 1) table.remove(contents, 1)
end end
file.write(textutils.serialize(contents)) file.write(textutils.serialize(contents))
else else
file.write(textutils.serialize({{ file.write(textutils.serialize({{
time = getTime(), time = cTime,
id = yourID, id = yourID,
uniqueID = uniqueID, uniqueID = uniqueID,
messageID = math.random(1, 2^31 - 1), messageID = math.random(1, 2^31 - 1),
@ -137,7 +143,7 @@ disknet.send = function(channel, message, recipient)
end end
end end
local fList, pList = {}, {} local fList, pList, sList = {}, {}, {}
local loadFList = function() local loadFList = function()
fList, pList = {}, {} fList, pList = {}, {}
@ -152,36 +158,38 @@ local loadFList = function()
end end
end end
disknet.receive = function(channel) disknet.receive = function(channel, senderFilter)
local valid, grr = checkValidChannel(channel) local valid, grr = checkValidChannel(channel)
if valid or not channel then if valid or not channel then
local output, contents local output, contents
local doRewrite = false local doRewrite = false
loadFList()
local good, goddamnit = pcall(function() local good, goddamnit = pcall(function()
local cTime = getTime()
while true do while true do
loadFList()
for i = 1, #fList do for i = 1, #fList do
contents = fList[i].readAll() contents = fList[i].readAll()
if contents ~= "" then if contents ~= "" then
fList[i].close() contents = textutils.unserialize(contents)
fList[i] = fs.open(pList[i], "r")
contents = textutils.unserialize(fList[i].readAll())
if type(contents) == "table" then if type(contents) == "table" then
if contents[1] then if contents[1] then
if not output then if not output then
for look = 1, #contents do for look = 1, #contents do
if (contents[look].uniqueID ~= uniqueID) and (not msgCheckList[contents[look].messageID]) then if (contents[look].uniqueID ~= uniqueID) and (not msgCheckList[contents[look].messageID]) then -- make sure you're not receiving messages that you sent
if (not contents[look].recipient) or contents[look].recipient == yourID then if (not contents[look].recipient) or contents[look].recipient == yourID then -- make sure that messages intended for others aren't picked up
if getTime() - (contents[look].time or 0) <= ageToToss then if (not channel) or channel == contents[look].channel then -- make sure that messages are the same channel as the filter, if any
msgCheckList[contents[look].messageID] = true if (not senderFilter) or senderFilter == contents[look].id then -- make sure that the sender is the same as the id filter, if any
output = {} if cTime - (contents[look].time or 0) <= ageToToss then -- make sure the message isn't too old
for k,v in pairs(contents[look]) do msgCheckList[contents[look].messageID] = true
output[k] = v output = {}
for k,v in pairs(contents[look]) do
output[k] = v
end
break
end
end end
break
end end
end end
end end
@ -191,7 +199,7 @@ disknet.receive = function(channel)
-- delete old msesages -- delete old msesages
doRewrite = false doRewrite = false
for t = #contents, 1, -1 do for t = #contents, 1, -1 do
if getTime() - (contents[t].time or 0) > ageToToss then if cTime - (contents[t].time or 0) > ageToToss then
msgCheckList[contents[t].messageID] = nil msgCheckList[contents[t].messageID] = nil
table.remove(contents, t) table.remove(contents, t)
doRewrite = true doRewrite = true
@ -201,10 +209,6 @@ disknet.receive = function(channel)
writeFile(pList[i], textutils.serialize(contents)) writeFile(pList[i], textutils.serialize(contents))
end end
if output then if output then
for i = 1, #fList do
fList[i].close()
end
fList, pList = {}, {}
break break
end end
end end
@ -217,6 +221,10 @@ disknet.receive = function(channel)
os.queueEvent("") os.queueEvent("")
os.pullEvent("") os.pullEvent("")
end end
for i = 1, #fList do
fList[i].close()
end
fList, pList = {}, {}
end end
end) end)