Last active
August 29, 2015 14:23
-
-
Save notcake/2ed3207757c31def89dd to your computer and use it in GitHub Desktop.
XC.IO.RemoteInStream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local self = {} | |
XC.IO.RemoteInStream = XC.MakeConstructor (self, XC.IO.IInStream) | |
function XC.IO.RemoteInStream.Handle (inStream, outBuffer, connection) | |
outBuffer:UInt64 (inStream:GetSize ()) | |
connection:Write (outBuffer) | |
while true do | |
local command, inBuffer = connection:Read ("StringN8") | |
XC.Logger:Message ("XC.IO.RemoteInStream.Handle : Command " .. command) | |
if command == "" or command == "Close" then | |
-- Don't close the connection since we didn't open it | |
return | |
elseif command == "SeekAbsolute" then | |
local seekPos = inBuffer:UInt64 () | |
inStream:SeekAbsolute (seekPos) | |
connection:Write ("UInt64", inStream:GetPosition ()) | |
elseif command == "Read" then | |
local size = inBuffer:UInt64 () | |
local data = inStream:Read (size) | |
local chunks = {} | |
for i = 1, #data, 48 * 1024 do | |
chunks [#chunks + 1] = string.sub (data, i, i + 48 * 1024 - 1) | |
end | |
connection:Write ("UInt64", inStream:GetPosition (), "UInt16", #chunks, "StringN16", chunks [1]) | |
for i = 2, #chunks do | |
connection:Write ("StringN16", chunks [i]) | |
end | |
end | |
end | |
end | |
function self:ctor (inBuffer, connection) | |
self.Connection = connection | |
self.Position = 0 | |
self.Size = inBuffer:UInt64 () | |
end | |
function self:dtor () | |
self:Close () | |
end | |
function self:Close () | |
if self.Connection then | |
self.Connection:Write ("StringN8", "Close") | |
self.Connection:Close () | |
self.Connection = nil | |
end | |
end | |
function self:Read (size) | |
self.Connection:Write ("StringN8", "Read", "UInt64", size) | |
local position, chunkCount, firstChunk, inBuffer = self.Connection:Read ("UInt64", "UInt16", "StringN16") | |
self.Position = position | |
local chunks = {} | |
local chunkCount = chunkCount | |
chunks [1] = firstChunk | |
for i = 2, chunkCount do | |
chunks [i] = self.Connection:Read ("StringN16") | |
end | |
return table.concat (chunks) | |
end | |
function self:GetPosition () | |
return self.Position | |
end | |
function self:GetSize () | |
return self.Size | |
end | |
function self:SeekAbsolute (seekPos) | |
self.Connection:Write ("StringN8", "SeekAbsolute", "UInt64", seekPos) | |
self.Position = self.Connection:Read ("UInt64") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment