~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/mysql_protocol/mysql_protocol.cc

  • Committer: Mark Atwood
  • Date: 2011-06-14 06:10:57 UTC
  • mfrom: (2318.2.19 rf)
  • Revision ID: me@mark.atwood.name-20110614061057-u08az4cidd4l7blm
mergeĀ lp:~olafvdspek/drizzle/refactor4

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
static const uint32_t random_max= 0x3FFFFFFF;
62
62
static const double random_max_double= (double)0x3FFFFFFF;
63
63
 
64
 
 
65
 
ProtocolCounters *ListenMySQLProtocol::mysql_counters= new ProtocolCounters();
66
 
 
67
 
ListenMySQLProtocol::~ListenMySQLProtocol()
68
 
{ }
 
64
ProtocolCounters ListenMySQLProtocol::mysql_counters;
69
65
 
70
66
void ListenMySQLProtocol::addCountersToTable()
71
67
{
72
 
  counters.push_back(new drizzled::plugin::ListenCounter(new std::string("connection_count"), &getCounters()->connectionCount));
73
 
  counters.push_back(new drizzled::plugin::ListenCounter(new std::string("connected"), &getCounters()->connected));
74
 
  counters.push_back(new drizzled::plugin::ListenCounter(new std::string("failed_connections"), &getCounters()->failedConnections));
 
68
  counters.push_back(new drizzled::plugin::ListenCounter(new std::string("connection_count"), &getCounters().connectionCount));
 
69
  counters.push_back(new drizzled::plugin::ListenCounter(new std::string("connected"), &getCounters().connected));
 
70
  counters.push_back(new drizzled::plugin::ListenCounter(new std::string("failed_connections"), &getCounters().failedConnections));
75
71
}
76
72
 
77
73
const std::string ListenMySQLProtocol::getHost(void) const
86
82
 
87
83
plugin::Client *ListenMySQLProtocol::getClient(int fd)
88
84
{
89
 
  int new_fd;
90
 
  new_fd= acceptTcp(fd);
91
 
  if (new_fd == -1)
92
 
    return NULL;
93
 
 
94
 
  return new ClientMySQLProtocol(new_fd, _using_mysql41_protocol, getCounters());
 
85
  int new_fd= acceptTcp(fd);
 
86
  return new_fd == -1 ? NULL : new ClientMySQLProtocol(new_fd, _using_mysql41_protocol, getCounters());
95
87
}
96
88
 
97
 
ClientMySQLProtocol::ClientMySQLProtocol(int fd, bool using_mysql41_protocol, ProtocolCounters *set_counters):
 
89
ClientMySQLProtocol::ClientMySQLProtocol(int fd, bool using_mysql41_protocol, ProtocolCounters& set_counters) :
98
90
  _using_mysql41_protocol(using_mysql41_protocol),
99
91
  _is_interactive(false),
100
92
  counters(set_counters)
101
93
{
102
 
  
103
94
  net.vio= 0;
104
95
 
105
96
  if (fd == -1)
106
97
    return;
107
98
 
108
 
  if (drizzleclient_net_init_sock(&net, fd, buffer_length.get()))
109
 
    throw bad_alloc();
110
 
 
 
99
  drizzleclient_net_init_sock(&net, fd, buffer_length.get());
111
100
  drizzleclient_net_set_read_timeout(&net, read_timeout.get());
112
101
  drizzleclient_net_set_write_timeout(&net, write_timeout.get());
113
102
  net.retry_count=retry_count.get();
119
108
    net.vio->close();
120
109
}
121
110
 
122
 
int ClientMySQLProtocol::getFileDescriptor(void)
 
111
int ClientMySQLProtocol::getFileDescriptor()
123
112
{
124
113
  return drizzleclient_net_get_sd(&net);
125
114
}
129
118
  return net.vio != 0;
130
119
}
131
120
 
132
 
bool ClientMySQLProtocol::isReading(void)
 
121
bool ClientMySQLProtocol::isReading()
133
122
{
134
123
  return net.reading_or_writing == 1;
135
124
}
136
125
 
137
 
bool ClientMySQLProtocol::isWriting(void)
 
126
bool ClientMySQLProtocol::isWriting()
138
127
{
139
128
  return net.reading_or_writing == 2;
140
129
}
143
132
{
144
133
  if (net.vio == NULL)
145
134
    return false;
146
 
  bool ret= drizzleclient_net_write(&net, (unsigned char*) packet.ptr(),
147
 
                           packet.length());
 
135
  bool ret= drizzleclient_net_write(&net, (unsigned char*) packet.ptr(), packet.length());
148
136
  packet.length(0);
149
137
  return ret;
150
138
}
151
139
 
152
 
void ClientMySQLProtocol::close(void)
 
140
void ClientMySQLProtocol::close()
153
141
{
154
142
  if (net.vio)
155
143
  { 
156
144
    drizzleclient_net_close(&net);
157
145
    drizzleclient_net_end(&net);
158
 
    counters->connected.decrement();
 
146
    counters.connected.decrement();
159
147
  }
160
148
}
161
149
 
