~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/net_serv.c

  • Committer: Stewart Smith
  • Date: 2008-09-25 10:04:06 UTC
  • mto: This revision was merged to the branch mainline in revision 408.
  • Revision ID: stewart@flamingspork.com-20080925100406-hld92f4dr4nuar3a
Move compression functions (compress, uncompress and compressed_length) out into modules and fix test

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
 
 
23
21
#include "libdrizzle.h"
24
22
#include "libdrizzle_priv.h"
25
23
#include <libdrizzle/errmsg.h>
343
341
{
344
342
  uint32_t length=len+1+head_len;            /* 1 extra byte for command */
345
343
  unsigned char buff[NET_HEADER_SIZE+1];
346
 
  uint32_t header_size=NET_HEADER_SIZE+1;
 
344
  uint header_size=NET_HEADER_SIZE+1;
347
345
 
348
346
  buff[4]=command;                /* For first packet */
349
347
 
464
462
{
465
463
  size_t length;
466
464
  const unsigned char *pos,*end;
467
 
  uint32_t retry_count= 0;
 
465
  uint retry_count= 0;
468
466
 
469
467
  /* Backup of the original SO_RCVTIMEO timeout */
470
 
#ifndef __sun
471
 
  struct timespec backtime;
 
468
  struct timeval backtime;
472
469
  int error;
473
 
#endif
474
470
 
475
471
  if (net->error == 2)
476
472
    return(-1);                /* socket can't be used */
480
476
  {
481
477
    size_t complen;
482
478
    unsigned char *b;
483
 
    const uint32_t header_length=NET_HEADER_SIZE+COMP_HEADER_SIZE;
 
479
    const uint header_length=NET_HEADER_SIZE+COMP_HEADER_SIZE;
484
480
    if (!(b= (unsigned char*) malloc(len + NET_HEADER_SIZE +
485
481
                             COMP_HEADER_SIZE)))
486
482
    {
524
520
    packet= b;
525
521
  }
526
522
 
527
 
#ifndef __sun
528
523
  /* Check for error, currently assert */
529
524
  if (net->write_timeout)
530
525
  {
531
 
    struct timespec waittime;
 
526
    struct timeval waittime;
532
527
    socklen_t length;
533
528
 
534
529
    waittime.tv_sec= net->write_timeout;
535
 
    waittime.tv_nsec= 0;
 
530
    waittime.tv_usec= 0;
536
531
 
537
 
    memset(&backtime, 0, sizeof(struct timespec));
538
 
    length= sizeof(struct timespec);
 
532
    memset(&backtime, 0, sizeof(struct timeval));
 
533
    length= sizeof(struct timeval);
539
534
    error= getsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
540
535
                      &backtime, &length);
541
536
    if (error != 0)
544
539
      assert(error == 0);
545
540
    }
546
541
    error= setsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
547
 
                      &waittime, (socklen_t)sizeof(struct timespec));
 
542
                      &waittime, (socklen_t)sizeof(struct timeval));
548
543
    assert(error == 0);
549
544
  }
550
 
#endif
551
 
 
552
545
  pos= packet;
553
546
  end=pos+len;
554
547
  /* Loop until we have read everything */
600
593
    free((char*) packet);
601
594
  net->reading_or_writing=0;
602
595
 
603
 
#ifndef __sun
604
596
  if (net->write_timeout)
605
597
    error= setsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
606
 
                      &backtime, (socklen_t)sizeof(struct timespec));
607
 
#endif
 
598
                      &backtime, (socklen_t)sizeof(struct timeval));
608
599
 
609
600
  return(((int) (pos != end)));
610
601
}
624
615
{
625
616
  unsigned char *pos;
626
617
  size_t length;
627
 
  uint32_t i,retry_count=0;
 
618
  uint i,retry_count=0;
628
619
  uint32_t len=packet_error;
629
620
  uint32_t remain= (net->compress ? NET_HEADER_SIZE+COMP_HEADER_SIZE :
630
621
                    NET_HEADER_SIZE);
631
 
 
632
 
#ifndef __sun
633
622
  /* Backup of the original SO_RCVTIMEO timeout */
634
 
  struct timespec backtime;
 
623
  struct timeval backtime;
635
624
  int error= 0;
636
 
#endif
637
625
 
638
626
  *complen = 0;
639
627
 
643
631
  pos = net->buff + net->where_b;        /* net->packet -4 */
644
632
 
645
633
 
646
 
#ifndef __sun
647
634
  /* Check for error, currently assert */
648
635
  if (net->read_timeout)
649
636
  {
650
 
    struct timespec waittime;
 
637
    struct timeval waittime;
651
638
    socklen_t length;
652
639
 
653
640
    waittime.tv_sec= net->read_timeout;
654
 
    waittime.tv_nsec= 0;
 
641
    waittime.tv_usec= 0;
655
642
 
656
 
    memset(&backtime, 0, sizeof(struct timespec));
657
 
    length= sizeof(struct timespec);
 
643
    memset(&backtime, 0, sizeof(struct timeval));
 
644
    length= sizeof(struct timeval);
658
645
    error= getsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
659
646
                      &backtime, &length);
660
647
    if (error != 0)
663
650
      assert(error == 0);
664
651
    }
665
652
    error= setsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
666
 
                      &waittime, (socklen_t)sizeof(struct timespec));
 
653
                      &waittime, (socklen_t)sizeof(struct timeval));
667
654
    assert(error == 0);
668
655
  }
669
 
#endif
670
656
 
671
657
  for (i= 0; i < 2 ; i++)
672
658
  {
737
723
  }
738
724
 
739
725
end:
740
 
#ifndef __sun
741
726
  if  (net->read_timeout)
742
727
    error= setsockopt(net->vio->sd, SOL_SOCKET, SO_RCVTIMEO,
743
 
                      &backtime, (socklen_t)sizeof(struct timespec));
 
728
                      &backtime, (socklen_t)sizeof(struct timeval));
744
729
  assert(error == 0);
745
 
#endif
746
730
  net->reading_or_writing= 0;
747
731
 
748
732
  return(len);
800
784
    uint32_t buf_length;
801
785
    uint32_t start_of_packet;
802
786
    uint32_t first_packet_offset;
803
 
    uint32_t read_length, multi_byte_packet=0;
 
787
    uint read_length, multi_byte_packet=0;
804
788
 
805
789
    if (net->remain_in_buf)
806
790
    {
916
900
  }
917
901
 
918
902
 
919
 
void my_net_set_read_timeout(NET *net, uint32_t timeout)
 
903
void my_net_set_read_timeout(NET *net, uint timeout)
920
904
{
921
905
  net->read_timeout= timeout;
922
 
#ifndef __sun
923
906
  if (net->vio)
924
907
    vio_timeout(net->vio, 0, timeout);
925
 
#endif
926
908
  return;
927
909
}
928
910
 
929
911
 
930
 
void my_net_set_write_timeout(NET *net, uint32_t timeout)
 
912
void my_net_set_write_timeout(NET *net, uint timeout)
931
913
{
932
914
  net->write_timeout= timeout;
933
 
#ifndef __sun
934
915
  if (net->vio)
935
916
    vio_timeout(net->vio, 1, timeout);
936
 
#endif
937
917
  return;
938
918
}
939
919
/**