Permalink
Newer
Older
100644 174 lines (158 sloc) 6.25 KB
1
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
/*
3
* Copyright 2010, 2011 Couchbase, Inc.
4
*
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
8
*
9
* http://www.apache.org/licenses/LICENSE-2.0
10
*
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
16
*/
17
18
/**
19
* This file contains the functions to operate on the packets
20
*
21
* @author Trond Norbye
22
* @todo add more documentation
23
*/
24
25
#include "internal.h"
26
@trondn
Aug 23, 2012
27
void lcb_server_buffer_start_packet(lcb_server_t *c,
28
const void *command_cookie,
29
ringbuffer_t *buff,
30
ringbuffer_t *buff_cookie,
31
const void *data,
32
lcb_size_t size)
33
{
@trondn
Aug 23, 2012
34
struct lcb_command_data_st ct;
35
memset(&ct, 0, sizeof(struct lcb_command_data_st));
36
/* @todo we don't want to call gethrtime for each operation, */
37
/* so I need to pass it down the chain so that a large */
38
/* multiget can reuse the same timer... */
39
ct.start = gethrtime();
40
ct.cookie = command_cookie;
42
if (!ringbuffer_ensure_capacity(buff, size) ||
43
!ringbuffer_ensure_capacity(&c->cmd_log, size) ||
44
!ringbuffer_ensure_capacity(buff_cookie, sizeof(ct)) ||
45
ringbuffer_write(buff, data, size) != size ||
46
ringbuffer_write(&c->cmd_log, data, size) != size ||
47
ringbuffer_write(buff_cookie, &ct, sizeof(ct)) != sizeof(ct)) {
50
}
51
@trondn
Aug 23, 2012
52
void lcb_server_buffer_retry_packet(lcb_server_t *c,
53
struct lcb_command_data_st *ct,
54
ringbuffer_t *buff,
55
ringbuffer_t *buff_cookie,
56
const void *data,
57
lcb_size_t size)
@trondn
Aug 23, 2012
59
lcb_size_t ct_size = sizeof(struct lcb_command_data_st);
61
if (!ringbuffer_ensure_capacity(buff, size) ||
62
!ringbuffer_ensure_capacity(&c->cmd_log, size) ||
63
!ringbuffer_ensure_capacity(buff_cookie, ct_size) ||
64
ringbuffer_write(buff, data, size) != size ||
65
ringbuffer_write(&c->cmd_log, data, size) != size ||
66
ringbuffer_write(buff_cookie, ct, ct_size) != ct_size) {
67
abort();
68
}
69
}
70
@trondn
Aug 23, 2012
71
void lcb_server_buffer_write_packet(lcb_server_t *c,
72
ringbuffer_t *buff,
73
const void *data,
74
lcb_size_t size)
75
{
77
if (!ringbuffer_ensure_capacity(buff, size) ||
78
!ringbuffer_ensure_capacity(&c->cmd_log, size) ||
79
ringbuffer_write(buff, data, size) != size ||
80
ringbuffer_write(&c->cmd_log, data, size) != size) {
83
}
84
@trondn
Aug 23, 2012
85
void lcb_server_buffer_end_packet(lcb_server_t *c,
86
ringbuffer_t *buff)
87
{
88
(void)c;
89
(void)buff;
90
}
91
@trondn
Aug 23, 2012
92
void lcb_server_buffer_complete_packet(lcb_server_t *c,
93
const void *command_cookie,
94
ringbuffer_t *buff,
95
ringbuffer_t *buff_cookie,
96
const void *data,
97
lcb_size_t size)
98
{
@trondn
Aug 23, 2012
100
lcb_server_buffer_start_packet(c, command_cookie,
101
buff, buff_cookie, data, size);
102
lcb_server_buffer_end_packet(c, buff);
103
}
104
@trondn
Aug 23, 2012
105
void lcb_server_retry_packet(lcb_server_t *c,
106
struct lcb_command_data_st *command_data,
107
const void *data,
108
lcb_size_t size)
109
{
110
if (c->connected) {
@trondn
Aug 23, 2012
111
lcb_server_buffer_retry_packet(c, command_data,
112
&c->output,
113
&c->output_cookies,
114
data, size);
@trondn
Aug 23, 2012
116
lcb_server_buffer_retry_packet(c, command_data,
117
&c->pending,
118
&c->pending_cookies,
119
data, size);
@trondn
Aug 23, 2012
123
void lcb_server_start_packet(lcb_server_t *c,
124
const void *command_cookie,
125
const void *data,
126
lcb_size_t size)
128
if (c->connected) {
@trondn
Aug 23, 2012
129
lcb_server_buffer_start_packet(c, command_cookie,
130
&c->output,
131
&c->output_cookies,
132
data, size);
133
} else {
@trondn
Aug 23, 2012
134
lcb_server_buffer_start_packet(c, command_cookie,
135
&c->pending,
136
&c->pending_cookies,
137
data, size);
139
}
140
@trondn
Aug 23, 2012
141
void lcb_server_write_packet(lcb_server_t *c,
142
const void *data,
143
lcb_size_t size)
145
if (c->connected) {
@trondn
Aug 23, 2012
146
lcb_server_buffer_write_packet(c, &c->output, data, size);
147
} else {
@trondn
Aug 23, 2012
148
lcb_server_buffer_write_packet(c, &c->pending, data, size);
149
}
150
}
151
@trondn
Aug 23, 2012
152
void lcb_server_end_packet(lcb_server_t *c)
155
}
156
@trondn
Aug 23, 2012
157
void lcb_server_complete_packet(lcb_server_t *c,
158
const void *command_cookie,
159
const void *data,
160
lcb_size_t size)
162
if (c->connected) {
@trondn
Aug 23, 2012
163
lcb_server_buffer_complete_packet(c, command_cookie,
164
&c->output,
165
&c->output_cookies,
166
data, size);
@trondn
Aug 23, 2012
168
lcb_server_buffer_complete_packet(c, command_cookie,
169
&c->pending,
170
&c->pending_cookies,
171
data, size);