libdrizzle Developer Documentation

command.c
Go to the documentation of this file.
1/*
2 * Drizzle Client & Protocol Library
3 *
4 * Copyright (C) 2008 Eric Day (eday@oddments.org)
5 * All rights reserved.
6 *
7 * Use and distribution licensed under the BSD license. See
8 * the COPYING file in this directory for full text.
9 */
10
16#include "common.h"
17
18/*
19 * Private variables.
20 */
21
56
57/*
58 * State Definitions
59 */
60
62{
63 drizzle_log_debug(con->drizzle, "drizzle_state_command_read");
64
65 if (con->buffer_size == 0)
66 {
68 return DRIZZLE_RETURN_OK;
69 }
70
71 if (con->command_total == 0)
72 {
73 con->command= (drizzle_command_t)(con->buffer_ptr[0]);
74 con->buffer_ptr++;
75 con->buffer_size--;
76
77 con->command_total= (con->packet_size - 1);
78 }
79
80 if (con->buffer_size < (con->command_total - con->command_offset))
81 {
82 con->command_size= con->buffer_size;
83 con->command_offset+= con->command_size;
84 }
85 else
86 {
87 con->command_size= (con->command_total - con->command_offset);
89 }
90
91 con->command_data= con->buffer_ptr;
92 con->buffer_ptr+= con->command_size;
93 con->buffer_size-= con->command_size;
94
95 if (con->command_offset == con->command_total)
97 else
99
100 return DRIZZLE_RETURN_OK;
101}
102
104{
105 uint8_t *start;
106 uint8_t *ptr;
107 size_t free_size;
109
110 drizzle_log_debug(con->drizzle, "drizzle_state_command_write");
111
112 if (con->command_data == NULL && con->command_total != 0 &&
114 {
116 }
117
118 if (con->buffer_size == 0)
119 {
120 con->buffer_ptr= con->buffer;
121 start= con->buffer;
122 }
123 else
124 start= con->buffer_ptr + con->buffer_size;
125
126 if (con->command_offset == 0)
127 {
128 /* Make sure we can fit the largest non-streaming packet, currently a
129 DRIZZLE_COMMAND_CHANGE_USER command. */
130
131 con->packet_size= 1 /* Command */
132 + strlen(con->user) + 1
133 + 1 /* Scramble size */
135 + strlen(con->db) + 1;
136
137 /* Flush buffer if there is not enough room. */
138 free_size= (size_t)DRIZZLE_MAX_BUFFER_SIZE - (size_t)(start - con->buffer);
139 if (free_size < con->packet_size)
140 {
142 return DRIZZLE_RETURN_OK;
143 }
144
145 /* Store packet size at the end since it may change. */
146 con->packet_number= 1;
147 ptr= start;
148 ptr[3]= 0;
149 if (con->options & DRIZZLE_CON_MYSQL)
150 ptr[4]= (uint8_t)(con->command);
151 else
152 ptr[4]= (uint8_t)(_command_drizzle_map[con->command]);
153 ptr+= 5;
154
156 {
157 ptr= drizzle_pack_auth(con, ptr, &ret);
158 if (ret != DRIZZLE_RETURN_OK)
159 return ret;
160
161 con->buffer_size+= (4 + con->packet_size);
162 }
163 else if (con->command_total == 0)
164 {
165 con->packet_size= 1;
166 con->buffer_size+= 5;
167 }
168 else
169 {
170 con->packet_size= 1 + con->command_total;
171 free_size-= 5;
172
173 /* Copy as much of the data in as we can into the write buffer. */
174 if (con->command_size <= free_size)
175 {
176 memcpy(ptr, con->command_data, con->command_size);
177 con->command_offset= con->command_size;
178 con->command_data= NULL;
179 con->buffer_size+= 5 + con->command_size;
180 }
181 else
182 {
183 memcpy(ptr, con->command_data, free_size);
184 con->command_offset= free_size;
185 con->command_data+= free_size;
186 con->command_size-= free_size;
187 con->buffer_size+= 5 + free_size;
188 }
189 }
190
191 /* Store packet size now. */
192 drizzle_set_byte3(start, con->packet_size);
193 }
194 else
195 {
196 /* Write directly from the caller buffer for the rest. */
197 con->buffer_ptr= con->command_data;
198 con->buffer_size= con->command_size;
199 con->command_offset+= con->command_size;
200 con->command_data= NULL;
201 }
202
203 if (con->command_offset == con->command_total)
204 {
206
207 if (!(con->options & (DRIZZLE_CON_RAW_PACKET |
210 {
213 }
214 }
215
217
218 return DRIZZLE_RETURN_OK;
219}
static drizzle_command_drizzle_t _command_drizzle_map[]
Definition command.c:22
System Include Files.
drizzle_command_t
Definition constants.h:214
@ DRIZZLE_COMMAND_FIELD_LIST
Definition constants.h:219
@ DRIZZLE_COMMAND_CHANGE_USER
Definition constants.h:232
drizzle_command_drizzle_t
Definition constants.h:253
@ DRIZZLE_COMMAND_DRIZZLE_QUERY
Definition constants.h:257
@ DRIZZLE_COMMAND_DRIZZLE_PING
Definition constants.h:260
@ DRIZZLE_COMMAND_DRIZZLE_SHUTDOWN
Definition constants.h:258
@ DRIZZLE_COMMAND_DRIZZLE_QUIT
Definition constants.h:255
@ DRIZZLE_COMMAND_DRIZZLE_INIT_DB
Definition constants.h:256
@ DRIZZLE_COMMAND_DRIZZLE_END
Definition constants.h:261
static void drizzle_state_pop(drizzle_con_st *con)
Definition conn_local.h:73
static void drizzle_state_push(drizzle_con_st *con, drizzle_state_fn *function)
Definition conn_local.h:57
@ DRIZZLE_CON_NO_RESULT_READ
Definition constants.h:139
@ DRIZZLE_CON_MYSQL
Definition constants.h:135
@ DRIZZLE_CON_RAW_PACKET
Definition constants.h:136
#define DRIZZLE_MAX_BUFFER_SIZE
Definition constants.h:55
#define DRIZZLE_MAX_SCRAMBLE_SIZE
Definition constants.h:58
drizzle_return_t
Definition constants.h:69
@ DRIZZLE_RETURN_PAUSE
Definition constants.h:72
@ DRIZZLE_RETURN_OK
Definition constants.h:70
static void drizzle_log_debug(drizzle_st *drizzle, const char *format,...)
#define drizzle_set_byte3(__buffer, __int)
Definition constants.h:478
uint8_t * drizzle_pack_auth(drizzle_con_st *con, uint8_t *ptr, drizzle_return_t *ret_ptr)
Definition pack.c:172
drizzle_return_t drizzle_state_packet_read(drizzle_con_st *con)
Definition state.c:40
drizzle_return_t drizzle_state_write(drizzle_con_st *con)
Definition conn.c:975
drizzle_return_t drizzle_state_command_read(drizzle_con_st *con)
Definition command.c:61
drizzle_return_t drizzle_state_read(drizzle_con_st *con)
Definition conn.c:897
drizzle_return_t drizzle_state_command_write(drizzle_con_st *con)
Definition command.c:103
drizzle_return_t drizzle_state_result_read(drizzle_con_st *con)
Definition result.c:384
size_t command_offset
Definition structs.h:93
uint8_t packet_number
Definition structs.h:76
uint8_t buffer[DRIZZLE_MAX_BUFFER_SIZE]
Definition structs.h:115
drizzle_st * drizzle
Definition structs.h:103
uint8_t * command_data
Definition structs.h:100
drizzle_con_options_t options
Definition structs.h:84
size_t packet_size
Definition structs.h:96
size_t command_total
Definition structs.h:95
uint8_t * buffer_ptr
Definition structs.h:98
size_t command_size
Definition structs.h:94
char db[DRIZZLE_MAX_DB_SIZE]
Definition structs.h:116
size_t buffer_size
Definition structs.h:92
drizzle_command_t command
Definition structs.h:83
char user[DRIZZLE_MAX_USER_SIZE]
Definition structs.h:121