~drizzle-trunk/drizzle/development

390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
20
/* Various helper functions not intended to be part of a public API */
21
612.2.4 by Monty Taylor
Moved some defines to config.h. Stopped including config.h directly anywhere.
22
#include <drizzled/global.h>
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
23
#include "libdrizzle_priv.h"
139.1.13 by Trond Norbye
Replaced inclusion of sys/poll and sys/fcntl with poll.h and fcntl.h
24
#include <poll.h>
25
#include <fcntl.h>
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
26
27
const char  *unknown_sqlstate= "HY000";
28
const char  *not_error_sqlstate= "00000";
29
const char  *cant_connect_sqlstate= "08001";
30
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
31
const char * drizzleclient_sqlstate_get_unknown(void)
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
32
{
33
  return unknown_sqlstate;
34
}
35
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
36
const char * drizzleclient_sqlstate_get_not_error(void)
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
37
{
38
  return not_error_sqlstate;
39
}
40
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
41
const char * drizzleclient_sqlstate_get_cant_connect(void)
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
42
{
43
  return cant_connect_sqlstate;
44
}
45
840.1.18 by Monty Taylor
More drizzleclient lib renaming.
46
/*
47
  Wait up to timeout seconds for a connection to be established.
48
49
  We prefer to do this with poll() as there is no limitations with this.
50
  If not, we will use select()
51
*/
52
53
static int wait_for_data(int fd, int32_t timeout)
54
{
55
  struct pollfd ufds;
56
  int res;
57
58
  ufds.fd= fd;
59
  ufds.events= POLLIN | POLLPRI;
60
  if (!(res= poll(&ufds, 1, (int) timeout*1000)))
61
  {
62
    errno= EINTR;
63
    return -1;
64
  }
65
  if (res < 0 || !(ufds.revents & (POLLIN | POLLPRI)) || (ufds.revents & POLLHUP))
66
    return -1;
67
  return 0;
68
}
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
69
/****************************************************************************
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
70
  A modified version of connect().  drizzleclient_connect_with_timeout() allows you to specify
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
71
  a timeout value, in seconds, that we should wait until we
72
  derermine we can't connect to a particular host.  If timeout is 0,
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
73
  drizzleclient_connect_with_timeout() will behave exactly like connect().
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
74
75
  Base version coded by Steve Bernacki, Jr. <steve@navinet.net>
76
*****************************************************************************/
77
840.1.20 by Monty Taylor
Renamed non-prefixed things from libdrizzleclient to drizzleclient.
78
int drizzleclient_connect_with_timeout(int fd, const struct sockaddr *name, uint32_t namelen, int32_t timeout)
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
79
{
80
  int flags, res, s_err;
81
82
  /*
83
    If they passed us a timeout of zero, we should behave
84
    exactly like the normal connect() call does.
85
  */
86
87
  if (timeout == 0)
88
    return connect(fd, (struct sockaddr*) name, namelen);
89
90
  flags = fcntl(fd, F_GETFL, 0);    /* Set socket to not block */
91
#ifdef O_NONBLOCK
92
  fcntl(fd, F_SETFL, flags | O_NONBLOCK);  /* and save the flags..  */
93
#endif
94
95
  res= connect(fd, (struct sockaddr*) name, namelen);
96
  s_err= errno;      /* Save the error... */
97
  fcntl(fd, F_SETFL, flags);
98
  if ((res != 0) && (s_err != EINPROGRESS))
99
  {
100
    errno= s_err;      /* Restore it */
101
    return(-1);
102
  }
103
  if (res == 0)        /* Connected quickly! */
104
    return(0);
105
106
  return wait_for_data(fd, timeout);
107
}
108