~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/net_serv.c

  • Committer: Monty Taylor
  • Date: 2008-09-16 06:40:44 UTC
  • mfrom: (390.1.7 client-split)
  • Revision ID: monty@inaugust.com-20080916064044-vbgmaf36cvm8jufx
Merged in from client-split.

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
 
#include <drizzled/global.h>
22
21
#include "libdrizzle.h"
 
22
#include "libdrizzle_priv.h"
23
23
#include <libdrizzle/errmsg.h>
24
24
#include <vio/violite.h>
 
25
#include <assert.h>
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
#include <string.h>
25
29
#include <signal.h>
26
30
#include <errno.h>
 
31
#include <sys/socket.h>
27
32
#include <sys/poll.h>
28
33
#include <zlib.h>
29
34
 
53
58
{
54
59
  net->vio = vio;
55
60
  my_net_local_init(net);            /* Set some limits */
56
 
  if (!(net->buff=(uchar*) malloc((size_t) net->max_packet+
 
61
  if (!(net->buff=(unsigned char*) malloc((size_t) net->max_packet+
57
62
                                  NET_HEADER_SIZE + COMP_HEADER_SIZE)))
58
63
    return(1);
59
64
  net->buff_end=net->buff+net->max_packet;
144
149
 
145
150
bool net_realloc(NET *net, size_t length)
146
151
{
147
 
  uchar *buff;
 
152
  unsigned char *buff;
148
153
  size_t pkt_length;
149
154
 
150
155
  if (length >= net->max_packet_size)
159
164
    We must allocate some extra bytes for the end 0 and to be able to
160
165
    read big compressed blocks
161
166
  */
162
 
  if (!(buff= (uchar*) realloc((char*) net->buff, pkt_length +
 
167
  if (!(buff= (unsigned char*) realloc((char*) net->buff, pkt_length +
163
168
                               NET_HEADER_SIZE + COMP_HEADER_SIZE)))
164
169
  {
165
170
    /* @todo: 1 and 2 codes are identical. */
247
252
  bool error= 0;
248
253
  if (net->buff != net->write_pos)
249
254
  {
250
 
    error=test(net_real_write(net, net->buff,
251
 
                              (size_t) (net->write_pos - net->buff)));
 
255
    error=net_real_write(net, net->buff,
 
256
                         (size_t) (net->write_pos - net->buff)) ? 1 : 0;
252
257
    net->write_pos=net->buff;
253
258
  }
254
259
  /* Sync packet number if using compression */
273
278
*/
274
279
 
275
280
bool
276
 
my_net_write(NET *net,const uchar *packet,size_t len)
 
281
my_net_write(NET *net,const unsigned char *packet,size_t len)
277
282
{
278
 
  uchar buff[NET_HEADER_SIZE];
 
283
  unsigned char buff[NET_HEADER_SIZE];
279
284
  if (unlikely(!net->vio)) /* nowhere to write */
280
285
    return 0;
281
286
  /*
287
292
  {
288
293
    const uint32_t z_size = MAX_PACKET_LENGTH;
289
294
    int3store(buff, z_size);
290
 
    buff[3]= (uchar) net->pkt_nr++;
 
295
    buff[3]= (unsigned char) net->pkt_nr++;
291
296
    if (net_write_buff(net, buff, NET_HEADER_SIZE) ||
292
297
        net_write_buff(net, packet, z_size))
293
298
      return 1;
296
301
  }
297
302
  /* Write last packet */
298
303
  int3store(buff,len);
299
 
  buff[3]= (uchar) net->pkt_nr++;
 
304
  buff[3]= (unsigned char) net->pkt_nr++;
300
305
  if (net_write_buff(net, buff, NET_HEADER_SIZE))
301
306
    return 1;
302
 
  return test(net_write_buff(net,packet,len));
 
307
  return net_write_buff(net,packet,len) ? 1 : 0;
303
308
}
304
309
 
305
310
/**
330
335
*/
331
336
 
332
337
bool
333
 
net_write_command(NET *net,uchar command,
334
 
                  const uchar *header, size_t head_len,
335
 
                  const uchar *packet, size_t len)
 
338
net_write_command(NET *net,unsigned char command,
 
339
                  const unsigned char *header, size_t head_len,
 
340
                  const unsigned char *packet, size_t len)
336
341
{
337
342
  uint32_t length=len+1+head_len;            /* 1 extra byte for command */
338
 
  uchar buff[NET_HEADER_SIZE+1];
 
343
  unsigned char buff[NET_HEADER_SIZE+1];
339
344
  uint header_size=NET_HEADER_SIZE+1;
340
345
 
341
346
  buff[4]=command;                /* For first packet */
347
352
    do
348
353
    {
349
354
      int3store(buff, MAX_PACKET_LENGTH);
350
 
      buff[3]= (uchar) net->pkt_nr++;
 
355
      buff[3]= (unsigned char) net->pkt_nr++;
351
356
      if (net_write_buff(net, buff, header_size) ||
352
357
          net_write_buff(net, header, head_len) ||
353
358
          net_write_buff(net, packet, len))
361
366
    len=length;                    /* Data left to be written */
362
367
  }
363
368
  int3store(buff,length);
364
 
  buff[3]= (uchar) net->pkt_nr++;
365
 
  return(test(net_write_buff(net, buff, header_size) ||
366
 
              (head_len && net_write_buff(net, header, head_len)) ||
367
 
              net_write_buff(net, packet, len) || net_flush(net)));
 
369
  buff[3]= (unsigned char) net->pkt_nr++;
 
370
  return((net_write_buff(net, buff, header_size) ||
 
371
          (head_len && net_write_buff(net, header, head_len)) ||
 
372
          net_write_buff(net, packet, len) || net_flush(net)) ? 1 : 0 );
368
373
}
369
374
 
370
375
/**
453
458
  in the server, yield to another process and come back later.
454
459
*/
455
460
int
456
 
net_real_write(NET *net,const uchar *packet, size_t len)
 
461
net_real_write(NET *net,const unsigned char *packet, size_t len)
457
462
{
458
463
  size_t length;
459
 
  const uchar *pos,*end;
 
464
  const unsigned char *pos,*end;
460
465
  uint retry_count= 0;
461
466
 
462
467
  /* Backup of the original SO_RCVTIMEO timeout */
470
475
  if (net->compress)
471
476
  {
472
477
    size_t complen;
473
 
    uchar *b;
 
478
    unsigned char *b;
474
479
    const uint header_length=NET_HEADER_SIZE+COMP_HEADER_SIZE;
475
 
    if (!(b= (uchar*) malloc(len + NET_HEADER_SIZE +
 
480
    if (!(b= (unsigned char*) malloc(len + NET_HEADER_SIZE +
476
481
                             COMP_HEADER_SIZE)))
477
482
    {
478
483
      net->error= 2;
510
515
    }
511
516
    int3store(&b[NET_HEADER_SIZE],complen);
512
517
    int3store(b,len);
513
 
    b[3]=(uchar) (net->compress_pkt_nr++);
 
518
    b[3]=(unsigned char) (net->compress_pkt_nr++);
514
519
    len+= header_length;
515
520
    packet= b;
516
521
  }
571
576
          continue;
572
577
      }
573
578
 
574
 
      if (vio_errno(net->vio) == SOCKET_EINTR)
 
579
      if (vio_errno(net->vio) == EINTR)
575
580
      {
576
581
        continue;
577
582
      }
608
613
static uint32_t
609
614
my_real_read(NET *net, size_t *complen)
610
615
{
611
 
  uchar *pos;
 
616
  unsigned char *pos;
612
617
  size_t length;
613
618
  uint i,retry_count=0;
614
619
  uint32_t len=packet_error;
663
668
          if (retry_count++ < net->retry_count)
664
669
            continue;
665
670
        }
666
 
        if (vio_errno(net->vio) == SOCKET_EINTR)
 
671
        if (vio_errno(net->vio) == EINTR)
667
672
        {
668
673
          continue;
669
674
        }
683
688
    {                    /* First parts is packet length */
684
689
      uint32_t helping;
685
690
 
686
 
      if (net->buff[net->where_b + 3] != (uchar) net->pkt_nr)
 
691
      if (net->buff[net->where_b + 3] != (unsigned char) net->pkt_nr)
687
692
      {
688
693
        len= packet_error;
689
694
        /* Not a NET error on the client. XXX: why? */
911
916
    vio_timeout(net->vio, 1, timeout);
912
917
  return;
913
918
}
 
919
/**
 
920
  Clear possible error state of struct NET
 
921
 
 
922
  @param net  clear the state of the argument
 
923
*/
 
924
 
 
925
void net_clear_error(NET *net)
 
926
{
 
927
  net->last_errno= 0;
 
928
  net->last_error[0]= '\0';
 
929
  strcpy(net->sqlstate, sqlstate_get_not_error());
 
930
}
 
931