forked from swift-server/swift-memcache-gsoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemcachedRequestEncoder.swift
97 lines (78 loc) · 3.18 KB
/
MemcachedRequestEncoder.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-memcache-gsoc open source project
//
// Copyright (c) 2023 Apple Inc. and the swift-memcache-gsoc project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of swift-memcache-gsoc project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIOCore
import NIOPosix
struct MemcachedRequestEncoder: MessageToByteEncoder {
typealias OutboundIn = MemcachedRequest
func encode(data: MemcachedRequest, out: inout ByteBuffer) throws {
switch data {
case .set(var command):
precondition(!command.key.isEmpty, "Key must not be empty")
// write command and key
out.writeInteger(UInt8.m)
out.writeInteger(UInt8.s)
out.writeInteger(UInt8.whitespace)
out.writeBytes(command.key.utf8)
out.writeInteger(UInt8.whitespace)
// write value length
let length = command.value.readableBytes
out.writeIntegerAsASCII(length)
// write flags if there are any
if let flags = command.flags {
out.writeMemcachedFlags(flags: flags)
}
// write separator
out.writeInteger(UInt8.carriageReturn)
out.writeInteger(UInt8.newline)
// write value and end line
out.writeBuffer(&command.value)
out.writeInteger(UInt8.carriageReturn)
out.writeInteger(UInt8.newline)
case .get(let command):
precondition(!command.key.isEmpty, "Key must not be empty")
// write command and key
out.writeInteger(UInt8.m)
out.writeInteger(UInt8.g)
out.writeInteger(UInt8.whitespace)
out.writeBytes(command.key.utf8)
// write flags if there are any
out.writeMemcachedFlags(flags: command.flags)
// write separator
out.writeInteger(UInt8.carriageReturn)
out.writeInteger(UInt8.newline)
case .delete(let command):
precondition(!command.key.isEmpty, "Key must not be empty")
// write command and key
out.writeInteger(UInt8.m)
out.writeInteger(UInt8.d)
out.writeInteger(UInt8.whitespace)
out.writeBytes(command.key.utf8)
// write separator
out.writeInteger(UInt8.carriageReturn)
out.writeInteger(UInt8.newline)
case .arithmetic(let command):
precondition(!command.key.isEmpty, "Key must not be empty")
// write command and key
out.writeInteger(UInt8.m)
out.writeInteger(UInt8.a)
out.writeInteger(UInt8.whitespace)
out.writeBytes(command.key.utf8)
// write flags if there are any
out.writeMemcachedFlags(flags: command.flags)
// write separator
out.writeInteger(UInt8.carriageReturn)
out.writeInteger(UInt8.newline)
}
}
}