On this cases, the sender first sends a textline, something like:
FILE {filename} {hashofthefile}
The receiver read the line first and then receives the file.
Server Example of code:
LLine := AContext.Connection.IOHandler.ReadLn(IndyTextEncoding_UTF8); AFileStream := TFileStream.Create(UpdateZipName, fmCreate); AContext.Connection.IOHandler.ReadStream(AFileStream);
But i want be able to NOT DOWNLOAD the file and clear it from the buffer.
If i use
Conexiones[Slot].context.Connection.IOHandler.InputBuffer.Clear;
it deletes the file, but also all that is inmediately after the file, including text lines and/or other files.
You should include the file's size, too.
I would not send the hash up front, though.
I would also suggest making the receiver send a preliminary response back to the sender BEFORE allowing the sender to begin sending the actual file bytes.
EDIT: I got the filesize in bytes using filesize. Now i can send that info too, but how i could clear that specified number of bytes from inputbuffer?
QuoteI would not send the hash up front, though.
99% of times, the file hash is already stored on memory (files are hashed inmediately after been created). Also, the hash is the main information to let know the receiver if it is the required file.
QuoteI would also suggest making the receiver send a preliminary response back to the sender BEFORE allowing the sender to begin sending the actual file bytes.
It is been done before (almost always):
Receiver: REQUEST {filename} {filehash}
sender: FILE {filename} {filehash}
But sometimes the sender generates a new file that must be shared with all the peers in the network. In those cases, the sender sends this:
sender: FILE {filename} {filehash} {UserWhoCreatedThefile} {SignToVerify}
The receiver, in this case, should verify if {UserWhoCreatedThefile} and {SignToVerify} are valid. If so, then download the file and re-send to the other peers; if not, just ignore the file.
For your reply, i guess i could send the filesize in advance ... so, if the receiver do not want to receive the file, it could clear that size from the inputbuffer
what i neither know how could be done
How i can get an error during the download of the file, so i can close the connection?
(Note: This function is running in a thread)
AFileStream := TFileStream.Create(UpdateZipName, fmCreate); AContext.Connection.IOHandler.ReadStream(AFileStream); // If an error happens during the download, how i could catch it? // Or how i could know the size of the data already downloaded? // Try...except block somehow is not working...
AContext.Connection.IOHandler.DiscardAll();