mirror of
https://github.com/aziascreations/BakaShrot.git
synced 2026-11-23 20:58:46 +01:00
Add attachments (protocol 4), their edge-case tests, and handoff docs
- One optional attachment per message, any size up to the server's MaxAttachmentSize (default 1G), sent and stored in 1 MB parts, checked with its SHA-256. Uploaded before its message (UploadStart, UploadChunk), then attached by the PushMessage in the same transaction; uploads never pushed expire after UploadTimeout. Read back a part at a time (GetAttachment). - Replicated separately, after the messages: the message carries the size and SHA-256, then each check-in lists what the peer is missing (MissingAttachments) and sends the parts from where it stopped. Over the receiver's limit, the attachment is marked not kept. - Deleted with its message by the retention, parts cleaned up in the background. Schema v7 (attachments, blobs, blob_chunks), replacing the unused attachements table. - DLL: PushMessageWithFile/WithData, SaveAttachment, LoadAttachment, GetMessageAttachmentSize, IsMessageAttachmentReady. Python: push(..., attachment=), get_attachment(), save_attachment(). Tools: PushMessage -a, GetAttachment. The release smoke test round-trips one. - Tests/Attachments-Test.ps1 (39 checks) and Tests/AttachmentEdges-Test.ps1 (51 checks, with a raw protocol client): wrong SHA-256, bad parts, others' uploads, expiry, replication resuming after a kill, a corrupted stored copy. Found: a corrupted copy is re-sent to peers forever, noted as open item 3 in Notes.md with a proposed fix. - CLAUDE.md: how to build, test, the traps and working rules, for the next session. Notes.md and the Design docs updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RwFeVPXGLs3KK1MAQn3ouP
This commit is contained in:
@@ -17,10 +17,12 @@
|
||||
;
|
||||
; Once authenticated, a client's thread answers its requests one at a time
|
||||
; until it disconnects: PushMessage, GetLatestMessage, GetMessages,
|
||||
; QueryRequest, ResolveName and ListNames, each one only if its
|
||||
; QueryRequest, ResolveName and ListNames, UploadStart, UploadChunk and
|
||||
; GetAttachment (Design/Attachments.md), each one only if its
|
||||
; [BakaShrot.Client.<name>] allows it (AllowWrite and PushTopics, AllowRead
|
||||
; and ReadTopics, AllowQuery), and for
|
||||
; peers, SyncStateRequest and ReplicateMessages, refused for regular clients,
|
||||
; peers, SyncStateRequest and ReplicateMessages, then MissingAttachmentsRequest
|
||||
; and ReplicateAttachmentChunk, refused for regular clients,
|
||||
; within the peer's AcceptTopics and AcceptNodes. Any other packet aborts it.
|
||||
;
|
||||
; Topics and nodes come by name or id (Commons/Networking/Names.pbi), and
|
||||
@@ -421,7 +423,8 @@ EndProcedure
|
||||
Procedure.i BakaShrot_NetClient_HandlePushMessage(*Client.BakaShrot_NetClient, *Packet, PacketSize.i)
|
||||
Protected *View.MsgPackData, *Push.BakaShrot_PushMessage
|
||||
Protected Pushed.BakaShrot_MessagePushed
|
||||
Protected JsonData.s, TopicRef.s, TopicId.i, JsonSize.i, MessageId.q, StoreError.String
|
||||
Protected JsonData.s, TopicRef.s, TopicId.i, JsonSize.i, MessageId.q, StoreError.String, UploadId.q
|
||||
Protected Blob.BakaShrot_StorageBlob
|
||||
Protected *Answer, AnswerSize.Integer, Sent.i
|
||||
|
||||
*Push = 0
|
||||
@@ -436,6 +439,7 @@ Procedure.i BakaShrot_NetClient_HandlePushMessage(*Client.BakaShrot_NetClient, *
|
||||
EndIf
|
||||
JsonData = *Push\JsonData
|
||||
TopicRef = *Push\Topic
|
||||
UploadId = *Push\UploadId
|
||||
FreeStructure(*Push)
|
||||
|
||||
; The node is the authenticated one, the client never sends it here.
|
||||
@@ -473,7 +477,20 @@ Procedure.i BakaShrot_NetClient_HandlePushMessage(*Client.BakaShrot_NetClient, *
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The message couldn't be stored")
|
||||
EndIf
|
||||
|
||||
MessageId = InsertNextMessage(TopicId, *Client\Metadata\NodeId, JsonData, @StoreError)
|
||||
; Its attachment: one of this client's complete uploads, for that topic.
|
||||
If UploadId
|
||||
If Not GetStorageBlob(*Client\DbId, UploadId, @Blob) Or Blob\NodeId <> *Client\Metadata\NodeId
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "No such upload")
|
||||
EndIf
|
||||
If Blob\State <> #BakaShrot_Blob_Complete
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The upload isn't complete, or is attached already")
|
||||
EndIf
|
||||
If Blob\TopicId <> TopicId
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The upload is for topic '" + BakaShrot_Names_TopicRef(Blob\TopicId) + "'")
|
||||
EndIf
|
||||
EndIf
|
||||
|
||||
MessageId = InsertNextMessage(TopicId, *Client\Metadata\NodeId, JsonData, @StoreError, UploadId)
|
||||
If MessageId < 1
|
||||
PrintN("[Net] Failed to store a message from client " + Str(*Client\ClientId) + ": " + StoreError\s)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The message couldn't be stored")
|
||||
@@ -551,6 +568,8 @@ Procedure.i BakaShrot_NetClient_HandleGetLatestMessage(*Client.BakaShrot_NetClie
|
||||
Latest\MessageId = Message\MessageId
|
||||
Latest\PushedAt = Message\PushedAt
|
||||
Latest\JsonData = Message\JsonData
|
||||
Latest\AttachmentSize = Message\AttachmentSize
|
||||
Latest\AttachmentReady = Message\AttachmentReady
|
||||
*Answer = BakaShrot_LatestMessagePacket_Encode(@Latest, @AnswerSize)
|
||||
If Not *Answer
|
||||
ProcedureReturn #False
|
||||
@@ -636,6 +655,8 @@ Procedure.i BakaShrot_NetClient_HandleGetMessages(*Client.BakaShrot_NetClient, *
|
||||
Answer\Messages()\MessageId = Messages()\MessageId
|
||||
Answer\Messages()\PushedAt = Messages()\PushedAt
|
||||
Answer\Messages()\JsonData = Messages()\JsonData
|
||||
Answer\Messages()\AttachmentSize = Messages()\AttachmentSize
|
||||
Answer\Messages()\AttachmentReady = Messages()\AttachmentReady
|
||||
Next
|
||||
ClearList(Messages())
|
||||
|
||||
@@ -975,7 +996,7 @@ Procedure.i BakaShrot_NetClient_HandleReplicateMessages(*Client.BakaShrot_NetCli
|
||||
If MaxAge > 0
|
||||
SkipBefore = BakaShrot_NowUtcMs() - MaxAge
|
||||
EndIf
|
||||
IsStored = InsertReplicatedMessages(TopicId, NodeId, *Batch\Messages(), @StoreError, SkipBefore, @Skipped)
|
||||
IsStored = InsertReplicatedMessages(TopicId, NodeId, *Batch\Messages(), @StoreError, SkipBefore, @Skipped, *Client\Server\Config\Server\MaxAttachmentSize)
|
||||
FreeStructure(*Batch)
|
||||
If #BakaShrot_Net_LogRequests And Skipped\i > 0
|
||||
PrintN("[Net] Skipped " + Str(Skipped\i) + " messages of " + TopicRef + ":" + NodeRef + " from peer '" + *Client\PeerName + "', past their max age here")
|
||||
@@ -996,6 +1017,358 @@ Procedure.i BakaShrot_NetClient_HandleReplicateMessages(*Client.BakaShrot_NetCli
|
||||
ProcedureReturn Sent
|
||||
EndProcedure
|
||||
|
||||
; --- Attachments -----------------------------------------------------------------
|
||||
; See Design/Attachments.md and Storage/Attachments.pbi.
|
||||
|
||||
; Sends a ChunkStored. Returns #False if it couldn't be sent.
|
||||
Procedure.i BakaShrot_NetClient_SendChunkStored(*Client.BakaShrot_NetClient, Received.q, Complete.i)
|
||||
Protected Stored.BakaShrot_ChunkStored, *Answer, AnswerSize.Integer, Sent.i
|
||||
|
||||
Stored\Received = Received
|
||||
Stored\Complete = Complete
|
||||
*Answer = BakaShrot_ChunkStoredPacket_Encode(@Stored, @AnswerSize)
|
||||
If Not *Answer
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
Sent = BakaShrot_SendPacket(*Client\ClientId, *Answer, AnswerSize\i, @*Client\Metadata\Cipher)
|
||||
FreeMemory(*Answer)
|
||||
ProcedureReturn Sent
|
||||
EndProcedure
|
||||
|
||||
|
||||
; Starts an upload, for a message the client will push to that topic.
|
||||
; Same permissions as pushing, and at most MaxAttachmentSize bytes.
|
||||
; Returns #False if the client must be aborted.
|
||||
Procedure.i BakaShrot_NetClient_HandleUploadStart(*Client.BakaShrot_NetClient, *Packet, PacketSize.i)
|
||||
Protected *View.MsgPackData, *Start.BakaShrot_UploadStart
|
||||
Protected Started.BakaShrot_UploadStarted
|
||||
Protected TopicRef.s, TopicId.i, Size.q, MaxSize.q, BlobId.q, StoreError.String
|
||||
Protected Dim Sha256.a(#BakaShrot_Sha256Size - 1)
|
||||
Protected *Answer, AnswerSize.Integer, Sent.i
|
||||
|
||||
*Start = 0
|
||||
*View = MsgPackWrapMsgBuffer(*Packet, PacketSize)
|
||||
If *View
|
||||
*Start = BakaShrot_UploadStartPacket_Decode(*View)
|
||||
MsgPackFree(*View, #False)
|
||||
EndIf
|
||||
If Not *Start
|
||||
PrintN("[Net] Client " + Str(*Client\ClientId) + " sent an invalid upload start")
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
TopicRef = *Start\Topic
|
||||
Size = *Start\Size
|
||||
CopyMemory(@*Start\Sha256[0], @Sha256(0), #BakaShrot_Sha256Size)
|
||||
FreeStructure(*Start)
|
||||
|
||||
If Not *Client\Permissions\Write
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "Not allowed to write")
|
||||
EndIf
|
||||
If TopicRef = ""
|
||||
TopicId = *Client\Metadata\TopicId
|
||||
Else
|
||||
TopicId = BakaShrot_Names_ResolveTopic(TopicRef)
|
||||
If TopicId < 0
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "Unknown topic '" + Left(TopicRef, 80) + "'")
|
||||
EndIf
|
||||
EndIf
|
||||
If Not NameSetHas(@*Client\PushTopics, TopicId, BakaShrot_Names_TopicName(TopicId))
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "Not allowed to push to topic '" + BakaShrot_Names_TopicRef(TopicId) + "'")
|
||||
EndIf
|
||||
|
||||
MaxSize = *Client\Server\Config\Server\MaxAttachmentSize
|
||||
If Size > MaxSize
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The attachment is too big: " + Str(Size) + " bytes, at most " + Str(MaxSize) + " here")
|
||||
EndIf
|
||||
|
||||
BlobId = CreateStorageBlob(TopicId, *Client\Metadata\NodeId, Size, @Sha256(0), @StoreError)
|
||||
If Not BlobId
|
||||
PrintN("[Net] Failed to start an upload for client " + Str(*Client\ClientId) + ": " + StoreError\s)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The upload couldn't be started")
|
||||
EndIf
|
||||
|
||||
Started\UploadId = BlobId
|
||||
Started\ChunkSize = #BakaShrot_AttachmentChunkSize
|
||||
*Answer = BakaShrot_UploadStartedPacket_Encode(@Started, @AnswerSize)
|
||||
If Not *Answer
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
Sent = BakaShrot_SendPacket(*Client\ClientId, *Answer, AnswerSize\i, @*Client\Metadata\Cipher)
|
||||
FreeMemory(*Answer)
|
||||
ProcedureReturn Sent
|
||||
EndProcedure
|
||||
|
||||
|
||||
; Stores the next part of one of this client's uploads. Once all of it is
|
||||
; here, checks its SHA-256: complete, or dropped and refused.
|
||||
; Returns #False if the client must be aborted.
|
||||
Procedure.i BakaShrot_NetClient_HandleUploadChunk(*Client.BakaShrot_NetClient, *Packet, PacketSize.i)
|
||||
Protected *View.MsgPackData, *Chunk.BakaShrot_UploadChunk
|
||||
Protected Blob.BakaShrot_StorageBlob, Received.q, StoreError.String, Expected.i
|
||||
|
||||
*Chunk = 0
|
||||
*View = MsgPackWrapMsgBuffer(*Packet, PacketSize)
|
||||
If *View
|
||||
*Chunk = BakaShrot_UploadChunkPacket_Decode(*View)
|
||||
MsgPackFree(*View, #False)
|
||||
EndIf
|
||||
If Not *Chunk
|
||||
PrintN("[Net] Client " + Str(*Client\ClientId) + " sent an invalid upload chunk")
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
|
||||
; Only its own uploads: same node, a topic it may still push to.
|
||||
If Not GetStorageBlob(*Client\DbId, *Chunk\UploadId, @Blob) Or Blob\NodeId <> *Client\Metadata\NodeId Or Blob\State <> #BakaShrot_Blob_Receiving
|
||||
BakaShrot_UploadChunkPacket_Free(*Chunk)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "No such upload in progress")
|
||||
EndIf
|
||||
If Not *Client\Permissions\Write Or Not NameSetHas(@*Client\PushTopics, Blob\TopicId, BakaShrot_Names_TopicName(Blob\TopicId))
|
||||
BakaShrot_UploadChunkPacket_Free(*Chunk)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "Not allowed to push to topic '" + BakaShrot_Names_TopicRef(Blob\TopicId) + "'")
|
||||
EndIf
|
||||
Expected = BakaShrot_AttachmentChunkBytes(Blob\Size, *Chunk\ChunkIndex)
|
||||
If *Chunk\ChunkIndex * #BakaShrot_AttachmentChunkSize <> Blob\Received Or *Chunk\DataSize <> Expected
|
||||
BakaShrot_UploadChunkPacket_Free(*Chunk)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "Expected part " + Str(Blob\Received / #BakaShrot_AttachmentChunkSize) + " of " + Str(BakaShrot_AttachmentChunkCount(Blob\Size)) + ", " + Str(BakaShrot_AttachmentChunkBytes(Blob\Size, Blob\Received / #BakaShrot_AttachmentChunkSize)) + " bytes")
|
||||
EndIf
|
||||
|
||||
Received = StoreStorageBlobChunk(Blob\BlobId, *Chunk\ChunkIndex, *Chunk\Data, *Chunk\DataSize, @StoreError)
|
||||
BakaShrot_UploadChunkPacket_Free(*Chunk)
|
||||
If Received < 0
|
||||
PrintN("[Net] Failed to store a part of an upload from client " + Str(*Client\ClientId) + ": " + StoreError\s)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The part couldn't be stored")
|
||||
EndIf
|
||||
|
||||
If Received < Blob\Size
|
||||
ProcedureReturn BakaShrot_NetClient_SendChunkStored(*Client, Received, #False)
|
||||
EndIf
|
||||
|
||||
; All of it: the SHA-256 given at the start must match.
|
||||
If Not HashStorageBlob(*Client\DbId, Blob\BlobId, Blob\Size, @Blob\Sha256[0])
|
||||
SetStorageBlobState(Blob\BlobId, #BakaShrot_Blob_Deleted)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The upload doesn't match its SHA-256, it's dropped")
|
||||
EndIf
|
||||
If Not SetStorageBlobState(Blob\BlobId, #BakaShrot_Blob_Complete)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The upload couldn't be stored")
|
||||
EndIf
|
||||
ProcedureReturn BakaShrot_NetClient_SendChunkStored(*Client, Received, #True)
|
||||
EndProcedure
|
||||
|
||||
|
||||
; Answers with a part of a message's attachment, same permissions as
|
||||
; reading the message. Refuses one that isn't all here yet.
|
||||
; Returns #False if the client must be aborted.
|
||||
Procedure.i BakaShrot_NetClient_HandleGetAttachment(*Client.BakaShrot_NetClient, *Packet, PacketSize.i)
|
||||
Protected *View.MsgPackData, *Request.BakaShrot_GetAttachment
|
||||
Protected Chunk.BakaShrot_AttachmentChunk, Attachment.BakaShrot_StorageAttachment
|
||||
Protected TopicId.i, NodeId.i, TopicRef.s, NodeRef.s, MessageId.q, Index.q, DataSize.Integer
|
||||
Protected *Answer, AnswerSize.Integer, Sent.i
|
||||
|
||||
*Request = 0
|
||||
*View = MsgPackWrapMsgBuffer(*Packet, PacketSize)
|
||||
If *View
|
||||
*Request = BakaShrot_GetAttachmentPacket_Decode(*View)
|
||||
MsgPackFree(*View, #False)
|
||||
EndIf
|
||||
If Not *Request
|
||||
PrintN("[Net] Client " + Str(*Client\ClientId) + " sent an invalid attachment request")
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
TopicRef = *Request\Topic
|
||||
NodeRef = *Request\Node
|
||||
MessageId = *Request\MessageId
|
||||
Index = *Request\ChunkIndex
|
||||
FreeStructure(*Request)
|
||||
|
||||
If Not *Client\Permissions\Read
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "Not allowed to read")
|
||||
EndIf
|
||||
TopicId = BakaShrot_Names_ResolveTopic(TopicRef)
|
||||
If TopicId < 0
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "Unknown topic '" + Left(TopicRef, 80) + "'")
|
||||
EndIf
|
||||
If Not NameSetHas(@*Client\ReadTopics, TopicId, BakaShrot_Names_TopicName(TopicId))
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "Not allowed to read topic '" + BakaShrot_Names_TopicRef(TopicId) + "'")
|
||||
EndIf
|
||||
NodeId = BakaShrot_Names_ResolveNode(NodeRef)
|
||||
If NodeId < 0
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "Unknown node '" + Left(NodeRef, 80) + "'")
|
||||
EndIf
|
||||
|
||||
If Not GetStorageAttachment(*Client\DbId, TopicId, NodeId, MessageId, @Attachment)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "No attachment for " + BakaShrot_Names_TopicRef(TopicId) + ":" + BakaShrot_Names_NodeRef(NodeId) + "#" + Str(MessageId))
|
||||
EndIf
|
||||
Select Attachment\State
|
||||
Case #BakaShrot_Attachment_NotKept
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The attachment isn't kept here: " + Str(Attachment\Size) + " bytes, more than MaxAttachmentSize")
|
||||
Case #BakaShrot_Attachment_Missing, #BakaShrot_Attachment_Receiving
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The attachment isn't here yet: " + Str(Attachment\Received) + " of " + Str(Attachment\Size) + " bytes")
|
||||
EndSelect
|
||||
If Index >= BakaShrot_AttachmentChunkCount(Attachment\Size)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The attachment has " + Str(BakaShrot_AttachmentChunkCount(Attachment\Size)) + " parts")
|
||||
EndIf
|
||||
|
||||
Chunk\Data = ReadStorageBlobChunk(*Client\DbId, Attachment\BlobId, Index, @DataSize)
|
||||
If Not Chunk\Data
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The attachment couldn't be read")
|
||||
EndIf
|
||||
Chunk\DataSize = DataSize\i
|
||||
Chunk\TopicId = TopicId
|
||||
Chunk\NodeId = NodeId
|
||||
Chunk\MessageId = MessageId
|
||||
Chunk\Size = Attachment\Size
|
||||
Chunk\ChunkIndex = Index
|
||||
CopyMemory(@Attachment\Sha256[0], @Chunk\Sha256[0], #BakaShrot_Sha256Size)
|
||||
*Answer = BakaShrot_AttachmentChunkPacket_Encode(@Chunk, @AnswerSize)
|
||||
FreeMemory(Chunk\Data)
|
||||
If Not *Answer
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
Sent = BakaShrot_SendPacket(*Client\ClientId, *Answer, AnswerSize\i, @*Client\Metadata\Cipher)
|
||||
FreeMemory(*Answer)
|
||||
ProcedureReturn Sent
|
||||
EndProcedure
|
||||
|
||||
|
||||
; Tells a peer which attachments this server is missing, among what it
|
||||
; takes from it (AcceptTopics, AcceptNodes): the peer sends what it has.
|
||||
; Returns #False if the client must be aborted.
|
||||
Procedure.i BakaShrot_NetClient_HandleMissingAttachmentsRequest(*Client.BakaShrot_NetClient, *Packet, PacketSize.i)
|
||||
Protected *View.MsgPackData, *Request.BakaShrot_MissingAttachmentsRequest
|
||||
Protected Answer.BakaShrot_MissingAttachments
|
||||
Protected NewList Missing.BakaShrot_StorageMissingAttachment()
|
||||
Protected *Answer, AnswerSize.Integer, Sent.i
|
||||
|
||||
*Request = 0
|
||||
*View = MsgPackWrapMsgBuffer(*Packet, PacketSize)
|
||||
If *View
|
||||
*Request = BakaShrot_MissingAttachmentsRequestPacket_Decode(*View)
|
||||
MsgPackFree(*View, #False)
|
||||
EndIf
|
||||
If Not *Request
|
||||
PrintN("[Net] Client " + Str(*Client\ClientId) + " sent an invalid missing attachments request")
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
FreeStructure(*Request)
|
||||
|
||||
If *Client\PeerName = ""
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "Only peers can replicate")
|
||||
EndIf
|
||||
|
||||
; More than one answer holds, some of them may be someone else's.
|
||||
If Not GetMissingStorageAttachments(*Client\DbId, 4 * #BakaShrot_MaxMissingAttachments, Missing())
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The missing attachments couldn't be read")
|
||||
EndIf
|
||||
ForEach Missing()
|
||||
If ListSize(Answer\Attachments()) >= #BakaShrot_MaxMissingAttachments
|
||||
Break
|
||||
EndIf
|
||||
If Not NameSetHas(@*Client\AcceptTopics, Missing()\TopicId, Missing()\Topic) Or Not NameSetHas(@*Client\AcceptNodes, Missing()\NodeId, Missing()\Node)
|
||||
Continue
|
||||
EndIf
|
||||
If Not BakaShrot_IsValidName(Missing()\Topic) Or Not BakaShrot_IsValidName(Missing()\Node)
|
||||
Continue
|
||||
EndIf
|
||||
AddElement(Answer\Attachments())
|
||||
Answer\Attachments()\Topic = Missing()\Topic
|
||||
Answer\Attachments()\Node = Missing()\Node
|
||||
Answer\Attachments()\MessageId = Missing()\MessageId
|
||||
Answer\Attachments()\NextChunk = Missing()\Received / #BakaShrot_AttachmentChunkSize
|
||||
Next
|
||||
|
||||
*Answer = BakaShrot_MissingAttachmentsPacket_Encode(@Answer, @AnswerSize)
|
||||
If Not *Answer
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
Sent = BakaShrot_SendPacket(*Client\ClientId, *Answer, AnswerSize\i, @*Client\Metadata\Cipher)
|
||||
FreeMemory(*Answer)
|
||||
ProcedureReturn Sent
|
||||
EndProcedure
|
||||
|
||||
|
||||
; Stores a part of an attachment a peer replicates, of a message this server
|
||||
; already has: in order, resuming where a previous link stopped. Once all of
|
||||
; it is here, checks its SHA-256 (the one its message came with): ready, or
|
||||
; dropped to be sent again.
|
||||
; Returns #False if the client must be aborted.
|
||||
Procedure.i BakaShrot_NetClient_HandleReplicateAttachmentChunk(*Client.BakaShrot_NetClient, *Packet, PacketSize.i)
|
||||
Protected *View.MsgPackData, *Chunk.BakaShrot_ReplicateAttachmentChunk
|
||||
Protected Attachment.BakaShrot_StorageAttachment
|
||||
Protected TopicId.i, NodeId.i, TopicRef.s, NodeRef.s, MessageId.q, BlobId.q, Received.q, IsValid.i, StoreError.String
|
||||
|
||||
*Chunk = 0
|
||||
*View = MsgPackWrapMsgBuffer(*Packet, PacketSize)
|
||||
If *View
|
||||
*Chunk = BakaShrot_ReplicateAttachmentChunkPacket_Decode(*View)
|
||||
MsgPackFree(*View, #False)
|
||||
EndIf
|
||||
If Not *Chunk
|
||||
PrintN("[Net] Client " + Str(*Client\ClientId) + " sent an invalid attachment chunk")
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
TopicRef = *Chunk\Topic
|
||||
NodeRef = *Chunk\Node
|
||||
MessageId = *Chunk\MessageId
|
||||
|
||||
If *Client\PeerName = ""
|
||||
BakaShrot_ReplicateAttachmentChunkPacket_Free(*Chunk)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "Only peers can replicate")
|
||||
EndIf
|
||||
TopicId = BakaShrot_Names_TopicByName(TopicRef)
|
||||
NodeId = BakaShrot_Names_NodeByName(NodeRef)
|
||||
If TopicId < 0 Or NodeId < 0 Or Not NameSetHas(@*Client\AcceptTopics, TopicId, TopicRef) Or Not NameSetHas(@*Client\AcceptNodes, NodeId, NodeRef)
|
||||
BakaShrot_ReplicateAttachmentChunkPacket_Free(*Chunk)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "'" + TopicRef + "':'" + NodeRef + "' isn't replicated here")
|
||||
EndIf
|
||||
|
||||
If Not GetStorageAttachment(*Client\DbId, TopicId, NodeId, MessageId, @Attachment)
|
||||
BakaShrot_ReplicateAttachmentChunkPacket_Free(*Chunk)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "No attachment expected for " + TopicRef + ":" + NodeRef + "#" + Str(MessageId))
|
||||
EndIf
|
||||
If Attachment\State = #BakaShrot_Attachment_Ready
|
||||
; Someone else's link was faster: done.
|
||||
BakaShrot_ReplicateAttachmentChunkPacket_Free(*Chunk)
|
||||
ProcedureReturn BakaShrot_NetClient_SendChunkStored(*Client, Attachment\Size, #True)
|
||||
EndIf
|
||||
If Attachment\State = #BakaShrot_Attachment_NotKept
|
||||
BakaShrot_ReplicateAttachmentChunkPacket_Free(*Chunk)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The attachment isn't kept here")
|
||||
EndIf
|
||||
If *Chunk\ChunkIndex * #BakaShrot_AttachmentChunkSize <> Attachment\Received Or *Chunk\DataSize <> BakaShrot_AttachmentChunkBytes(Attachment\Size, *Chunk\ChunkIndex)
|
||||
BakaShrot_ReplicateAttachmentChunkPacket_Free(*Chunk)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "Expected part " + Str(Attachment\Received / #BakaShrot_AttachmentChunkSize))
|
||||
EndIf
|
||||
|
||||
BlobId = Attachment\BlobId
|
||||
If Not BlobId
|
||||
BlobId = StartReplicatedAttachment(TopicId, NodeId, MessageId, @StoreError)
|
||||
EndIf
|
||||
If BlobId
|
||||
Received = StoreStorageBlobChunk(BlobId, *Chunk\ChunkIndex, *Chunk\Data, *Chunk\DataSize, @StoreError)
|
||||
Else
|
||||
Received = -1
|
||||
EndIf
|
||||
BakaShrot_ReplicateAttachmentChunkPacket_Free(*Chunk)
|
||||
If Received < 0
|
||||
PrintN("[Net] Failed to store an attachment's part from peer '" + *Client\PeerName + "': " + StoreError\s)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The part couldn't be stored")
|
||||
EndIf
|
||||
|
||||
If Received < Attachment\Size
|
||||
ProcedureReturn BakaShrot_NetClient_SendChunkStored(*Client, Received, #False)
|
||||
EndIf
|
||||
|
||||
IsValid = HashStorageBlob(*Client\DbId, BlobId, Attachment\Size, @Attachment\Sha256[0])
|
||||
If Not FinishReplicatedAttachment(TopicId, NodeId, MessageId, BlobId, IsValid)
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The attachment couldn't be stored")
|
||||
EndIf
|
||||
If Not IsValid
|
||||
PrintN("[Net] The attachment of " + TopicRef + ":" + NodeRef + "#" + Str(MessageId) + " from peer '" + *Client\PeerName + "' doesn't match its SHA-256, dropped")
|
||||
ProcedureReturn BakaShrot_NetClient_Refuse(*Client, "The attachment doesn't match its SHA-256, send it again")
|
||||
EndIf
|
||||
ProcedureReturn BakaShrot_NetClient_SendChunkStored(*Client, Received, #True)
|
||||
EndProcedure
|
||||
|
||||
|
||||
; Handles one request from an authenticated client.
|
||||
; Returns #False if the client must be aborted.
|
||||
@@ -1024,6 +1397,21 @@ Procedure.i BakaShrot_NetClient_HandleRequest(*Client.BakaShrot_NetClient, *Pack
|
||||
|
||||
Case #BakaShrot_PacketType_ReplicateMessages
|
||||
ProcedureReturn BakaShrot_NetClient_HandleReplicateMessages(*Client, *Packet, PacketSize)
|
||||
|
||||
Case #BakaShrot_PacketType_UploadStart
|
||||
ProcedureReturn BakaShrot_NetClient_HandleUploadStart(*Client, *Packet, PacketSize)
|
||||
|
||||
Case #BakaShrot_PacketType_UploadChunk
|
||||
ProcedureReturn BakaShrot_NetClient_HandleUploadChunk(*Client, *Packet, PacketSize)
|
||||
|
||||
Case #BakaShrot_PacketType_GetAttachment
|
||||
ProcedureReturn BakaShrot_NetClient_HandleGetAttachment(*Client, *Packet, PacketSize)
|
||||
|
||||
Case #BakaShrot_PacketType_MissingAttachmentsRequest
|
||||
ProcedureReturn BakaShrot_NetClient_HandleMissingAttachmentsRequest(*Client, *Packet, PacketSize)
|
||||
|
||||
Case #BakaShrot_PacketType_ReplicateAttachmentChunk
|
||||
ProcedureReturn BakaShrot_NetClient_HandleReplicateAttachmentChunk(*Client, *Packet, PacketSize)
|
||||
EndSelect
|
||||
|
||||
PrintN("[Net] Client " + Str(*Client\ClientId) + " sent an unexpected packet")
|
||||
|
||||
Reference in New Issue
Block a user