51
51
/** Init with packet info. */
53
bool my_net_init(NET *net, Vio* vio)
53
bool drizzleclient_net_init(NET *net, Vio* vio)
56
my_net_local_init(net); /* Set some limits */
56
drizzleclient_net_local_init(net); /* Set some limits */
57
57
if (!(net->buff=(unsigned char*) malloc((size_t) net->max_packet+
58
58
NET_HEADER_SIZE + COMP_HEADER_SIZE)))
70
70
if (vio != 0) /* If real connection */
72
net->fd = vio_fd(vio); /* For perl DBI/DBD */
72
net->fd = drizzleclient_vio_fd(vio); /* For perl DBI/DBD */
73
drizzleclient_vio_fastsend(vio);
78
bool net_init_sock(NET * net, int sock, int flags)
78
bool drizzleclient_net_init_sock(NET * net, int sock, int flags)
81
Vio *vio_tmp= vio_new(sock, VIO_TYPE_TCPIP, flags);
81
Vio *drizzleclient_vio_tmp= drizzleclient_vio_new(sock, VIO_TYPE_TCPIP, flags);
82
if (drizzleclient_vio_tmp == NULL)
85
if (my_net_init(net, vio_tmp))
85
if (drizzleclient_net_init(net, drizzleclient_vio_tmp))
87
87
/* Only delete the temporary vio if we didn't already attach it to the
90
if (vio_tmp && (net->vio != vio_tmp))
90
if (drizzleclient_vio_tmp && (net->vio != drizzleclient_vio_tmp))
91
drizzleclient_vio_delete(drizzleclient_vio_tmp);
94
94
(void) shutdown(sock, SHUT_RDWR);
110
void net_close(NET *net)
110
void drizzleclient_net_close(NET *net)
112
112
if (net->vio != NULL)
114
vio_delete(net->vio);
114
drizzleclient_vio_delete(net->vio);
119
bool net_peer_addr(NET *net, char *buf, uint16_t *port, size_t buflen)
121
return vio_peer_addr(net->vio, buf, port, buflen);
124
void net_keepalive(NET *net, bool flag)
126
vio_keepalive(net->vio, flag);
129
int net_get_sd(NET *net)
119
bool drizzleclient_net_peer_addr(NET *net, char *buf, uint16_t *port, size_t buflen)
121
return drizzleclient_vio_peer_addr(net->vio, buf, port, buflen);
124
void drizzleclient_net_keepalive(NET *net, bool flag)
126
drizzleclient_vio_keepalive(net->vio, flag);
129
int drizzleclient_net_get_sd(NET *net)
131
131
return net->vio->sd;
134
bool net_should_close(NET *net)
134
bool drizzleclient_net_should_close(NET *net)
136
136
return net->error || (net->vio == 0);
139
bool net_more_data(NET *net)
139
bool drizzleclient_net_more_data(NET *net)
141
141
return (net->vio == 0 || net->vio->read_pos < net->vio->read_end);
144
144
/** Realloc the packet buffer. */
146
bool net_realloc(NET *net, size_t length)
146
bool drizzleclient_net_realloc(NET *net, size_t length)
148
148
unsigned char *buff;
149
149
size_t pkt_length;
209
209
Read from socket until there is nothing more to read. Discard
212
If there is anything when to read 'net_clear' is called this
212
If there is anything when to read 'drizzleclient_net_clear' is called this
213
213
normally indicates an error in the protocol.
215
215
When connection is properly closed (for TCP it means with
220
220
@param clear_buffer if <> 0, then clear all data from comm buff
223
void net_clear(NET *net, bool clear_buffer)
223
void drizzleclient_net_clear(NET *net, bool clear_buffer)
225
225
if (clear_buffer)
227
227
while (net_data_is_ready(net->vio->sd) > 0)
229
229
/* The socket is ready */
230
if (vio_read(net->vio, net->buff,
230
if (drizzleclient_vio_read(net->vio, net->buff,
231
231
(size_t) net->max_packet) <= 0)
244
244
/** Flush write_buffer if not empty. */
246
bool net_flush(NET *net)
246
bool drizzleclient_net_flush(NET *net)
249
249
if (net->buff != net->write_pos)
251
error=net_real_write(net, net->buff,
251
error=drizzleclient_net_real_write(net, net->buff,
252
252
(size_t) (net->write_pos - net->buff)) ? 1 : 0;
253
253
net->write_pos=net->buff;
277
my_net_write(NET *net,const unsigned char *packet,size_t len)
277
drizzleclient_net_write(NET *net,const unsigned char *packet,size_t len)
279
279
unsigned char buff[NET_HEADER_SIZE];
280
280
if (unlikely(!net->vio)) /* nowhere to write */
334
net_write_command(NET *net,unsigned char command,
334
drizzleclient_net_write_command(NET *net,unsigned char command,
335
335
const unsigned char *header, size_t head_len,
336
336
const unsigned char *packet, size_t len)
365
365
buff[3]= (unsigned char) net->pkt_nr++;
366
366
return((net_write_buff(net, buff, header_size) ||
367
367
(head_len && net_write_buff(net, header, head_len)) ||
368
net_write_buff(net, packet, len) || net_flush(net)) ? 1 : 0 );
368
net_write_buff(net, packet, len) || drizzleclient_net_flush(net)) ? 1 : 0 );
383
383
@param len Length of packet
386
The cached buffer can be sent as it is with 'net_flush()'.
386
The cached buffer can be sent as it is with 'drizzleclient_net_flush()'.
387
387
In this code we have to be careful to not send a packet longer than
388
MAX_PACKET_LENGTH to net_real_write() if we are using the compressed
388
MAX_PACKET_LENGTH to drizzleclient_net_real_write() if we are using the compressed
389
389
protocol as we store the length of the compressed packet in 3 bytes.
410
410
/* Fill up already used packet and write it */
411
411
memcpy(net->write_pos,packet,left_length);
412
if (net_real_write(net, net->buff,
412
if (drizzleclient_net_real_write(net, net->buff,
413
413
(size_t) (net->write_pos - net->buff) + left_length))
415
415
net->write_pos= net->buff;
425
425
left_length= MAX_PACKET_LENGTH;
426
426
while (len > left_length)
428
if (net_real_write(net, packet, left_length))
428
if (drizzleclient_net_real_write(net, packet, left_length))
430
430
packet+= left_length;
431
431
len-= left_length;
434
434
if (len > net->max_packet)
435
return net_real_write(net, packet, len) ? 1 : 0;
435
return drizzleclient_net_real_write(net, packet, len) ? 1 : 0;
436
436
/* Send out rest of the blocks as full sized blocks */
438
438
memcpy(net->write_pos,packet,len);
454
454
in the server, yield to another process and come back later.
457
net_real_write(NET *net, const unsigned char *packet, size_t len)
457
drizzleclient_net_real_write(NET *net, const unsigned char *packet, size_t len)
460
460
const unsigned char *pos, *end;
549
549
while (pos != end)
552
if ((long) (length= vio_write(net->vio, pos, (size_t) (end-pos))) <= 0)
552
if ((long) (length= drizzleclient_vio_write(net->vio, pos, (size_t) (end-pos))) <= 0)
554
const bool interrupted= vio_should_retry(net->vio);
554
const bool interrupted= drizzleclient_vio_should_retry(net->vio);
556
556
If we read 0, or we were interrupted this means that
557
557
we need to switch to blocking mode and wait until the timeout
564
while (vio_blocking(net->vio, true, &old_mode) < 0)
564
while (drizzleclient_vio_blocking(net->vio, true, &old_mode) < 0)
566
if (vio_should_retry(net->vio) && retry_count++ < net->retry_count)
566
if (drizzleclient_vio_should_retry(net->vio) && retry_count++ < net->retry_count)
568
568
net->error= 2; /* Close socket */
569
569
net->last_errno= CR_NET_PACKET_TOO_LARGE;
608
608
Reads one packet to net->buff + net->where_b.
609
Long packets are handled by my_net_read().
609
Long packets are handled by drizzleclient_net_read().
610
610
This function reallocates the net->buff buffer if necessary.
667
667
while (remain > 0)
669
669
/* First read is done with non blocking mode */
670
if ((long) (length= vio_read(net->vio, pos, remain)) <= 0L)
670
if ((long) (length= drizzleclient_vio_read(net->vio, pos, remain)) <= 0L)
672
const bool interrupted = vio_should_retry(net->vio);
672
const bool interrupted = drizzleclient_vio_should_retry(net->vio);
675
675
{ /* Probably in MIT threads */
676
676
if (retry_count++ < net->retry_count)
679
if (vio_errno(net->vio) == EINTR)
679
if (drizzleclient_vio_errno(net->vio) == EINTR)
683
683
len= packet_error;
684
684
net->error= 2; /* Close socket */
685
net->last_errno= (vio_was_interrupted(net->vio) ?
685
net->last_errno= (drizzleclient_vio_was_interrupted(net->vio) ?
686
686
CR_NET_READ_INTERRUPTED :
687
687
CR_NET_READ_ERROR);
688
688
ER(net->last_errno);
718
718
/* The necessary size of net->buff */
719
719
if (helping >= net->max_packet)
721
if (net_realloc(net,helping))
721
if (drizzleclient_net_realloc(net,helping))
723
723
len= packet_error; /* Return error and close connection */
903
903
len = ((uint32_t) (start_of_packet - first_packet_offset) - NET_HEADER_SIZE -
904
904
multi_byte_packet);
905
905
net->save_char= net->read_pos[len]; /* Must be saved */
906
net->read_pos[len]=0; /* Safeguard for drizzle_use_result */
906
net->read_pos[len]=0; /* Safeguard for drizzleclient_use_result */
912
void my_net_set_read_timeout(NET *net, uint32_t timeout)
912
void drizzleclient_net_set_read_timeout(NET *net, uint32_t timeout)
914
914
net->read_timeout= timeout;
917
vio_timeout(net->vio, 0, timeout);
917
drizzleclient_vio_timeout(net->vio, 0, timeout);
923
void my_net_set_write_timeout(NET *net, uint32_t timeout)
923
void drizzleclient_net_set_write_timeout(NET *net, uint32_t timeout)
925
925
net->write_timeout= timeout;
928
vio_timeout(net->vio, 1, timeout);
928
drizzleclient_vio_timeout(net->vio, 1, timeout);
935
935
@param net clear the state of the argument
938
void net_clear_error(NET *net)
938
void drizzleclient_drizzleclient_net_clear_error(NET *net)
940
940
net->last_errno= 0;
941
941
net->last_error[0]= '\0';
942
strcpy(net->sqlstate, sqlstate_get_not_error());
942
strcpy(net->sqlstate, drizzleclient_sqlstate_get_not_error());