103 lines
3.5 KiB
Go
103 lines
3.5 KiB
Go
package main
|
||
|
||
// Minimal frame abstractions retained after removal of the legacy TCP/frame
|
||
// networking layer. These types remain only to avoid a wide cascading refactor
|
||
// across existing grain / pool logic that still constructs and passes
|
||
// FrameWithPayload objects internally.
|
||
//
|
||
// The original responsibilities this replaces:
|
||
// - Binary framing, checksums, network IO
|
||
// - Distinction between request / reply frame types
|
||
//
|
||
// What remains:
|
||
// - A light weight container (FrameWithPayload) used as an in‑process
|
||
// envelope for status code + typed marker + payload bytes (JSON or proto).
|
||
// - Message / status constants referenced in existing code paths.
|
||
//
|
||
// Recommended future cleanup (post‑migration):
|
||
// - Remove FrameType entirely and replace with enumerated semantic results
|
||
// or error values.
|
||
// - Replace FrameWithPayload with a struct { Status int; Data []byte }.
|
||
// - Remove remote_* reply type branching once all callers rely on gRPC
|
||
// status + strongly typed responses.
|
||
//
|
||
// For now we keep this minimal surface to keep the gRPC migration focused.
|
||
|
||
type (
|
||
// FrameType is a symbolic identifier carried through existing code paths.
|
||
// No ordering or bit semantics are required anymore.
|
||
FrameType uint32
|
||
StatusCode uint32
|
||
)
|
||
|
||
type Frame struct {
|
||
Type FrameType
|
||
StatusCode StatusCode
|
||
Length uint32
|
||
// Checksum retained for compatibility; no longer validated.
|
||
Checksum uint32
|
||
}
|
||
|
||
// FrameWithPayload wraps a Frame with an opaque payload.
|
||
// Payload usually contains JSON encoded cart state or an error message.
|
||
type FrameWithPayload struct {
|
||
Frame
|
||
Payload []byte
|
||
}
|
||
|
||
// -----------------------------------------------------------------------------
|
||
// Legacy Frame Type Constants (minimal subset still referenced)
|
||
// -----------------------------------------------------------------------------
|
||
const (
|
||
RemoteGetState = FrameType(0x01)
|
||
RemoteHandleMutation = FrameType(0x02)
|
||
ResponseBody = FrameType(0x03) // (rarely used; kept for completeness)
|
||
RemoteGetStateReply = FrameType(0x04)
|
||
RemoteHandleMutationReply = FrameType(0x05)
|
||
RemoteCreateOrderReply = FrameType(0x06)
|
||
)
|
||
|
||
// MakeFrameWithPayload constructs an in‑process frame wrapper.
|
||
// Length & Checksum are filled for backward compatibility (no validation logic
|
||
// depends on the checksum anymore).
|
||
func MakeFrameWithPayload(msg FrameType, statusCode StatusCode, payload []byte) FrameWithPayload {
|
||
length := uint32(len(payload))
|
||
return FrameWithPayload{
|
||
Frame: Frame{
|
||
Type: msg,
|
||
StatusCode: statusCode,
|
||
Length: length,
|
||
Checksum: (uint32(msg) + uint32(statusCode) + length) / 8, // simple legacy formula
|
||
},
|
||
Payload: payload,
|
||
}
|
||
}
|
||
|
||
// Clone creates a shallow copy of the frame, duplicating the payload slice.
|
||
func (f *FrameWithPayload) Clone() *FrameWithPayload {
|
||
if f == nil {
|
||
return nil
|
||
}
|
||
cp := make([]byte, len(f.Payload))
|
||
copy(cp, f.Payload)
|
||
return &FrameWithPayload{
|
||
Frame: f.Frame,
|
||
Payload: cp,
|
||
}
|
||
}
|
||
|
||
// NewErrorFrame helper for creating an error frame with a textual payload.
|
||
func NewErrorFrame(msg FrameType, code StatusCode, err error) FrameWithPayload {
|
||
var b []byte
|
||
if err != nil {
|
||
b = []byte(err.Error())
|
||
}
|
||
return MakeFrameWithPayload(msg, code, b)
|
||
}
|
||
|
||
// IsSuccess returns true if the status code indicates success in the
|
||
// conventional HTTP style range (200–299). This mirrors previous usage patterns.
|
||
func (f *FrameWithPayload) IsSuccess() bool {
|
||
return f != nil && f.StatusCode >= 200 && f.StatusCode < 300
|
||
}
|