60
60
#define COMP_HEADER_SIZE 3 /* compression header extra size */
62
62
#define MAX_PACKET_LENGTH (256L*256L*256L-1)
63
const char *not_error_sqlstate= "00000";
65
static bool net_write_buff(NET *net, const unsigned char *packet, uint32_t len);
64
static bool net_write_buff(NET*, const void*, uint32_t len);
66
65
static int drizzleclient_net_real_write(NET *net, const unsigned char *packet, size_t len);
68
67
/** Init with packet info. */
70
void drizzleclient_net_init(NET *net, Vio* vio, uint32_t buffer_length)
73
net->max_packet= (uint32_t) buffer_length;
74
net->max_packet_size= max(buffer_length, drizzled::global_system_variables.max_allowed_packet);
76
net->buff=(unsigned char*) malloc((size_t) net->max_packet + NET_HEADER_SIZE + COMP_HEADER_SIZE);
77
net->buff_end=net->buff+net->max_packet;
78
net->error=0; net->return_status=0;
79
net->pkt_nr=net->compress_pkt_nr=0;
80
net->write_pos=net->read_pos = net->buff;
82
net->compress=0; net->reading_or_writing=0;
83
net->where_b = net->remain_in_buf=0;
87
if (vio != 0) /* If real connection */
89
net->fd = vio->get_fd(); /* For perl DBI/DBD */
94
void drizzleclient_net_init_sock(NET* net, int sock, uint32_t buffer_length)
96
drizzleclient_net_init(net, new Vio(sock), buffer_length);
99
void drizzleclient_net_end(NET *net)
105
void drizzleclient_net_close(NET *net)
107
drizzled::safe_delete(net->vio);
110
bool drizzleclient_net_peer_addr(NET *net, char *buf, uint16_t *port, size_t buflen)
112
return net->vio->peer_addr(buf, port, buflen);
115
void drizzleclient_net_keepalive(NET *net, bool flag)
117
net->vio->keepalive(flag);
120
int drizzleclient_net_get_sd(NET *net)
122
return net->vio->get_fd();
69
void NET::init(int sock, uint32_t buffer_length)
72
max_packet= (uint32_t) buffer_length;
73
max_packet_size= max(buffer_length, drizzled::global_system_variables.max_allowed_packet);
75
buff= (unsigned char*) malloc((size_t) max_packet + NET_HEADER_SIZE + COMP_HEADER_SIZE);
76
buff_end= buff + max_packet;
78
pkt_nr= compress_pkt_nr= 0;
79
write_pos= read_pos= buff;
81
where_b= remain_in_buf= 0;
94
drizzled::safe_delete(vio);
97
bool NET::peer_addr(char *buf, size_t buflen, uint16_t& port)
99
return vio->peer_addr(buf, buflen, port);
102
void NET::keepalive(bool flag)
104
vio->keepalive(flag);
107
int NET::get_sd() const
109
return vio->get_fd();
125
112
/** Realloc the packet buffer. */
148
bool drizzleclient_net_flush(NET *net)
151
if (net->buff != net->write_pos)
138
if (buff != write_pos)
153
error= drizzleclient_net_real_write(net, net->buff, (size_t) (net->write_pos - net->buff)) ? 1 : 0;
154
net->write_pos= net->buff;
140
error= drizzleclient_net_real_write(this, buff, write_pos - buff) ? true : false;
156
143
/* Sync packet number if using compression */
158
net->pkt_nr=net->compress_pkt_nr;
145
pkt_nr= compress_pkt_nr;
174
161
If compression is used the original package is modified!
178
drizzleclient_net_write(NET *net,const unsigned char *packet,size_t len)
165
drizzleclient_net_write(NET* net, const void* packet0, size_t len)
167
const unsigned char* packet= reinterpret_cast<const unsigned char*>(packet0);
180
168
unsigned char buff[NET_HEADER_SIZE];
181
169
if (unlikely(!net->vio)) /* nowhere to write */
190
178
const uint32_t z_size = MAX_PACKET_LENGTH;
191
179
int3store(buff, z_size);
192
180
buff[3]= (unsigned char) net->pkt_nr++;
193
if (net_write_buff(net, buff, NET_HEADER_SIZE) ||
194
net_write_buff(net, packet, z_size))
181
if (net_write_buff(net, buff, NET_HEADER_SIZE) || net_write_buff(net, packet, z_size))
196
183
packet += z_size;
265
250
int3store(buff,length);
266
251
buff[3]= (unsigned char) net->pkt_nr++;
267
return((net_write_buff(net, buff, header_size) ||
252
return (net_write_buff(net, buff, header_size) ||
268
253
(head_len && net_write_buff(net, header, head_len)) ||
269
net_write_buff(net, packet, len) || drizzleclient_net_flush(net)) ? 1 : 0 );
254
net_write_buff(net, packet, len) || net->flush());
476
460
my_real_read(NET *net, size_t *complen)
479
462
size_t length= 0;
480
uint32_t i,retry_count=0;
463
uint32_t retry_count=0;
481
464
size_t len=packet_error;
482
uint32_t remain= (net->compress ? NET_HEADER_SIZE+COMP_HEADER_SIZE :
465
uint32_t remain= net->compress ? NET_HEADER_SIZE+COMP_HEADER_SIZE : NET_HEADER_SIZE;
487
net->reading_or_writing= 1;
488
469
/* Read timeout is set in drizzleclient_net_set_read_timeout */
490
pos = net->buff + net->where_b; /* net->packet -4 */
471
unsigned char* pos = net->buff + net->where_b; /* net->packet -4 */
492
for (i= 0; i < 2 ; i++)
473
for (uint32_t i= 0; i < 2 ; i++)
494
475
while (remain > 0)
751
void drizzleclient_net_set_read_timeout(NET *net, uint32_t timeout)
753
net->read_timeout= timeout;
756
net->vio->timeout(0, timeout);
762
void drizzleclient_net_set_write_timeout(NET *net, uint32_t timeout)
764
net->write_timeout= timeout;
767
net->vio->timeout(1, timeout);
729
void NET::set_read_timeout(uint32_t timeout)
731
read_timeout_= timeout;
734
vio->timeout(0, timeout);
739
void NET::set_write_timeout(uint32_t timeout)
741
write_timeout_= timeout;
744
vio->timeout(1, timeout);
749
bool NET::write(const void* data, size_t size)
751
return drizzleclient_net_write(this, data, size);
754
bool NET::write_command(unsigned char command,
755
const unsigned char *header, size_t head_len,
756
const unsigned char *packet, size_t len)
758
return drizzleclient_net_write_command(this, command, header, head_len, packet, len);
763
return drizzleclient_net_read(this);
772
766
} /* namespace drizzle_plugin */