~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzleclient/net_serv.cc

Moved the last of the libdrizzleclient calls into Protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#define __need_timeval 1
22
 
 
 
21
#include <drizzled/global.h>
23
22
#include "libdrizzle.h"
24
23
#include "libdrizzle_priv.h"
25
 
#include <libdrizzle/errmsg.h>
26
 
#include <vio/violite.h>
 
24
#include "errmsg.h"
 
25
#include "vio.h"
27
26
#include <assert.h>
28
27
#include <stdio.h>
29
28
#include <stdlib.h>
34
33
#include <sys/poll.h>
35
34
#include <zlib.h>
36
35
 
 
36
using namespace std;
 
37
 
37
38
/*
38
39
  The following handles the differences when this is linked between the
39
40
  client and the server.
44
45
*/
45
46
 
46
47
 
47
 
#define update_statistics(A)
48
 
#define thd_increment_bytes_sent(N)
49
 
 
50
 
#define TEST_BLOCKING        8
51
48
#define MAX_PACKET_LENGTH (256L*256L*256L-1)
52
 
#define MIN_COMPRESS_LENGTH             50      /* Don't compress small bl. */
53
49
 
54
50
static bool net_write_buff(NET *net, const unsigned char *packet, uint32_t len);
55
51
 
56
52
 
57
53
/** Init with packet info. */
58
54
 
59
 
bool my_net_init(NET *net, Vio* vio)
 
55
bool drizzleclient_net_init(NET *net, Vio* vio)
60
56
{
61
57
  net->vio = vio;
62
 
  my_net_local_init(net);            /* Set some limits */
 
58
  drizzleclient_net_local_init(net);            /* Set some limits */
63
59
  if (!(net->buff=(unsigned char*) malloc((size_t) net->max_packet+
64
 
                                  NET_HEADER_SIZE + COMP_HEADER_SIZE)))
 
60
                                          NET_HEADER_SIZE + COMP_HEADER_SIZE)))
65
61
    return(1);
66
62
  net->buff_end=net->buff+net->max_packet;
67
63
  net->error=0; net->return_status=0;
75
71
 
76
72
  if (vio != 0)                    /* If real connection */
77
73
  {
78
 
    net->fd  = vio_fd(vio);            /* For perl DBI/DBD */
79
 
    vio_fastsend(vio);
 
74
    net->fd  = drizzleclient_vio_fd(vio);            /* For perl DBI/DBD */
 
75
    drizzleclient_vio_fastsend(vio);
80
76
  }
81
77
  return(0);
82
78
}
83
79
 
84
 
bool net_init_sock(NET * net, int sock, int flags)
 
