1
0
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:
2026-11-08 18:56:15 +01:00
parent 9caf816dca
commit df61ef9719
42 changed files with 4920 additions and 106 deletions
+16
View File
@@ -31,6 +31,10 @@
; C->S QueryRequest -> S->C QueryResult or RequestRefused
; C->S ResolveName -> S->C NameResolved or RequestRefused
; C->S ListNames -> S->C NamesList
; C->S UploadStart -> S->C UploadStarted or RequestRefused
; C->S UploadChunk -> S->C ChunkStored or RequestRefused
; then a PushMessage with the UploadId attaches it to the message
; C->S GetAttachment -> S->C AttachmentChunk or RequestRefused
; Topics and nodes are given by name or id, see Names.pbi.
; A refusal keeps the connection open, a malformed or unexpected packet
; still aborts it.
@@ -39,6 +43,9 @@
; link it opened to the server:
; P->S SyncStateRequest -> S->P SyncState or RequestRefused
; P->S ReplicateMessages -> S->P MessagePushed (last id) or RequestRefused
; once the messages are caught up:
; P->S MissingAttachmentsRequest -> S->P MissingAttachments or RequestRefused
; P->S ReplicateAttachmentChunk -> S->P ChunkStored or RequestRefused
;
;
; Framing: every packet is sent as its size (4 bytes, little-endian),
@@ -73,6 +80,15 @@ XIncludeFile "Packets/ListNamesPacket.pbi"
XIncludeFile "Packets/NamesListPacket.pbi"
XIncludeFile "Packets/GetMessagesPacket.pbi"
XIncludeFile "Packets/MessagesPacket.pbi"
XIncludeFile "Packets/UploadStartPacket.pbi"
XIncludeFile "Packets/UploadStartedPacket.pbi"
XIncludeFile "Packets/UploadChunkPacket.pbi"
XIncludeFile "Packets/ChunkStoredPacket.pbi"
XIncludeFile "Packets/GetAttachmentPacket.pbi"
XIncludeFile "Packets/AttachmentChunkPacket.pbi"
XIncludeFile "Packets/MissingAttachmentsRequestPacket.pbi"
XIncludeFile "Packets/MissingAttachmentsPacket.pbi"
XIncludeFile "Packets/ReplicateAttachmentChunkPacket.pbi"
; Reads the type of a received packet, to know which decoder to use.
+42 -1
View File
@@ -20,7 +20,8 @@ EndStructure
; 3: the Hello sealed for the server, a key per connection, everything after
; the ChallengeRequest encrypted (Connections.pbi). Messages carry when
; they were pushed (PushedAt).
#BakaShrot_ProtocolVersion = 3
; 4: attachments (Design/Attachments.md).
#BakaShrot_ProtocolVersion = 4
; Size in bytes of the nonce sent in a challenge request and echoed in its response.
; It MUST be filled with CryptRandomData(), never reused, and be checked against the one the
@@ -50,6 +51,32 @@ Procedure.i BakaShrot_IsValidTimestamp(Timestamp.q)
ProcedureReturn Bool(Timestamp >= 1 And Timestamp <= #BakaShrot_MaxTimestamp)
EndProcedure
; Attachments (Design/Attachments.md): at most one per message, sent and
; stored in parts of #BakaShrot_AttachmentChunkSize bytes (the last one what's
; left), checked with their SHA-256. #BakaShrot_MaxAttachmentSize is the
; protocol's limit, each server sets its own ([BakaShrot.Server]
; MaxAttachmentSize).
#BakaShrot_AttachmentChunkSize = 1048576
#BakaShrot_MaxAttachmentSize = 1099511627776
#BakaShrot_MaxAttachmentChunks = #BakaShrot_MaxAttachmentSize / #BakaShrot_AttachmentChunkSize
#BakaShrot_Sha256Size = 32
; How many parts Size bytes take.
Procedure.q BakaShrot_AttachmentChunkCount(Size.q)
ProcedureReturn (Size + #BakaShrot_AttachmentChunkSize - 1) / #BakaShrot_AttachmentChunkSize
EndProcedure
; How big part Index of Size bytes is, 0 if there's no such part.
Procedure.i BakaShrot_AttachmentChunkBytes(Size.q, Index.q)
Protected Left.q = Size - Index * #BakaShrot_AttachmentChunkSize
If Index < 0 Or Left <= 0
ProcedureReturn 0
ElseIf Left > #BakaShrot_AttachmentChunkSize
ProcedureReturn #BakaShrot_AttachmentChunkSize
EndIf
ProcedureReturn Left
EndProcedure
; Biggest packet accepted on the wire once authenticated, anything bigger
; aborts the connection. Before that, only #BakaShrot_MaxHandshakePacketSize
; (Connections.pbi).
@@ -97,6 +124,20 @@ Enumeration BakaShrot_PacketTypes
; A run of messages of a topic/node, only accepted once authenticated.
#BakaShrot_PacketType_GetMessages = 22
#BakaShrot_PacketType_Messages = 23
; Attachments, only accepted once authenticated: uploading one (before
; its PushMessage), and reading one, in parts.
#BakaShrot_PacketType_UploadStart = 24
#BakaShrot_PacketType_UploadStarted = 25
#BakaShrot_PacketType_UploadChunk = 26
#BakaShrot_PacketType_ChunkStored = 27
#BakaShrot_PacketType_GetAttachment = 28
#BakaShrot_PacketType_AttachmentChunk = 29
; Replicating attachments, only accepted from peers, once their messages are.
#BakaShrot_PacketType_MissingAttachmentsRequest = 30
#BakaShrot_PacketType_MissingAttachments = 31
#BakaShrot_PacketType_ReplicateAttachmentChunk = 32
EndEnumeration
@@ -0,0 +1,195 @@
; ============================================================================
; ...
; ============================================================================
XIncludeFile "../Packets.pbi"
; S2C, answers a GetAttachment: part ChunkIndex of the attachment of
; TopicId:NodeId#MessageId (the server's ids), Size bytes in all, in parts of
; #BakaShrot_AttachmentChunkSize, whose SHA-256 is Sha256.
Structure BakaShrot_AttachmentChunk Extends BakaShrot_PacketBase
TopicId.l
NodeId.l
MessageId.q
Size.q
Sha256.a[#BakaShrot_Sha256Size]
ChunkIndex.l
*Data
DataSize.l
EndStructure
; Encodes *Chunk into a freshly allocated buffer, its base fields are set by this procedure.
; *OutSize receives its size in bytes.
; Returns 0 on failure, the caller owns the returned buffer - free it with FreeMemory.
Procedure.i BakaShrot_AttachmentChunkPacket_Encode(*Chunk.BakaShrot_AttachmentChunk, *OutSize.Integer)
Protected *MsgPackData.MsgPackData, *Buffer
*Chunk\Magic = #BakaShrot_ProtocolMagic
*Chunk\Version = #BakaShrot_ProtocolVersion
*Chunk\PacketType = #BakaShrot_PacketType_AttachmentChunk
If *Chunk\TopicId < 0 Or *Chunk\TopicId > #BakaShrot_MaxId Or *Chunk\NodeId < 0 Or *Chunk\NodeId > #BakaShrot_MaxId Or *Chunk\MessageId < 1 Or *Chunk\Size < 1 Or *Chunk\Size > #BakaShrot_MaxAttachmentSize Or *Chunk\ChunkIndex < 0 Or *Chunk\ChunkIndex > #BakaShrot_MaxAttachmentChunks Or Not *Chunk\Data Or *Chunk\DataSize < 1 Or *Chunk\DataSize > #BakaShrot_AttachmentChunkSize
ProcedureReturn 0
EndIf
*MsgPackData = MsgPackCreate(#BakaShrot_AttachmentChunkSize + 256)
If Not *MsgPackData
ProcedureReturn 0
EndIf
If Not BakaShrot_WritePacketBase(*Chunk, *MsgPackData)
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Chunk\TopicId) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Chunk\NodeId) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Chunk\MessageId) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Chunk\Size) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteBinary(*MsgPackData, @*Chunk\Sha256[0], #BakaShrot_Sha256Size) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Chunk\ChunkIndex) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteBinary(*MsgPackData, *Chunk\Data, *Chunk\DataSize) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
; Frees *MsgPackData on success, leaves it untouched on failure.
*Buffer = MsgPackFinalize(*MsgPackData)
If Not *Buffer
Goto EncodeFailed
EndIf
*OutSize\i = MemorySize(*Buffer)
ProcedureReturn *Buffer
EncodeFailed:
MsgPackFree(*MsgPackData, #True)
ProcedureReturn 0
EndProcedure
; Frees a decoded *Chunk and the data it holds.
Procedure BakaShrot_AttachmentChunkPacket_Free(*Chunk.BakaShrot_AttachmentChunk)
If Not *Chunk
ProcedureReturn
EndIf
If *Chunk\Data
FreeMemory(*Chunk\Data)
EndIf
FreeStructure(*Chunk)
EndProcedure
; Decodes the packet at *MsgPackData's current offset into a freshly allocated struct.
; *MsgPackData stays owned by the caller, and should be discarded on failure.
; Returns 0 on failure, the caller owns the returned struct - free it with BakaShrot_AttachmentChunkPacket_Free().
Procedure.i BakaShrot_AttachmentChunkPacket_Decode(*MsgPackData.MsgPackData)
Protected *Chunk.BakaShrot_AttachmentChunk, Value.q, ReadSize.q
*Chunk = AllocateStructure(BakaShrot_AttachmentChunk)
If Not *Chunk
ProcedureReturn 0
EndIf
; The base parser doesn't check which packet it is, that's on us.
If Not BakaShrot_ReadPacketBase(*Chunk, *MsgPackData) Or *Chunk\PacketType <> #BakaShrot_PacketType_AttachmentChunk
Goto DecodeFailed
EndIf
; TopicId
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 0 Or Value > #BakaShrot_MaxId
Goto DecodeFailed
EndIf
*Chunk\TopicId = Value
; NodeId
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 0 Or Value > #BakaShrot_MaxId
Goto DecodeFailed
EndIf
*Chunk\NodeId = Value
; MessageId
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 1
Goto DecodeFailed
EndIf
*Chunk\MessageId = Value
; Size
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 1 Or Value > #BakaShrot_MaxAttachmentSize
Goto DecodeFailed
EndIf
*Chunk\Size = Value
; Sha256
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Binary
Goto DecodeFailed
EndIf
; Anything bigger than the struct's array is rejected by MsgPackReadBinary, anything smaller by the size check.
ReadSize = MsgPackReadBinary(*MsgPackData, @*Chunk\Sha256[0], #BakaShrot_Sha256Size)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or ReadSize <> #BakaShrot_Sha256Size
Goto DecodeFailed
EndIf
; ChunkIndex
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 0 Or Value > #BakaShrot_MaxAttachmentChunks
Goto DecodeFailed
EndIf
*Chunk\ChunkIndex = Value
; Data
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Binary
Goto DecodeFailed
EndIf
; A copy, freed with the packet (_Free()).
*Chunk\Data = MsgPackCopyBinary(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Not *Chunk\Data
Goto DecodeFailed
EndIf
*Chunk\DataSize = MemorySize(*Chunk\Data)
If *Chunk\DataSize > #BakaShrot_AttachmentChunkSize
Goto DecodeFailed
EndIf
ProcedureReturn *Chunk
; Everything read so far is tainted, drop it.
DecodeFailed:
BakaShrot_AttachmentChunkPacket_Free(*Chunk)
ProcedureReturn 0
EndProcedure
@@ -0,0 +1,104 @@
; ============================================================================
; ...
; ============================================================================
XIncludeFile "../Packets.pbi"
; S2C (and S2P), answers an UploadChunk (or a ReplicateAttachmentChunk): Received
; bytes are stored so far. Complete is 1 once they all are, and their SHA-256
; checked out.
Structure BakaShrot_ChunkStored Extends BakaShrot_PacketBase
Received.q
Complete.l
EndStructure
; Encodes *Stored into a freshly allocated buffer, its base fields are set by this procedure.
; *OutSize receives its size in bytes.
; Returns 0 on failure, the caller owns the returned buffer - free it with FreeMemory.
Procedure.i BakaShrot_ChunkStoredPacket_Encode(*Stored.BakaShrot_ChunkStored, *OutSize.Integer)
Protected *MsgPackData.MsgPackData, *Buffer
*Stored\Magic = #BakaShrot_ProtocolMagic
*Stored\Version = #BakaShrot_ProtocolVersion
*Stored\PacketType = #BakaShrot_PacketType_ChunkStored
If *Stored\Received < 0 Or *Stored\Received > #BakaShrot_MaxAttachmentSize Or *Stored\Complete < 0 Or *Stored\Complete > 1
ProcedureReturn 0
EndIf
*MsgPackData = MsgPackCreate(64)
If Not *MsgPackData
ProcedureReturn 0
EndIf
If Not BakaShrot_WritePacketBase(*Stored, *MsgPackData)
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Stored\Received) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Stored\Complete) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
; Frees *MsgPackData on success, leaves it untouched on failure.
*Buffer = MsgPackFinalize(*MsgPackData)
If Not *Buffer
Goto EncodeFailed
EndIf
*OutSize\i = MemorySize(*Buffer)
ProcedureReturn *Buffer
EncodeFailed:
MsgPackFree(*MsgPackData, #True)
ProcedureReturn 0
EndProcedure
; Decodes the packet at *MsgPackData's current offset into a freshly allocated struct.
; *MsgPackData stays owned by the caller, and should be discarded on failure.
; Returns 0 on failure, the caller owns the returned struct - free it with FreeStructure.
Procedure.i BakaShrot_ChunkStoredPacket_Decode(*MsgPackData.MsgPackData)
Protected *Stored.BakaShrot_ChunkStored, Value.q
*Stored = AllocateStructure(BakaShrot_ChunkStored)
If Not *Stored
ProcedureReturn 0
EndIf
; The base parser doesn't check which packet it is, that's on us.
If Not BakaShrot_ReadPacketBase(*Stored, *MsgPackData) Or *Stored\PacketType <> #BakaShrot_PacketType_ChunkStored
Goto DecodeFailed
EndIf
; Received
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 0 Or Value > #BakaShrot_MaxAttachmentSize
Goto DecodeFailed
EndIf
*Stored\Received = Value
; Complete
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 0 Or Value > 1
Goto DecodeFailed
EndIf
*Stored\Complete = Value
ProcedureReturn *Stored
; Everything read so far is tainted, drop it.
DecodeFailed:
FreeStructure(*Stored)
ProcedureReturn 0
EndProcedure
@@ -0,0 +1,134 @@
; ============================================================================
; ...
; ============================================================================
XIncludeFile "../Packets.pbi"
; C2S, asks for part ChunkIndex (0 for the first) of the attachment of a message.
; Topic, Node: each a name or an id in digits, as GetMessages'. ReadTopics must
; allow the topic.
; Answered with an AttachmentChunk, or a RequestRefused (no such message, no
; attachment, or not here yet: replicated attachments come after their message).
Structure BakaShrot_GetAttachment Extends BakaShrot_PacketBase
Topic.s
Node.s
MessageId.q
ChunkIndex.l
EndStructure
; Encodes *Request into a freshly allocated buffer, its base fields are set by this procedure.
; *OutSize receives its size in bytes.
; Returns 0 on failure, the caller owns the returned buffer - free it with FreeMemory.
Procedure.i BakaShrot_GetAttachmentPacket_Encode(*Request.BakaShrot_GetAttachment, *OutSize.Integer)
Protected *MsgPackData.MsgPackData, *Buffer
*Request\Magic = #BakaShrot_ProtocolMagic
*Request\Version = #BakaShrot_ProtocolVersion
*Request\PacketType = #BakaShrot_PacketType_GetAttachment
If *Request\MessageId < 1 Or *Request\ChunkIndex < 0 Or *Request\ChunkIndex > #BakaShrot_MaxAttachmentChunks
ProcedureReturn 0
EndIf
*MsgPackData = MsgPackCreate(64)
If Not *MsgPackData
ProcedureReturn 0
EndIf
If Not BakaShrot_WritePacketBase(*Request, *MsgPackData)
Goto EncodeFailed
EndIf
If MsgPackWriteString(*MsgPackData, *Request\Topic) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteString(*MsgPackData, *Request\Node) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Request\MessageId) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Request\ChunkIndex) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
; Frees *MsgPackData on success, leaves it untouched on failure.
*Buffer = MsgPackFinalize(*MsgPackData)
If Not *Buffer
Goto EncodeFailed
EndIf
*OutSize\i = MemorySize(*Buffer)
ProcedureReturn *Buffer
EncodeFailed:
MsgPackFree(*MsgPackData, #True)
ProcedureReturn 0
EndProcedure
; Decodes the packet at *MsgPackData's current offset into a freshly allocated struct.
; *MsgPackData stays owned by the caller, and should be discarded on failure.
; Returns 0 on failure, the caller owns the returned struct - free it with FreeStructure.
Procedure.i BakaShrot_GetAttachmentPacket_Decode(*MsgPackData.MsgPackData)
Protected *Request.BakaShrot_GetAttachment, Value.q
*Request = AllocateStructure(BakaShrot_GetAttachment)
If Not *Request
ProcedureReturn 0
EndIf
; The base parser doesn't check which packet it is, that's on us.
If Not BakaShrot_ReadPacketBase(*Request, *MsgPackData) Or *Request\PacketType <> #BakaShrot_PacketType_GetAttachment
Goto DecodeFailed
EndIf
; Topic
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_String
Goto DecodeFailed
EndIf
*Request\Topic = MsgPackReadString(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success
Goto DecodeFailed
EndIf
; Node
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_String
Goto DecodeFailed
EndIf
*Request\Node = MsgPackReadString(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success
Goto DecodeFailed
EndIf
; MessageId
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 1
Goto DecodeFailed
EndIf
*Request\MessageId = Value
; ChunkIndex
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 0 Or Value > #BakaShrot_MaxAttachmentChunks
Goto DecodeFailed
EndIf
*Request\ChunkIndex = Value
ProcedureReturn *Request
; Everything read so far is tainted, drop it.
DecodeFailed:
FreeStructure(*Request)
ProcedureReturn 0
EndProcedure
@@ -17,6 +17,10 @@ Structure BakaShrot_LatestMessage Extends BakaShrot_PacketBase
MessageId.q
PushedAt.q
JsonData.s
; Its attachment's size in bytes, 0 for none, and whether this server has
; all of it yet (a replicated one comes after its message).
AttachmentSize.q
AttachmentReady.l
EndStructure
@@ -65,6 +69,12 @@ Procedure.i BakaShrot_LatestMessagePacket_Encode(*Latest.BakaShrot_LatestMessage
If MsgPackWriteString(*MsgPackData, *Latest\JsonData) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Latest\AttachmentSize) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Latest\AttachmentReady) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
; Frees *MsgPackData on success, leaves it untouched on failure.
*Buffer = MsgPackFinalize(*MsgPackData)
@@ -161,6 +171,23 @@ Procedure.i BakaShrot_LatestMessagePacket_Decode(*MsgPackData.MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success
Goto DecodeFailed
EndIf
; AttachmentSize
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
*Latest\AttachmentSize = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or *Latest\AttachmentSize < 0 Or *Latest\AttachmentSize > #BakaShrot_MaxAttachmentSize
Goto DecodeFailed
EndIf
; AttachmentReady
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
*Latest\AttachmentReady = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or *Latest\AttachmentReady < 0 Or *Latest\AttachmentReady > 1
Goto DecodeFailed
EndIf
ProcedureReturn *Latest
+28 -1
View File
@@ -11,6 +11,10 @@ Structure BakaShrot_MessagesEntry
MessageId.q
; When it was pushed, see Packets.pbi.
PushedAt.q
; Its attachment's size in bytes, 0 for none, and whether this server has
; all of it yet (a replicated one comes after its message).
AttachmentSize.q
AttachmentReady.l
JsonData.s
EndStructure
@@ -21,7 +25,7 @@ EndStructure
; Encoded size of the answer's own fields, and of each message on top of its
; JSON, rounded up. Used to stop an answer before it gets over #BakaShrot_MaxPacketSize.
#BakaShrot_MessagesOverhead = 64 + 2 * (#BakaShrot_MaxNameLength + 2)
#BakaShrot_MessagesMessageOverhead = 25
#BakaShrot_MessagesMessageOverhead = 40
; S2C, answers a GetMessages with the messages of that topic/node combination
; stored after the asked id, in message id order: as many as were asked for,
@@ -99,6 +103,12 @@ Procedure.i BakaShrot_MessagesPacket_Encode(*Answer.BakaShrot_Messages, *OutSize
If MsgPackWriteString(*MsgPackData, *Answer\Messages()\JsonData) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Answer\Messages()\AttachmentSize) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Answer\Messages()\AttachmentReady) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
Next
; Frees *MsgPackData on success, leaves it untouched on failure.
@@ -222,6 +232,23 @@ Procedure.i BakaShrot_MessagesPacket_Decode(*MsgPackData.MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success
Goto DecodeFailed
EndIf
; AttachmentSize
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
*Answer\Messages()\AttachmentSize = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or *Answer\Messages()\AttachmentSize < 0 Or *Answer\Messages()\AttachmentSize > #BakaShrot_MaxAttachmentSize
Goto DecodeFailed
EndIf
; AttachmentReady
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
*Answer\Messages()\AttachmentReady = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or *Answer\Messages()\AttachmentReady < 0 Or *Answer\Messages()\AttachmentReady > 1
Goto DecodeFailed
EndIf
Next
ProcedureReturn *Answer
@@ -0,0 +1,156 @@
; ============================================================================
; ...
; ============================================================================
XIncludeFile "../Packets.pbi"
; Most attachments one MissingAttachments lists, the next check-in asks again.
#BakaShrot_MaxMissingAttachments = 256
; One attachment the server is missing: of Topic:Node#MessageId (names, never
; digits), from part NextChunk on (0 if it has none of it yet).
Structure BakaShrot_MissingAttachment
Topic.s
Node.s
MessageId.q
NextChunk.l
EndStructure
; S2P, answers a MissingAttachmentsRequest: attachments of messages the server
; has, that it takes from this peer, and doesn't have all of yet. Maybe none.
; On the wire: the count, then each one's topic, node, message id and next chunk.
Structure BakaShrot_MissingAttachments Extends BakaShrot_PacketBase
List Attachments.BakaShrot_MissingAttachment()
EndStructure
; Encodes *Answer into a freshly allocated buffer, its base fields are set by this procedure.
; *OutSize receives its size in bytes.
; Returns 0 on failure, the caller owns the returned buffer - free it with FreeMemory.
Procedure.i BakaShrot_MissingAttachmentsPacket_Encode(*Answer.BakaShrot_MissingAttachments, *OutSize.Integer)
Protected *MsgPackData.MsgPackData, *Buffer
*Answer\Magic = #BakaShrot_ProtocolMagic
*Answer\Version = #BakaShrot_ProtocolVersion
*Answer\PacketType = #BakaShrot_PacketType_MissingAttachments
If ListSize(*Answer\Attachments()) > #BakaShrot_MaxMissingAttachments
ProcedureReturn 0
EndIf
*MsgPackData = MsgPackCreate(256)
If Not *MsgPackData
ProcedureReturn 0
EndIf
If Not BakaShrot_WritePacketBase(*Answer, *MsgPackData)
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, ListSize(*Answer\Attachments())) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
ForEach *Answer\Attachments()
If MsgPackWriteString(*MsgPackData, *Answer\Attachments()\Topic) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteString(*MsgPackData, *Answer\Attachments()\Node) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Answer\Attachments()\MessageId) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Answer\Attachments()\NextChunk) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
Next
; Frees *MsgPackData on success, leaves it untouched on failure.
*Buffer = MsgPackFinalize(*MsgPackData)
If Not *Buffer
Goto EncodeFailed
EndIf
*OutSize\i = MemorySize(*Buffer)
ProcedureReturn *Buffer
EncodeFailed:
MsgPackFree(*MsgPackData, #True)
ProcedureReturn 0
EndProcedure
; Decodes the packet at *MsgPackData's current offset into a freshly allocated struct.
; *MsgPackData stays owned by the caller, and should be discarded on failure.
; Returns 0 on failure, the caller owns the returned struct - free it with FreeStructure.
Procedure.i BakaShrot_MissingAttachmentsPacket_Decode(*MsgPackData.MsgPackData)
Protected *Answer.BakaShrot_MissingAttachments
Protected Value.q, Count.q, i.q
*Answer = AllocateStructure(BakaShrot_MissingAttachments)
If Not *Answer
ProcedureReturn 0
EndIf
; The base parser doesn't check which packet it is, that's on us.
If Not BakaShrot_ReadPacketBase(*Answer, *MsgPackData) Or *Answer\PacketType <> #BakaShrot_PacketType_MissingAttachments
Goto DecodeFailed
EndIf
; Count
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Count = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Count < 0 Or Count > #BakaShrot_MaxMissingAttachments
Goto DecodeFailed
EndIf
For i = 1 To Count
AddElement(*Answer\Attachments())
; Topic
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_String
Goto DecodeFailed
EndIf
*Answer\Attachments()\Topic = MsgPackReadString(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Not BakaShrot_IsValidName(*Answer\Attachments()\Topic)
Goto DecodeFailed
EndIf
; Node
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_String
Goto DecodeFailed
EndIf
*Answer\Attachments()\Node = MsgPackReadString(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Not BakaShrot_IsValidName(*Answer\Attachments()\Node)
Goto DecodeFailed
EndIf
; MessageId
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
*Answer\Attachments()\MessageId = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or *Answer\Attachments()\MessageId < 1
Goto DecodeFailed
EndIf
; NextChunk
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 0 Or Value > #BakaShrot_MaxAttachmentChunks
Goto DecodeFailed
EndIf
*Answer\Attachments()\NextChunk = Value
Next
ProcedureReturn *Answer
; Everything read so far is tainted, drop it.
DecodeFailed:
FreeStructure(*Answer)
ProcedureReturn 0
EndProcedure
@@ -0,0 +1,71 @@
; ============================================================================
; ...
; ============================================================================
XIncludeFile "../Packets.pbi"
; P2S, asks which attachments the server is still missing from this peer, once
; the messages are caught up. Only the base fields.
; Answered with a MissingAttachments, or a RequestRefused.
Structure BakaShrot_MissingAttachmentsRequest Extends BakaShrot_PacketBase
EndStructure
; Encodes *Request into a freshly allocated buffer, its base fields are set by this procedure.
; *OutSize receives its size in bytes.
; Returns 0 on failure, the caller owns the returned buffer - free it with FreeMemory.
Procedure.i BakaShrot_MissingAttachmentsRequestPacket_Encode(*Request.BakaShrot_MissingAttachmentsRequest, *OutSize.Integer)
Protected *MsgPackData.MsgPackData, *Buffer
*Request\Magic = #BakaShrot_ProtocolMagic
*Request\Version = #BakaShrot_ProtocolVersion
*Request\PacketType = #BakaShrot_PacketType_MissingAttachmentsRequest
*MsgPackData = MsgPackCreate(64)
If Not *MsgPackData
ProcedureReturn 0
EndIf
If Not BakaShrot_WritePacketBase(*Request, *MsgPackData)
Goto EncodeFailed
EndIf
; Frees *MsgPackData on success, leaves it untouched on failure.
*Buffer = MsgPackFinalize(*MsgPackData)
If Not *Buffer
Goto EncodeFailed
EndIf
*OutSize\i = MemorySize(*Buffer)
ProcedureReturn *Buffer
EncodeFailed:
MsgPackFree(*MsgPackData, #True)
ProcedureReturn 0
EndProcedure
; Decodes the packet at *MsgPackData's current offset into a freshly allocated struct.
; *MsgPackData stays owned by the caller, and should be discarded on failure.
; Returns 0 on failure, the caller owns the returned struct - free it with FreeStructure.
Procedure.i BakaShrot_MissingAttachmentsRequestPacket_Decode(*MsgPackData.MsgPackData)
Protected *Request.BakaShrot_MissingAttachmentsRequest
*Request = AllocateStructure(BakaShrot_MissingAttachmentsRequest)
If Not *Request
ProcedureReturn 0
EndIf
; The base parser doesn't check which packet it is, that's on us.
If Not BakaShrot_ReadPacketBase(*Request, *MsgPackData) Or *Request\PacketType <> #BakaShrot_PacketType_MissingAttachmentsRequest
Goto DecodeFailed
EndIf
ProcedureReturn *Request
; Everything read so far is tainted, drop it.
DecodeFailed:
FreeStructure(*Request)
ProcedureReturn 0
EndProcedure
@@ -10,10 +10,12 @@ XIncludeFile "../Packets.pbi"
; "" for the topic of the client's authenticated identity. The client's
; [BakaShrot.Client.<name>] PushTopics must allow it.
; The node is always the client's authenticated one, the client can't pick it.
; UploadId: the attachment uploaded for it (UploadStart, all of it stored), 0 for none.
; Answered with a MessagePushed or a RequestRefused.
Structure BakaShrot_PushMessage Extends BakaShrot_PacketBase
Topic.s
JsonData.s
UploadId.q
EndStructure
@@ -44,6 +46,10 @@ Procedure.i BakaShrot_PushMessagePacket_Encode(*Push.BakaShrot_PushMessage, *Out
Goto EncodeFailed
EndIf
If *Push\UploadId < 0 Or MsgPackWriteInteger(*MsgPackData, *Push\UploadId) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
; Frees *MsgPackData on success, leaves it untouched on failure.
*Buffer = MsgPackFinalize(*MsgPackData)
If Not *Buffer
@@ -92,6 +98,15 @@ Procedure.i BakaShrot_PushMessagePacket_Decode(*MsgPackData.MsgPackData)
Goto DecodeFailed
EndIf
; UploadId
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
*Push\UploadId = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or *Push\UploadId < 0
Goto DecodeFailed
EndIf
ProcedureReturn *Push
; Everything read so far is tainted, drop it.
@@ -0,0 +1,164 @@
; ============================================================================
; ...
; ============================================================================
XIncludeFile "../Packets.pbi"
; P2S, part ChunkIndex of the attachment of Topic:Node#MessageId (names, never
; digits), a message the server already has, as MissingAttachments asked for.
; The size and SHA-256 came with the message (ReplicateMessages).
; Answered with a ChunkStored, or a RequestRefused.
Structure BakaShrot_ReplicateAttachmentChunk Extends BakaShrot_PacketBase
Topic.s
Node.s
MessageId.q
ChunkIndex.l
*Data
DataSize.l
EndStructure
; Encodes *Chunk into a freshly allocated buffer, its base fields are set by this procedure.
; *OutSize receives its size in bytes.
; Returns 0 on failure, the caller owns the returned buffer - free it with FreeMemory.
Procedure.i BakaShrot_ReplicateAttachmentChunkPacket_Encode(*Chunk.BakaShrot_ReplicateAttachmentChunk, *OutSize.Integer)
Protected *MsgPackData.MsgPackData, *Buffer
*Chunk\Magic = #BakaShrot_ProtocolMagic
*Chunk\Version = #BakaShrot_ProtocolVersion
*Chunk\PacketType = #BakaShrot_PacketType_ReplicateAttachmentChunk
If Not BakaShrot_IsValidName(*Chunk\Topic) Or Not BakaShrot_IsValidName(*Chunk\Node) Or *Chunk\MessageId < 1 Or *Chunk\ChunkIndex < 0 Or *Chunk\ChunkIndex > #BakaShrot_MaxAttachmentChunks Or Not *Chunk\Data Or *Chunk\DataSize < 1 Or *Chunk\DataSize > #BakaShrot_AttachmentChunkSize
ProcedureReturn 0
EndIf
*MsgPackData = MsgPackCreate(#BakaShrot_AttachmentChunkSize + 256)
If Not *MsgPackData
ProcedureReturn 0
EndIf
If Not BakaShrot_WritePacketBase(*Chunk, *MsgPackData)
Goto EncodeFailed
EndIf
If MsgPackWriteString(*MsgPackData, *Chunk\Topic) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteString(*MsgPackData, *Chunk\Node) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Chunk\MessageId) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Chunk\ChunkIndex) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteBinary(*MsgPackData, *Chunk\Data, *Chunk\DataSize) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
; Frees *MsgPackData on success, leaves it untouched on failure.
*Buffer = MsgPackFinalize(*MsgPackData)
If Not *Buffer
Goto EncodeFailed
EndIf
*OutSize\i = MemorySize(*Buffer)
ProcedureReturn *Buffer
EncodeFailed:
MsgPackFree(*MsgPackData, #True)
ProcedureReturn 0
EndProcedure
; Frees a decoded *Chunk and the data it holds.
Procedure BakaShrot_ReplicateAttachmentChunkPacket_Free(*Chunk.BakaShrot_ReplicateAttachmentChunk)
If Not *Chunk
ProcedureReturn
EndIf
If *Chunk\Data
FreeMemory(*Chunk\Data)
EndIf
FreeStructure(*Chunk)
EndProcedure
; Decodes the packet at *MsgPackData's current offset into a freshly allocated struct.
; *MsgPackData stays owned by the caller, and should be discarded on failure.
; Returns 0 on failure, the caller owns the returned struct - free it with BakaShrot_ReplicateAttachmentChunkPacket_Free().
Procedure.i BakaShrot_ReplicateAttachmentChunkPacket_Decode(*MsgPackData.MsgPackData)
Protected *Chunk.BakaShrot_ReplicateAttachmentChunk, Value.q
*Chunk = AllocateStructure(BakaShrot_ReplicateAttachmentChunk)
If Not *Chunk
ProcedureReturn 0
EndIf
; The base parser doesn't check which packet it is, that's on us.
If Not BakaShrot_ReadPacketBase(*Chunk, *MsgPackData) Or *Chunk\PacketType <> #BakaShrot_PacketType_ReplicateAttachmentChunk
Goto DecodeFailed
EndIf
; Topic
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_String
Goto DecodeFailed
EndIf
*Chunk\Topic = MsgPackReadString(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Not BakaShrot_IsValidName(*Chunk\Topic)
Goto DecodeFailed
EndIf
; Node
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_String
Goto DecodeFailed
EndIf
*Chunk\Node = MsgPackReadString(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Not BakaShrot_IsValidName(*Chunk\Node)
Goto DecodeFailed
EndIf
; MessageId
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 1
Goto DecodeFailed
EndIf
*Chunk\MessageId = Value
; ChunkIndex
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 0 Or Value > #BakaShrot_MaxAttachmentChunks
Goto DecodeFailed
EndIf
*Chunk\ChunkIndex = Value
; Data
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Binary
Goto DecodeFailed
EndIf
; A copy, freed with the packet (_Free()).
*Chunk\Data = MsgPackCopyBinary(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Not *Chunk\Data
Goto DecodeFailed
EndIf
*Chunk\DataSize = MemorySize(*Chunk\Data)
If *Chunk\DataSize > #BakaShrot_AttachmentChunkSize
Goto DecodeFailed
EndIf
ProcedureReturn *Chunk
; Everything read so far is tainted, drop it.
DecodeFailed:
BakaShrot_ReplicateAttachmentChunkPacket_Free(*Chunk)
ProcedureReturn 0
EndProcedure
@@ -7,10 +7,15 @@ XIncludeFile "../Packets.pbi"
; One message of a ReplicateMessages batch.
; PushedAt: when it was pushed to its origin (Packets.pbi), stored as is.
; AttachmentSize, AttachmentSha256: its attachment's, 0 for none. The data
; itself follows on its own, once the messages are caught up
; (MissingAttachments, ReplicateAttachmentChunk).
Structure BakaShrot_ReplicatedMessage
MessageId.q
PushedAt.q
JsonData.s
AttachmentSize.q
AttachmentSha256.a[#BakaShrot_Sha256Size]
EndStructure
; Most messages a batch may carry, whatever the sender's PeerBatchSize.
@@ -26,14 +31,15 @@ EndStructure
; Encoded size of a batch's own fields, and of each message on top of its
; JSON, rounded up. Used to stop a batch before it gets over #BakaShrot_MaxPacketSize.
#BakaShrot_ReplicateBatchOverhead = 64 + 2 * (#BakaShrot_MaxNameLength + 2)
#BakaShrot_ReplicateMessageOverhead = 25
#BakaShrot_ReplicateMessageOverhead = 70
; Peer -> Server, messages the server is missing for one topic/node, in
; message id order, with the message ids they were stored under on their
; origin. Topic and Node are names, never ids (Commons/Networking/Names.pbi):
; the server stores them under its own ids for those names, and refuses
; names it doesn't know.
; On the wire: topic, node, message count, then each message's id, PushedAt and JSON.
; On the wire: topic, node, message count, then each message's id, PushedAt,
; JSON, attachment size and SHA-256.
; Answered with a MessagePushed carrying the last message's id, or a RequestRefused.
Structure BakaShrot_ReplicateMessages Extends BakaShrot_PacketBase
Topic.s
@@ -87,6 +93,17 @@ Procedure.i BakaShrot_ReplicateMessagesPacket_Encode(*Batch.BakaShrot_ReplicateM
If MsgPackWriteString(*MsgPackData, *Batch\Messages()\JsonData) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Batch\Messages()\AttachmentSize) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
; Empty without an attachment.
If *Batch\Messages()\AttachmentSize > 0
If MsgPackWriteBinary(*MsgPackData, @*Batch\Messages()\AttachmentSha256[0], #BakaShrot_Sha256Size) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
ElseIf MsgPackWriteBinary(*MsgPackData, @*Batch\Messages()\AttachmentSha256[0], 0) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
Next
; Frees *MsgPackData on success, leaves it untouched on failure.
@@ -109,7 +126,7 @@ EndProcedure
; Returns 0 on failure, the caller owns the returned struct - free it with FreeStructure.
Procedure.i BakaShrot_ReplicateMessagesPacket_Decode(*MsgPackData.MsgPackData)
Protected *Batch.BakaShrot_ReplicateMessages
Protected Value.q, Count.q, i.q, PreviousMessageId.q
Protected Value.q, Count.q, i.q, PreviousMessageId.q, ReadSize.q
*Batch = AllocateStructure(BakaShrot_ReplicateMessages)
If Not *Batch
@@ -180,6 +197,23 @@ Procedure.i BakaShrot_ReplicateMessagesPacket_Decode(*MsgPackData.MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success
Goto DecodeFailed
EndIf
; AttachmentSize
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
*Batch\Messages()\AttachmentSize = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or *Batch\Messages()\AttachmentSize < 0 Or *Batch\Messages()\AttachmentSize > #BakaShrot_MaxAttachmentSize
Goto DecodeFailed
EndIf
; AttachmentSha256: 32 bytes with an attachment, empty without.
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Binary
Goto DecodeFailed
EndIf
ReadSize = MsgPackReadBinary(*MsgPackData, @*Batch\Messages()\AttachmentSha256[0], #BakaShrot_Sha256Size)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or (ReadSize <> #BakaShrot_Sha256Size And *Batch\Messages()\AttachmentSize > 0) Or (ReadSize <> 0 And *Batch\Messages()\AttachmentSize = 0)
Goto DecodeFailed
EndIf
Next
ProcedureReturn *Batch
@@ -0,0 +1,134 @@
; ============================================================================
; ...
; ============================================================================
XIncludeFile "../Packets.pbi"
; C2S, the next part of an upload: ChunkIndex (0 for the first) of UploadId.
; Answered with a ChunkStored, or a RequestRefused (the upload is then dropped).
Structure BakaShrot_UploadChunk Extends BakaShrot_PacketBase
UploadId.q
ChunkIndex.l
*Data
DataSize.l
EndStructure
; Encodes *Chunk into a freshly allocated buffer, its base fields are set by this procedure.
; *OutSize receives its size in bytes.
; Returns 0 on failure, the caller owns the returned buffer - free it with FreeMemory.
Procedure.i BakaShrot_UploadChunkPacket_Encode(*Chunk.BakaShrot_UploadChunk, *OutSize.Integer)
Protected *MsgPackData.MsgPackData, *Buffer
*Chunk\Magic = #BakaShrot_ProtocolMagic
*Chunk\Version = #BakaShrot_ProtocolVersion
*Chunk\PacketType = #BakaShrot_PacketType_UploadChunk
If *Chunk\UploadId < 1 Or *Chunk\ChunkIndex < 0 Or *Chunk\ChunkIndex > #BakaShrot_MaxAttachmentChunks Or Not *Chunk\Data Or *Chunk\DataSize < 1 Or *Chunk\DataSize > #BakaShrot_AttachmentChunkSize
ProcedureReturn 0
EndIf
*MsgPackData = MsgPackCreate(#BakaShrot_AttachmentChunkSize + 256)
If Not *MsgPackData
ProcedureReturn 0
EndIf
If Not BakaShrot_WritePacketBase(*Chunk, *MsgPackData)
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Chunk\UploadId) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Chunk\ChunkIndex) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteBinary(*MsgPackData, *Chunk\Data, *Chunk\DataSize) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
; Frees *MsgPackData on success, leaves it untouched on failure.
*Buffer = MsgPackFinalize(*MsgPackData)
If Not *Buffer
Goto EncodeFailed
EndIf
*OutSize\i = MemorySize(*Buffer)
ProcedureReturn *Buffer
EncodeFailed:
MsgPackFree(*MsgPackData, #True)
ProcedureReturn 0
EndProcedure
; Frees a decoded *Chunk and the data it holds.
Procedure BakaShrot_UploadChunkPacket_Free(*Chunk.BakaShrot_UploadChunk)
If Not *Chunk
ProcedureReturn
EndIf
If *Chunk\Data
FreeMemory(*Chunk\Data)
EndIf
FreeStructure(*Chunk)
EndProcedure
; Decodes the packet at *MsgPackData's current offset into a freshly allocated struct.
; *MsgPackData stays owned by the caller, and should be discarded on failure.
; Returns 0 on failure, the caller owns the returned struct - free it with BakaShrot_UploadChunkPacket_Free().
Procedure.i BakaShrot_UploadChunkPacket_Decode(*MsgPackData.MsgPackData)
Protected *Chunk.BakaShrot_UploadChunk, Value.q
*Chunk = AllocateStructure(BakaShrot_UploadChunk)
If Not *Chunk
ProcedureReturn 0
EndIf
; The base parser doesn't check which packet it is, that's on us.
If Not BakaShrot_ReadPacketBase(*Chunk, *MsgPackData) Or *Chunk\PacketType <> #BakaShrot_PacketType_UploadChunk
Goto DecodeFailed
EndIf
; UploadId
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 1
Goto DecodeFailed
EndIf
*Chunk\UploadId = Value
; ChunkIndex
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 0 Or Value > #BakaShrot_MaxAttachmentChunks
Goto DecodeFailed
EndIf
*Chunk\ChunkIndex = Value
; Data
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Binary
Goto DecodeFailed
EndIf
; A copy, freed with the packet (_Free()).
*Chunk\Data = MsgPackCopyBinary(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Not *Chunk\Data
Goto DecodeFailed
EndIf
*Chunk\DataSize = MemorySize(*Chunk\Data)
If *Chunk\DataSize > #BakaShrot_AttachmentChunkSize
Goto DecodeFailed
EndIf
ProcedureReturn *Chunk
; Everything read so far is tainted, drop it.
DecodeFailed:
BakaShrot_UploadChunkPacket_Free(*Chunk)
ProcedureReturn 0
EndProcedure
@@ -0,0 +1,121 @@
; ============================================================================
; ...
; ============================================================================
XIncludeFile "../Packets.pbi"
; C2S, starts uploading an attachment, before pushing the message it goes with
; (PushMessage's UploadId): Size bytes (1 to #BakaShrot_MaxAttachmentSize, and the
; server's [BakaShrot.Server] MaxAttachmentSize), whose SHA-256 is Sha256.
; Topic: where the message will be pushed, as PushMessage's: "" for the client's
; own. The client's PushTopics must allow it.
; Answered with an UploadStarted, or a RequestRefused.
Structure BakaShrot_UploadStart Extends BakaShrot_PacketBase
Topic.s
Size.q
Sha256.a[#BakaShrot_Sha256Size]
EndStructure
; Encodes *Start into a freshly allocated buffer, its base fields are set by this procedure.
; *OutSize receives its size in bytes.
; Returns 0 on failure, the caller owns the returned buffer - free it with FreeMemory.
Procedure.i BakaShrot_UploadStartPacket_Encode(*Start.BakaShrot_UploadStart, *OutSize.Integer)
Protected *MsgPackData.MsgPackData, *Buffer
*Start\Magic = #BakaShrot_ProtocolMagic
*Start\Version = #BakaShrot_ProtocolVersion
*Start\PacketType = #BakaShrot_PacketType_UploadStart
If *Start\Size < 1 Or *Start\Size > #BakaShrot_MaxAttachmentSize
ProcedureReturn 0
EndIf
*MsgPackData = MsgPackCreate(64)
If Not *MsgPackData
ProcedureReturn 0
EndIf
If Not BakaShrot_WritePacketBase(*Start, *MsgPackData)
Goto EncodeFailed
EndIf
If MsgPackWriteString(*MsgPackData, *Start\Topic) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Start\Size) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteBinary(*MsgPackData, @*Start\Sha256[0], #BakaShrot_Sha256Size) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
; Frees *MsgPackData on success, leaves it untouched on failure.
*Buffer = MsgPackFinalize(*MsgPackData)
If Not *Buffer
Goto EncodeFailed
EndIf
*OutSize\i = MemorySize(*Buffer)
ProcedureReturn *Buffer
EncodeFailed:
MsgPackFree(*MsgPackData, #True)
ProcedureReturn 0
EndProcedure
; Decodes the packet at *MsgPackData's current offset into a freshly allocated struct.
; *MsgPackData stays owned by the caller, and should be discarded on failure.
; Returns 0 on failure, the caller owns the returned struct - free it with FreeStructure.
Procedure.i BakaShrot_UploadStartPacket_Decode(*MsgPackData.MsgPackData)
Protected *Start.BakaShrot_UploadStart, Value.q, ReadSize.q
*Start = AllocateStructure(BakaShrot_UploadStart)
If Not *Start
ProcedureReturn 0
EndIf
; The base parser doesn't check which packet it is, that's on us.
If Not BakaShrot_ReadPacketBase(*Start, *MsgPackData) Or *Start\PacketType <> #BakaShrot_PacketType_UploadStart
Goto DecodeFailed
EndIf
; Topic
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_String
Goto DecodeFailed
EndIf
*Start\Topic = MsgPackReadString(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success
Goto DecodeFailed
EndIf
; Size
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 1 Or Value > #BakaShrot_MaxAttachmentSize
Goto DecodeFailed
EndIf
*Start\Size = Value
; Sha256
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Binary
Goto DecodeFailed
EndIf
; Anything bigger than the struct's array is rejected by MsgPackReadBinary, anything smaller by the size check.
ReadSize = MsgPackReadBinary(*MsgPackData, @*Start\Sha256[0], #BakaShrot_Sha256Size)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or ReadSize <> #BakaShrot_Sha256Size
Goto DecodeFailed
EndIf
ProcedureReturn *Start
; Everything read so far is tainted, drop it.
DecodeFailed:
FreeStructure(*Start)
ProcedureReturn 0
EndProcedure
@@ -0,0 +1,103 @@
; ============================================================================
; ...
; ============================================================================
XIncludeFile "../Packets.pbi"
; S2C, answers an UploadStart: send the data with UploadChunks for UploadId, in
; order, ChunkSize bytes each (the last one what's left).
Structure BakaShrot_UploadStarted Extends BakaShrot_PacketBase
UploadId.q
ChunkSize.l
EndStructure
; Encodes *Started into a freshly allocated buffer, its base fields are set by this procedure.
; *OutSize receives its size in bytes.
; Returns 0 on failure, the caller owns the returned buffer - free it with FreeMemory.
Procedure.i BakaShrot_UploadStartedPacket_Encode(*Started.BakaShrot_UploadStarted, *OutSize.Integer)
Protected *MsgPackData.MsgPackData, *Buffer
*Started\Magic = #BakaShrot_ProtocolMagic
*Started\Version = #BakaShrot_ProtocolVersion
*Started\PacketType = #BakaShrot_PacketType_UploadStarted
If *Started\UploadId < 1 Or *Started\ChunkSize < 1 Or *Started\ChunkSize > #BakaShrot_AttachmentChunkSize
ProcedureReturn 0
EndIf
*MsgPackData = MsgPackCreate(64)
If Not *MsgPackData
ProcedureReturn 0
EndIf
If Not BakaShrot_WritePacketBase(*Started, *MsgPackData)
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Started\UploadId) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
If MsgPackWriteInteger(*MsgPackData, *Started\ChunkSize) <> #MsgPack_Error_Success
Goto EncodeFailed
EndIf
; Frees *MsgPackData on success, leaves it untouched on failure.
*Buffer = MsgPackFinalize(*MsgPackData)
If Not *Buffer
Goto EncodeFailed
EndIf
*OutSize\i = MemorySize(*Buffer)
ProcedureReturn *Buffer
EncodeFailed:
MsgPackFree(*MsgPackData, #True)
ProcedureReturn 0
EndProcedure
; Decodes the packet at *MsgPackData's current offset into a freshly allocated struct.
; *MsgPackData stays owned by the caller, and should be discarded on failure.
; Returns 0 on failure, the caller owns the returned struct - free it with FreeStructure.
Procedure.i BakaShrot_UploadStartedPacket_Decode(*MsgPackData.MsgPackData)
Protected *Started.BakaShrot_UploadStarted, Value.q
*Started = AllocateStructure(BakaShrot_UploadStarted)
If Not *Started
ProcedureReturn 0
EndIf
; The base parser doesn't check which packet it is, that's on us.
If Not BakaShrot_ReadPacketBase(*Started, *MsgPackData) Or *Started\PacketType <> #BakaShrot_PacketType_UploadStarted
Goto DecodeFailed
EndIf
; UploadId
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 1
Goto DecodeFailed
EndIf
*Started\UploadId = Value
; ChunkSize
If MsgPackFormatCodeToDataType(MsgPackGetFormatCode(*MsgPackData)) <> #MsgPack_DataTypes_Integer
Goto DecodeFailed
EndIf
Value = MsgPackReadInteger(*MsgPackData)
If MsgPackLastError(*MsgPackData) <> #MsgPack_Error_Success Or Value < 1 Or Value > #BakaShrot_AttachmentChunkSize
Goto DecodeFailed
EndIf
*Started\ChunkSize = Value
ProcedureReturn *Started
; Everything read so far is tainted, drop it.
DecodeFailed:
FreeStructure(*Started)
ProcedureReturn 0
EndProcedure