~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/oldlibdrizzle/net_serv.cc

  • Committer: Brian Aker
  • Date: 2009-08-15 00:59:30 UTC
  • mfrom: (1115.1.7 merge)
  • Revision ID: brian@gaz-20090815005930-q47yenjrq1esiwsz
Merge of Trond + Brian

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 "config.h"
 
21
#include <drizzled/global.h>
22
22
#include <drizzled/session.h>
23
 
 
 
23
#include "libdrizzle.h"
 
24
#include "libdrizzle_priv.h"
 
25
#include "errmsg.h"
 
26
#include "vio.h"
24
27
#include <assert.h>
25
28
#include <stdio.h>
26
29
#include <stdlib.h>
30
33
#include <sys/socket.h>
31
34
#include <sys/poll.h>
32
35
#include <zlib.h>
 
36
 
33
37
#include <algorithm>
34
38
 
35
 
#include "errmsg.h"
36
 
#include "vio.h"
37
 
#include "net_serv.h"
38
 
 
39
39
using namespace std;
40
40
 
41
 
namespace drizzle_protocol
42
 
{
43
 
 
44
41
/*
45
42
  The following handles the differences when this is linked between the
46
43
  client and the server.
50
47
  can't normally do this the client should have a bigger max_allowed_packet.
51
48
*/
52
49
 
53
 
  /* Constants when using compression */
54
 
#define NET_HEADER_SIZE 4               /* standard header size */
55
 
#define COMP_HEADER_SIZE 3              /* compression header extra size */
56
50
 
57
51
#define MAX_PACKET_LENGTH (256L*256L*256L-1)
58
 
const char  *not_error_sqlstate= "00000";
59
52
 
60
53
static bool net_write_buff(NET *net, const unsigned char *packet, uint32_t len);
61
 
static int drizzleclient_net_real_write(NET *net, const unsigned char *packet, size_t len);
 
54
 
 
55
void drizzleclient_net_local_init(NET *net)
 
56
{
 
57
  net->max_packet= (uint32_t) global_system_variables.net_buffer_length;
 
58
  net->max_packet_size= max(global_system_variables.net_buffer_length,
 
59
                             global_system_variables.max_allowed_packet);
 
60
}
62
61
 
63
62
/** Init with packet info. */
64
63
 
65
 
bool drizzleclient_net_init(NET *net, Vio* vio, uint32_t buffer_length)
 
64
bool drizzleclient_net_init(NET *net, Vio* vio)
66
65
{
67
66
  net->vio = vio;
68
 
  net->max_packet= (uint32_t) buffer_length;
69
 
  net->max_packet_size= max(buffer_length,
70
 
                            drizzled::global_system_variables.max_allowed_packet);
71
 
 
 
67
  drizzleclient_net_local_init(net);            /* Set some limits */
72
68
  if (!(net->buff=(unsigned char*) malloc((size_t) net->max_packet+
73
69
                                          NET_HEADER_SIZE + COMP_HEADER_SIZE)))
74
70
    return(1);
90
86
  return(0);
91
87
}
92
88
 
93
 
bool drizzleclient_net_init_sock(NET * net, int sock, int flags,
94
 
                                 uint32_t buffer_length)
 
89
bool drizzleclient_net_init_sock(NET * net, int sock, int flags)
95
90
{
96
91
 
97
92
  Vio *drizzleclient_vio_tmp= drizzleclient_vio_new(sock, VIO_TYPE_TCPIP, flags);
98
93
  if (drizzleclient_vio_tmp == NULL)
99
94
    return true;
100
95
  else
101
 
    if (drizzleclient_net_init(net, drizzleclient_vio_tmp, buffer_length))
 
96
    if (drizzleclient_net_init(net, drizzleclient_vio_tmp))
102
97
    {
103
98
      /* Only delete the temporary vio if we didn't already attach it to the
104
99
       * NET object.
147
142
  return net->vio->sd;
148
143
}
149
144
 
 
145
bool drizzleclient_net_should_close(NET *net)
 
146
{
 
147
  return net->error || (net->vio == 0);
 
148
}
 
149
 
150
150
bool drizzleclient_net_more_data(NET *net)
151
151
{
152
152
  return (net->vio == 0 || net->vio->read_pos < net->vio->read_end);
154
154
 
155
155
/** Realloc the packet buffer. */
156
156
 
157
 
static bool drizzleclient_net_realloc(NET *net, size_t length)
 
157
bool drizzleclient_net_realloc(NET *net, size_t length)
158
158
{
159
159
  unsigned char *buff;
160
160
  size_t pkt_length;
464
464
  TODO: rewrite this in a manner to do non-block writes. If a write can not be made, and we are
465
465
  in the server, yield to another process and come back later.
466
466
*/
467
 
static int
 
467
int
468
468
drizzleclient_net_real_write(NET *net, const unsigned char *packet, size_t len)
469
469
{
470
470
  size_t length;
533
533
    assert(pos);
534
534
    if ((long) (length= drizzleclient_vio_write(net->vio, pos, (size_t) (end-pos))) <= 0)
535
535
    {
536
 
     /*
537
 
      * We could end up here with net->vio == NULL
538
 
      * See LP bug#436685
539
 
      * If that is the case, we exit the while loop
540
 
      */
541
 
      if (net->vio == NULL)
542
 
        break;
543
 
      
544
536
      const bool interrupted= drizzleclient_vio_should_retry(net->vio);
545
537
      /*
546
538
        If we read 0, or we were interrupted this means that
621
613
      /* First read is done with non blocking mode */
622
614
      if ((long) (length= drizzleclient_vio_read(net->vio, pos, remain)) <= 0L)
623
615
      {
624
 
        if (net->vio == NULL)
625
 
          goto end;
626
 
 
627
616
        const bool interrupted = drizzleclient_vio_should_retry(net->vio);
628
617
 
629
618
        if (interrupted)
888
877
{
889
878
  net->last_errno= 0;
890
879
  net->last_error[0]= '\0';
891
 
  strcpy(net->sqlstate, not_error_sqlstate);
 
880
  strcpy(net->sqlstate, drizzleclient_sqlstate_get_not_error());
892
881
}
893
882
 
894
 
} /* namespace drizzle_protocol */