264 lines
9.7 KiB
Ziran
264 lines
9.7 KiB
Ziran
// The same v6 registration and signed transaction as Sync, advanced by polls.
|
|
#import "mesh_client/async_client"
|
|
#import "mesh_client/client"
|
|
#import "mesh_client/auth"
|
|
#import "mesh_client/sync"
|
|
#import "net_http"
|
|
#import "text_buffer"
|
|
#import "mesh_client/sync_flow"
|
|
#import "byte_text_linux"
|
|
|
|
c :: #system_library "c";
|
|
SyncAllocate :: (size: usize) -> *void #foreign c "malloc";
|
|
SyncFree :: (pointer: *void) #foreign c "free";
|
|
|
|
PendingSync :: struct {
|
|
request: PendingRequest
|
|
client: Client
|
|
session: *Session
|
|
transport: AsyncTransport
|
|
device: Device
|
|
now: s64
|
|
random_context: *void
|
|
random: RandomNonce
|
|
body: string
|
|
output: []u8
|
|
registration: [6144]u8
|
|
registration_response: [2048]u8
|
|
transaction_header: [8192]u8
|
|
stage: s32
|
|
done: bool
|
|
result: AuthResult
|
|
status: s32
|
|
flow: SyncFlow
|
|
page: SyncPageState
|
|
page_body: *u8
|
|
record_limit: s32
|
|
transfer_retries: s32
|
|
download_only: bool
|
|
cached_registration: bool
|
|
registration_retried: bool
|
|
}
|
|
|
|
FinishSync :: (pending: *PendingSync, result: AuthResult) {
|
|
if pending.page_body != null {
|
|
SyncFree(cast(*void)pending.page_body)
|
|
pending.page_body = null
|
|
}
|
|
pending.result = result
|
|
pending.done = true
|
|
}
|
|
|
|
PrepareNextSync :: (pending: *PendingSync) -> bool {
|
|
if pending.flow.prepare != null {
|
|
if pending.page_body != null {
|
|
SyncFree(cast(*void)pending.page_body)
|
|
pending.page_body = null
|
|
}
|
|
maximum_records := pending.record_limit
|
|
if pending.download_only {
|
|
maximum_records = 0
|
|
}
|
|
pending.body = pending.flow.prepare(pending.flow.context, maximum_records)
|
|
if pending.flow.clock != null {
|
|
pending.now = pending.flow.clock(pending.flow.context)
|
|
}
|
|
if pending.body.count == 0 {
|
|
return false
|
|
}
|
|
if pending.page.enabled {
|
|
unused RestartStaleSyncPage(pending.body, *pending.page)
|
|
capacity := pending.body.count + 16384
|
|
pending.page_body = cast(*u8)SyncAllocate(cast(usize)capacity)
|
|
if pending.page_body == null || !BuildSyncPageRequest(pending.body, *pending.page,
|
|
pending.record_limit, BytesFromPointer(pending.page_body, cast(u64)capacity), pending.download_only) {
|
|
return false
|
|
}
|
|
pending.body = TextUntilNul(BytesFromPointer(pending.page_body, cast(u64)capacity))
|
|
}
|
|
}
|
|
return pending.body.count > 0
|
|
}
|
|
|
|
StartSyncTransaction :: (pending: *PendingSync) {
|
|
if !PrepareNextSync(pending) {
|
|
FinishSync(pending, cast(AuthResult)AuthResult.AUTH_PAYLOAD_FAILED)
|
|
return
|
|
}
|
|
prepared := PrepareSyncHeader(pending.client, pending.session,
|
|
pending.device, pending.now, pending.random_context,
|
|
pending.random, pending.body, pending.transaction_header[:])
|
|
if prepared != cast(AuthResult)AuthResult.AUTH_OK {
|
|
FinishSync(pending, prepared)
|
|
return
|
|
}
|
|
extra: [1]HttpHeader
|
|
extra[0].name = "X-Mesh-Tx"
|
|
extra[0].value = TextUntilNul(pending.transaction_header[:])
|
|
pending.stage = 3
|
|
if !BeginRequest(*pending.request, pending.client, pending.session,
|
|
pending.now, pending.transport, "POST", "/api/v1/sync",
|
|
pending.body, pending.output, extra[:]) {
|
|
FinishSync(pending, cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED)
|
|
}
|
|
}
|
|
|
|
#scope_export
|
|
BeginSync :: (pending: *PendingSync, client: Client, session: *Session,
|
|
device: Device, now: s64, random_context: *void, random: RandomNonce,
|
|
transport: AsyncTransport, body: string, output: []u8, flow: SyncFlow = .{}) -> bool {
|
|
if pending == null || session == null || pending.request.waiting ||
|
|
transport.start == null || transport.poll == null || transport.cancel == null {
|
|
return false
|
|
}
|
|
pending.* = PendingSync.{}
|
|
pending.client = client
|
|
pending.session = session
|
|
pending.device = device
|
|
pending.now = now
|
|
pending.random_context = random_context
|
|
pending.random = random
|
|
pending.transport = transport
|
|
pending.body = body
|
|
pending.output = output
|
|
pending.flow = flow
|
|
pending.record_limit = 64
|
|
if output.count == 0 || (body.count == 0 && flow.prepare == null) ||
|
|
(flow.prepare != null && (flow.load == null || flow.save == null || flow.accept == null)) {
|
|
FinishSync(pending, cast(AuthResult)AuthResult.AUTH_PAYLOAD_FAILED)
|
|
return true
|
|
}
|
|
if flow.prepare != null && !flow.load(flow.context, *pending.page) {
|
|
FinishSync(pending, cast(AuthResult)AuthResult.AUTH_PAYLOAD_FAILED)
|
|
return true
|
|
}
|
|
pending.download_only = pending.page.enabled && pending.page.pending
|
|
output[0] = cast(u8)0
|
|
pending.stage = 1
|
|
if !BeginSession(*pending.request, client, session, now, transport) {
|
|
FinishSync(pending, cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED)
|
|
}
|
|
return true
|
|
}
|
|
|
|
#scope_export
|
|
PollSync :: (pending: *PendingSync) -> bool {
|
|
if pending == null {
|
|
return true
|
|
}
|
|
if pending.done {
|
|
return true
|
|
}
|
|
if !PollRequest(*pending.request) {
|
|
return false
|
|
}
|
|
if pending.stage == 3 {
|
|
pending.status = pending.request.status
|
|
}
|
|
if pending.request.result != cast(AuthResult)AuthResult.AUTH_OK {
|
|
if pending.stage == 3 && pending.transfer_retries < 2 &&
|
|
ReplayedSyncTransaction(pending.request.status, TextUntilNul(pending.output)) {
|
|
pending.transfer_retries += 1
|
|
StartSyncTransaction(pending)
|
|
return pending.done
|
|
}
|
|
if pending.stage == 3 && pending.cached_registration && !pending.registration_retried &&
|
|
MissingRegisteredDevice(pending.request.status, TextUntilNul(pending.output)) {
|
|
pending.registration_retried = true
|
|
pending.session.registration = Registration.{}
|
|
pending.stage = 1
|
|
if !BeginSession(*pending.request, pending.client, pending.session, pending.now, pending.transport) {
|
|
FinishSync(pending, cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED)
|
|
}
|
|
return pending.done
|
|
}
|
|
if pending.stage == 3 && pending.flow.prepare != null &&
|
|
pending.request.result == cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED {
|
|
if pending.request.failure == TRANSFER_FAILURE_CAPACITY {
|
|
if !pending.page.enabled {
|
|
next := pending.page
|
|
next.enabled = true
|
|
next.pending = true
|
|
if !pending.flow.save(pending.flow.context, *next) {
|
|
FinishSync(pending, cast(AuthResult)AuthResult.AUTH_PAYLOAD_FAILED)
|
|
return true
|
|
}
|
|
pending.page = next
|
|
pending.download_only = true
|
|
StartSyncTransaction(pending)
|
|
return pending.done
|
|
}
|
|
if pending.record_limit > 1 {
|
|
pending.record_limit = pending.record_limit / 2
|
|
StartSyncTransaction(pending)
|
|
return pending.done
|
|
}
|
|
} else if pending.request.failure == TRANSFER_FAILURE_TRANSPORT && pending.transfer_retries < 2 {
|
|
pending.transfer_retries += 1
|
|
StartSyncTransaction(pending)
|
|
return pending.done
|
|
}
|
|
}
|
|
FinishSync(pending, pending.request.result)
|
|
return true
|
|
}
|
|
if pending.stage == 1 {
|
|
if RegistrationCurrent(pending.client, pending.session, pending.device, pending.now) {
|
|
pending.cached_registration = true
|
|
StartSyncTransaction(pending)
|
|
return pending.done
|
|
}
|
|
prepared := PrepareDeviceRegistration(pending.client, pending.session,
|
|
pending.device, pending.now, pending.random_context,
|
|
pending.random, pending.registration[:], pending.client.signed_protocol)
|
|
if prepared != cast(AuthResult)AuthResult.AUTH_OK {
|
|
FinishSync(pending, prepared)
|
|
return true
|
|
}
|
|
extra: [1]HttpHeader
|
|
pending.stage = 2
|
|
if !BeginRequest(*pending.request, pending.client, pending.session,
|
|
pending.now, pending.transport, "POST", "/api/v1/account/devices",
|
|
TextUntilNul(pending.registration[:]), pending.registration_response[:],
|
|
extra[0:0]) {
|
|
FinishSync(pending, cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED)
|
|
}
|
|
} else if pending.stage == 2 {
|
|
RememberRegistration(pending.client, pending.session, pending.device, pending.now)
|
|
StartSyncTransaction(pending)
|
|
} else {
|
|
RememberRegistration(pending.client, pending.session, pending.device, pending.now)
|
|
if pending.flow.prepare != null {
|
|
next: SyncPageState
|
|
accepted: bool
|
|
{
|
|
response := TextUntilNul(pending.output)
|
|
accepted = AcceptSyncResponse(pending.body, response, *pending.page, *next, pending.record_limit) &&
|
|
pending.flow.accept(pending.flow.context, response, *next)
|
|
}
|
|
if !accepted {
|
|
FinishSync(pending, cast(AuthResult)AuthResult.AUTH_PAYLOAD_FAILED)
|
|
return true
|
|
}
|
|
pending.page = next
|
|
pending.transfer_retries = 0
|
|
if next.pending {
|
|
pending.download_only = true
|
|
StartSyncTransaction(pending)
|
|
return pending.done
|
|
}
|
|
}
|
|
FinishSync(pending, pending.request.result)
|
|
}
|
|
return pending.done
|
|
}
|
|
|
|
#scope_export
|
|
CancelSync :: (pending: *PendingSync) {
|
|
if pending == null {
|
|
return
|
|
}
|
|
CancelRequest(*pending.request)
|
|
FinishSync(pending, cast(AuthResult)AuthResult.AUTH_REQUEST_FAILED)
|
|
}
|