162
150
bool ClientMySQLProtocol::authenticate()
163
151
{
164
 
  bool connection_is_valid;
165
 
  counters->connectionCount.increment();
166
 
  counters->connected.increment();
 
152
  counters.connectionCount.increment();
 
153
  counters.connected.increment();
167
154
 
168
155
  /* Use "connect_timeout" value during connection phase */
169
156
  drizzleclient_net_set_read_timeout(&net, connect_timeout.get());
170
157
  drizzleclient_net_set_write_timeout(&net, connect_timeout.get());
171
158
 
172
 
  connection_is_valid= checkConnection();
173
 
 
174
 
  if (connection_is_valid)
 
159
  if (checkConnection())
175
160
  {
176
 
    if (counters->connected > counters->max_connections)
 
161
    if (counters.connected > counters.max_connections)
177
162
    {
178
163
      std::string errmsg(ER(ER_CON_COUNT_ERROR));
179
164
      sendError(ER_CON_COUNT_ERROR, errmsg.c_str());
180
 
      counters->failedConnections.increment();
 
165
      counters.failedConnections.increment();
181
166
    }
182
167
    else
183
168
    {
187
172
  else
188
173
  {
189
174
    sendError(session->main_da().sql_errno(), session->main_da().message());
190
 
    counters->failedConnections.increment();
 
175
    counters.failedConnections.increment();
191
176
    return false;
192
177
  }
193
178
 
469
454
    1    Error  (Note that in this case the error is not sent to the
470
455
    client)
471
456
*/
472
 
bool ClientMySQLProtocol::sendFields(List<Item> *list)
 
457
void ClientMySQLProtocol::sendFields(List<Item>& list)
473
458
{
474
 
  List<Item>::iterator it(list->begin());
475
 
  Item *item;
 
459
  List<Item>::iterator it(list.begin());
476
460
  unsigned char buff[80];
477
461
  String tmp((char*) buff,sizeof(buff),&my_charset_bin);
478
462
 
479
 
  unsigned char *row_pos= storeLength(buff, list->size());
 
463
  unsigned char *row_pos= storeLength(buff, list.size());
480
464
  (void) drizzleclient_net_write(&net, buff, (size_t) (row_pos-buff));
481
465
 
482
 
  while ((item=it++))
 
466
  while (Item* item=it++)
483
467
  {
484
 
    char *pos;
485
468
    SendField field;
486
469
    item->make_field(&field);
487
470
 
496
479
    packet.realloc(packet.length()+12);
497
480
 
498
481
    /* Store fixed length fields */
499
 
    pos= (char*) packet.ptr()+packet.length();
 
482
    char* pos= (char*) packet.ptr()+packet.length();
500
483
    *pos++= 12;                // Length of packed fields
501
484
    /* No conversion */
502
485
    int2store(pos, field.charsetnr);
591
574
    Send no warning information, as it will be sent at statement end.
592
575
  */
593
576
  writeEOFPacket(session->server_status, session->total_warn_count);
594
 
  return 0; // return void
595
577
}
596
578
 
597
579
void ClientMySQLProtocol::store(Field *from)
662
644
  return net.error && net.vio != 0;
663
645
}
664
646
 
665
 
bool ClientMySQLProtocol::haveMoreData()
666
 
{
667
 
  return drizzleclient_net_more_data(&net);
668
 
}
669
 
 
670
647
bool ClientMySQLProtocol::haveError()
671
648
{
672
649
  return net.error || net.vio == 0;
851
828
  user_identifier->setUser(user);
852
829
  session->setUser(user_identifier);
853
830
 
854
 
  return session->checkUser(string(passwd, passwd_len),
855
 
                            string(l_db ? l_db : ""));
 
831
  return session->checkUser(string(passwd, passwd_len), string(l_db ? l_db : ""));
856
832
 
857
833
}
858
834
 
859
 
void ClientMySQLProtocol::netStoreData(const unsigned char *from, size_t length)
 
835
void ClientMySQLProtocol::netStoreData(const void* from, size_t length)
860
836
{
861
837
  size_t packet_length= packet.length();
862
838
  /*
956
932
  }
957
933
}
958
934
 
959
 
static ListenMySQLProtocol *listen_obj= NULL;
960
 
plugin::Create_function<MySQLPassword> *mysql_password= NULL;
 
935
static ListenMySQLProtocol* listen_obj= NULL;
 
936
plugin::Create_function<MySQLPassword>* mysql_password= NULL;
961
937
 
962
938
static int init(drizzled::module::Context &context)
963
939
{  
980
956
  context.registerVariable(new sys_var_constrained_value<uint32_t>("write_timeout", write_timeout));
981
957
  context.registerVariable(new sys_var_constrained_value<uint32_t>("retry_count", retry_count));
982
958
  context.registerVariable(new sys_var_constrained_value<uint32_t>("buffer_length", buffer_length));
983
 
  context.registerVariable(new sys_var_const_string_val("bind_address",
984
 
                                                        vm["bind-address"].as<std::string>()));
985
 
 
986
 
  context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &ListenMySQLProtocol::mysql_counters->max_connections));
 
959
  context.registerVariable(new sys_var_const_string_val("bind_address", vm["bind-address"].as<std::string>()));
 
960
  context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &ListenMySQLProtocol::mysql_counters.max_connections));
987
961
 
988
962
  return 0;
989
963
}
1013
987
          po::value<string>()->default_value("localhost"),
1014
988
          _("Address to bind to."));
1015
989
  context("max-connections",
1016
 
          po::value<uint32_t>(&ListenMySQLProtocol::mysql_counters->max_connections)->default_value(1000),
 
990
          po::value<uint32_t>(&ListenMySQLProtocol::mysql_counters.max_connections)->default_value(1000),
1017
991
          _("Maximum simultaneous connections."));
1018
992
}
1019
993