80
bool drizzleclient_net_init_sock(NET * net, int sock, int flags)
85
81
{
86
82
 
87
 
  Vio *vio_tmp= vio_new(sock, VIO_TYPE_TCPIP, flags);
88
 
  if (vio_tmp == NULL)
 
83
  Vio *drizzleclient_vio_tmp= drizzleclient_vio_new(sock, VIO_TYPE_TCPIP, flags);
 
84
  if (drizzleclient_vio_tmp == NULL)
89
85
    return true;
90
86
  else
91
 
    if (my_net_init(net, vio_tmp))
 
87
    if (drizzleclient_net_init(net, drizzleclient_vio_tmp))
92
88
    {
93
89
      /* Only delete the temporary vio if we didn't already attach it to the
94
90
       * NET object.
95
91
       */
96
 
      if (vio_tmp && (net->vio != vio_tmp))
97
 
        vio_delete(vio_tmp);
 
92
      if (drizzleclient_vio_tmp && (net->vio != drizzleclient_vio_tmp))
 
93
        drizzleclient_vio_delete(drizzleclient_vio_tmp);
98
94
      else
99
95
      {
100
96
        (void) shutdown(sock, SHUT_RDWR);
105
101
  return false;
106
102
}
107
103
 
108
 
void net_end(NET *net)
 
104
void drizzleclient_net_end(NET *net)
109
105
{
110
106
  if (net->buff != NULL)
111
107
    free(net->buff);
112
 
  net->buff=0;
 
108
  net->buff= NULL;
113
109
  return;
114
110
}
115
111
 
116
 
void net_close(NET *net)
 
112
void drizzleclient_net_close(NET *net)
117
113
{
118
114
  if (net->vio != NULL)
119
115
  {
120
 
    vio_delete(net->vio);
 
116
    drizzleclient_vio_delete(net->vio);
121
117
    net->vio= 0;
122
118
  }
123
119
}
124
120
 
125
 
bool net_peer_addr(NET *net, char *buf, uint16_t *port, size_t buflen)
126
 
{
127
 
  return vio_peer_addr(net->vio, buf, port, buflen);
128
 
}
129
 
 
130
 
void net_keepalive(NET *net, bool flag)
131
 
{
132
 
  vio_keepalive(net->vio, flag);
133
 
}
134
 
 
135
 
int net_get_sd(NET *net)
 
121
bool drizzleclient_net_peer_addr(NET *net, char *buf, uint16_t *port, size_t buflen)
 
122
{
 
123
  return drizzleclient_vio_peer_addr(net->vio, buf, port, buflen);
 
124
}
 
125
 
 
126
void drizzleclient_net_keepalive(NET *net, bool flag)
 
127
{
 
128
  drizzleclient_vio_keepalive(net->vio, flag);
 
129
}
 
130
 
 
131
int drizzleclient_net_get_sd(NET *net)
136
132
{
137
133
  return net->vio->sd;
138
134
}
139
135
 
140
 
bool net_should_close(NET *net)
 
136
bool drizzleclient_net_should_close(NET *net)
141
137
{
142
138
  return net->error || (net->vio == 0);
143
139
}
144
140
 
145
 
bool net_more_data(NET *net)
 
141
bool drizzleclient_net_more_data(NET *net)
146
142
{
147
143
  return (net->vio == 0 || net->vio->read_pos < net->vio->read_end);
148
144
}
149
145
 
150
146
/** Realloc the packet buffer. */
151
147
 
152
 
bool net_realloc(NET *net, size_t length)
 
148
bool drizzleclient_net_realloc(NET *net, size_t length)
153
149
{
154
150
  unsigned char *buff;
155
151
  size_t pkt_length;
215
211
   Read from socket until there is nothing more to read. Discard
216
212
   what is read.
217
213
 
218
 
   If there is anything when to read 'net_clear' is called this
 
214
   If there is anything when to read 'drizzleclient_net_clear' is called this
219
215
   normally indicates an error in the protocol.
220
216
 
221
217
   When connection is properly closed (for TCP it means with
226
222
   @param clear_buffer           if <> 0, then clear all data from comm buff
227
223
*/
228
224
 
229
 
void net_clear(NET *net, bool clear_buffer)
 
225
void drizzleclient_net_clear(NET *net, bool clear_buffer)
230
226
{
231
227
  if (clear_buffer)
232
228
  {
233
229
    while (net_data_is_ready(net->vio->sd) > 0)
234
230
    {
235
231
      /* The socket is ready */
236
 
      if (vio_read(net->vio, net->buff,
 
232
      if (drizzleclient_vio_read(net->vio, net->buff,
237
233
                   (size_t) net->max_packet) <= 0)
238
234
      {
239
235
        net->error= 2;
249
245
 
250
246
/** Flush write_buffer if not empty. */
251
247
 
252
 
bool net_flush(NET *net)
 
248
bool drizzleclient_net_flush(NET *net)
253
249
{
254
250
  bool error= 0;
255
251
  if (net->buff != net->write_pos)
256
252
  {
257
 
    error=net_real_write(net, net->buff,
 
253
    error=drizzleclient_net_real_write(net, net->buff,
258
254
                         (size_t) (net->write_pos - net->buff)) ? 1 : 0;
259
255
    net->write_pos=net->buff;
260
256
  }
280
276
*/
281
277
 
282
278
bool
283
 
my_net_write(NET *net,const unsigned char *packet,size_t len)
 
279
drizzleclient_net_write(NET *net,const unsigned char *packet,size_t len)
284
280
{
285
281
  unsigned char buff[NET_HEADER_SIZE];
286
282
  if (unlikely(!net->vio)) /* nowhere to write */
337
333
*/
338
334
 
339
335
bool
340
 
net_write_command(NET *net,unsigned char command,
 
336
drizzleclient_net_write_command(NET *net,unsigned char command,
341
337
                  const unsigned char *header, size_t head_len,
342
338
                  const unsigned char *packet, size_t len)
343
339
{
371
367
  buff[3]= (unsigned char) net->pkt_nr++;
372
368
  return((net_write_buff(net, buff, header_size) ||
373
369
          (head_len && net_write_buff(net, header, head_len)) ||
374
 
          net_write_buff(net, packet, len) || net_flush(net)) ? 1 : 0 );
 
370
          net_write_buff(net, packet, len) || drizzleclient_net_flush(net)) ? 1 : 0 );
375
371
}
376
372
 
377
373
/**
389
385
   @param len        Length of packet
390
386
 
391
387
   @note
392
 
   The cached buffer can be sent as it is with 'net_flush()'.
 
388
   The cached buffer can be sent as it is with 'drizzleclient_net_flush()'.
393
389
   In this code we have to be careful to not send a packet longer than
394
 
   MAX_PACKET_LENGTH to net_real_write() if we are using the compressed
 
390
   MAX_PACKET_LENGTH to drizzleclient_net_real_write() if we are using the compressed
395
391
   protocol as we store the length of the compressed packet in 3 bytes.
396
392
 
397
393
   @retval
415
411
    {
416
412
      /* Fill up already used packet and write it */
417
413
      memcpy(net->write_pos,packet,left_length);
418
 
      if (net_real_write(net, net->buff,
 
414
      if (drizzleclient_net_real_write(net, net->buff,
419
415
                         (size_t) (net->write_pos - net->buff) + left_length))
420
416
        return 1;
421
417
      net->write_pos= net->buff;
431
427
      left_length= MAX_PACKET_LENGTH;
432
428
      while (len > left_length)
433
429
      {
434
 
        if (net_real_write(net, packet, left_length))
 
430
        if (drizzleclient_net_real_write(net, packet, left_length))
435
431
          return 1;
436
432
        packet+= left_length;
437
433
        len-= left_length;
438
434
      }
439
435
    }
440
436
    if (len > net->max_packet)
441
 
      return net_real_write(net, packet, len) ? 1 : 0;
 
437
      return drizzleclient_net_real_write(net, packet, len) ? 1 : 0;
442
438
    /* Send out rest of the blocks as full sized blocks */
443
439
  }
444
440
  memcpy(net->write_pos,packet,len);
460
456
  in the server, yield to another process and come back later.
461
457
*/
462
458
int
463
 
net_real_write(NET *net,const unsigned char *packet, size_t len)
 
459
drizzleclient_net_real_write(NET *net, const unsigned char *packet, size_t len)
464
460
{
465
461
  size_t length;
466
 
  const unsigned char *pos,*end;
 
462
  const unsigned char *pos, *end;
467
463
  uint32_t retry_count= 0;
468
464
 
469
465
  /* Backup of the original SO_RCVTIMEO timeout */
529
525
  if (net->write_timeout)
530
526
  {
531
527
    struct timespec waittime;
532
 
    socklen_t length;
 
528
    socklen_t time_len;
533
529
 
534
530
    waittime.tv_sec= net->write_timeout;
535
531
    waittime.tv_nsec= 0;
536
532
 
537
533
    memset(&backtime, 0, sizeof(struct timespec));
538
 
    length= sizeof(struct timespec);
 
534
    time_len= sizeof(struct timespec);
539
535
    error= getsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
540
 
                      &backtime, &length);
 
536
                      &backtime, &time_len);
541
537
    if (error != 0)
542
538
    {
543
539
      perror("getsockopt");
554
550
  /* Loop until we have read everything */
555
551
  while (pos != end)
556
552
  {
557
 
    if ((long) (length= vio_write(net->vio,pos,(size_t) (end-pos))) <= 0)
 
553
    assert(pos);
 
554
    if ((long) (length= drizzleclient_vio_write(net->vio, pos, (size_t) (end-pos))) <= 0)
558
555
    {
559
 
      const bool interrupted= vio_should_retry(net->vio);
 
556
      const bool interrupted= drizzleclient_vio_should_retry(net->vio);
560
557
      /*
561
558
        If we read 0, or we were interrupted this means that
562
559
        we need to switch to blocking mode and wait until the timeout
566
563
      {
567
564
        bool old_mode;
568
565
 
569
 
        while (vio_blocking(net->vio, true, &old_mode) < 0)
 
566
        while (drizzleclient_vio_blocking(net->vio, true, &old_mode) < 0)
570
567
        {
571
 
          if (vio_should_retry(net->vio) && retry_count++ < net->retry_count)
 
568
          if (drizzleclient_vio_should_retry(net->vio) && retry_count++ < net->retry_count)
572
569
            continue;
573
570
          net->error= 2;                     /* Close socket */
574
571
          net->last_errno= CR_NET_PACKET_TOO_LARGE;
583
580
          continue;
584
581
      }
585
582
 
586
 
      if (vio_errno(net->vio) == EINTR)
 
583
      if (drizzleclient_vio_errno(net->vio) == EINTR)
587
584
      {
588
585
        continue;
589
586
      }
593
590
      break;
594
591
    }
595
592
    pos+=length;
596
 
    update_statistics(thd_increment_bytes_sent(length));
597
593
  }
598
594
end:
599
595
  if ((net->compress) && (packet != NULL))
612
608
 
613
609
/**
614
610
   Reads one packet to net->buff + net->where_b.
615
 
   Long packets are handled by my_net_read().
 
611
   Long packets are handled by drizzleclient_net_read().
616
612
   This function reallocates the net->buff buffer if necessary.
617
613
 
618
614
   @return
625
621
  unsigned char *pos;
626
622
  size_t length;
627
623
  uint32_t i,retry_count=0;
628
 
  uint32_t len=packet_error;
 
624
  size_t len=packet_error;
629
625
  uint32_t remain= (net->compress ? NET_HEADER_SIZE+COMP_HEADER_SIZE :
630
626
                    NET_HEADER_SIZE);
631
627
 
638
634
  *complen = 0;
639
635
 
640
636
  net->reading_or_writing= 1;
641
 
  /* Read timeout is set in my_net_set_read_timeout */
 
637
  /* Read timeout is set in drizzleclient_net_set_read_timeout */
642
638
 
643
639
  pos = net->buff + net->where_b;        /* net->packet -4 */
644
640
 
648
644
  if (net->read_timeout)
649
645
  {
650
646
    struct timespec waittime;
651
 
    socklen_t length;
 
647
    socklen_t time_len;
652
648
 
653
649
    waittime.tv_sec= net->read_timeout;
654
650
    waittime.tv_nsec= 0;
655
651
 
656
652
    memset(&backtime, 0, sizeof(struct timespec));
657
 
    length= sizeof(struct timespec);
 
653
    time_len= sizeof(struct timespec);
658
654
    error= getsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
659
 
                      &backtime, &length);
 
655
                      &backtime, &time_len);
660
656
    if (error != 0)
661
657
    {
662
658
      perror("getsockopt");
673
669
    while (remain > 0)
674
670
    {
675
671
      /* First read is done with non blocking mode */
676
 
      if ((long) (length= vio_read(net->vio, pos, remain)) <= 0L)
 
672
      if ((long) (length= drizzleclient_vio_read(net->vio, pos, remain)) <= 0L)
677
673
      {
678
 
        const bool interrupted = vio_should_retry(net->vio);
 
674
        const bool interrupted = drizzleclient_vio_should_retry(net->vio);
679
675
 
680
676
        if (interrupted)
681
677
        {                    /* Probably in MIT threads */
682
678
          if (retry_count++ < net->retry_count)
683
679
            continue;
684
680
        }
685
 
        if (vio_errno(net->vio) == EINTR)
 
681
        if (drizzleclient_vio_errno(net->vio) == EINTR)
686
682
        {
687
683
          continue;
688
684
        }
689
685
        len= packet_error;
690
686
        net->error= 2;                /* Close socket */
691
 
        net->last_errno= (vio_was_interrupted(net->vio) ?
 
687
        net->last_errno= (drizzleclient_vio_was_interrupted(net->vio) ?
692
688
                          CR_NET_READ_INTERRUPTED :
693
689
                          CR_NET_READ_ERROR);
694
690
        ER(net->last_errno);
696
692
      }
697
693
      remain -= (uint32_t) length;
698
694
      pos+= length;
699
 
      update_statistics(thd_increment_bytes_received(length));
700
695
    }
701
696
    if (i == 0)
702
697
    {                    /* First parts is packet length */
725
720
      /* The necessary size of net->buff */
726
721
      if (helping >= net->max_packet)
727
722
      {
728
 
        if (net_realloc(net,helping))
 
723
        if (drizzleclient_net_realloc(net,helping))
729
724
        {
730
725
          len= packet_error;          /* Return error and close connection */
731
726
          goto end;
766
761
*/
767
762
 
768
763
uint32_t
769
 
my_net_read(NET *net)
 
764
drizzleclient_net_read(NET *net)
770
765
{
771
766
  size_t len, complen;
772
767
 
790
785
    }
791
786
    net->read_pos = net->buff + net->where_b;
792
787
    if (len != packet_error)
793
 
      net->read_pos[len]=0;        /* Safeguard for drizzle_use_result */
 
788
      net->read_pos[len]=0;        /* Safeguard for drizzleclient_use_result */
794
789
    return len;
795
790
  }
796
791
  else
910
905
    len = ((uint32_t) (start_of_packet - first_packet_offset) - NET_HEADER_SIZE -
911
906
           multi_byte_packet);
912
907
    net->save_char= net->read_pos[len];    /* Must be saved */
913
 
    net->read_pos[len]=0;        /* Safeguard for drizzle_use_result */
 
908
    net->read_pos[len]=0;        /* Safeguard for drizzleclient_use_result */
914
909
  }
915
910
  return len;
916
911
  }
917
912
 
918
913
 
919
 
void my_net_set_read_timeout(NET *net, uint32_t timeout)
 
914
void drizzleclient_net_set_read_timeout(NET *net, uint32_t timeout)
920
915
{
921
916
  net->read_timeout= timeout;
922
917
#ifndef __sun
923
918
  if (net->vio)
924
 
    vio_timeout(net->vio, 0, timeout);
 
919
    drizzleclient_vio_timeout(net->vio, 0, timeout);
925
920
#endif
926
921
  return;
927
922
}
928
923
 
929
924
 
930
 
void my_net_set_write_timeout(NET *net, uint32_t timeout)
 
925
void drizzleclient_net_set_write_timeout(NET *net, uint32_t timeout)
931
926
{
932
927
  net->write_timeout= timeout;
933
928
#ifndef __sun
934
929
  if (net->vio)
935
 
    vio_timeout(net->vio, 1, timeout);
 
930
    drizzleclient_vio_timeout(net->vio, 1, timeout);
936
931
#endif
937
932
  return;
938
933
}
942
937
  @param net  clear the state of the argument
943
938
*/
944
939
 
945
 
void net_clear_error(NET *net)
 
940
void drizzleclient_drizzleclient_net_clear_error(NET *net)
946
941
{
947
942
  net->last_errno= 0;
948
943
  net->last_error[0]= '\0';
949
 
  strcpy(net->sqlstate, sqlstate_get_not_error());
 
944
  strcpy(net->sqlstate, drizzleclient_sqlstate_get_not_error());
950
945
}
951
946