~mdcallag/+junk/5.1-map

« back to all changes in this revision

Viewing changes to libmysqld/lib_vio.c

  • Committer: sasha at sashanet
  • Date: 2001-04-12 01:09:00 UTC
  • mfrom: (669.1.1)
  • Revision ID: sp1r-sasha@mysql.sashanet.com-20010412010900-14282
Ugly merge of 3.23 changes into 4.0 - fix up needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
 
2
   
 
3
   This library is free software; you can redistribute it and/or
 
4
   modify it under the terms of the GNU Library General Public
 
5
   License as published by the Free Software Foundation; either
 
6
   version 2 of the License, or (at your option) any later version.
 
7
   
 
8
   This library is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
   Library General Public License for more details.
 
12
   
 
13
   You should have received a copy of the GNU Library General Public
 
14
   License along with this library; if not, write to the Free
 
15
   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
16
   MA 02111-1307, USA */
 
17
 
 
18
/*
 
19
  Note that we can't have assertion on file descriptors;  The reason for
 
20
  this is that during mysql shutdown, another thread can close a file
 
21
  we are working on.  In this case we should just return read errors from
 
22
  the file descriptior.
 
23
*/
 
24
 
 
25
#include <global.h>
 
26
 
 
27
#ifndef HAVE_VIO                        /* is Vio suppored by the Vio lib ? */
 
28
 
 
29
#include <errno.h>
 
30
#include <my_sys.h>
 
31
#include "mysql.h"
 
32
#include <violite.h>
 
33
#include <my_sys.h>
 
34
#include <my_net.h>
 
35
#include <m_string.h>
 
36
#include <dbug.h>
 
37
#include <assert.h>
 
38
 
 
39
#if defined(__EMX__)
 
40
#include <sys/ioctl.h>
 
41
#define ioctlsocket(A,B,C) ioctl((A),(B),(void *)(C),sizeof(*(C)))
 
42
#undef HAVE_FCNTL
 
43
#endif                          /* defined(__EMX__) */
 
44
 
 
45
#if defined(MSDOS) || defined(__WIN__)
 
46
#ifdef __WIN__
 
47
#undef errno
 
48
#undef EINTR
 
49
#undef EAGAIN
 
50
#define errno WSAGetLastError()
 
51
#define EINTR  WSAEINTR
 
52
#define EAGAIN WSAEINPROGRESS
 
53
#endif /* __WIN__ */
 
54
#define O_NONBLOCK 1    /* For emulation of fcntl() */
 
55
#endif
 
56
#ifndef EWOULDBLOCK
 
57
#define EWOULDBLOCK EAGAIN
 
58
#endif
 
59
 
 
60
#ifndef __WIN__
 
61
#define HANDLE void *
 
62
#endif
 
63
 
 
64
struct st_vio
 
65
{
 
66
  my_socket             sd;             /* my_socket - real or imaginary */
 
67
  HANDLE hPipe;
 
68
  my_bool               localhost;      /* Are we from localhost? */
 
69
  int                   fcntl_mode;     /* Buffered fcntl(sd,F_GETFL) */
 
70
  struct sockaddr_in    local;          /* Local internet address */
 
71
  struct sockaddr_in    remote;         /* Remote internet address */
 
72
  enum enum_vio_type    type;           /* Type of connection */
 
73
  char                  desc[30];       /* String description */
 
74
  /* #ifdef EMBEDDED_LIBRARY */
 
75
  /* void *dest_net; */
 
76
  void *dest_thd;
 
77
  char *packets, **last_packet;
 
78
  char *where_in_packet, *end_of_packet;
 
79
  my_bool reading;
 
80
  MEM_ROOT root;
 
81
  /* #endif */
 
82
};
 
83
 
 
84
/* Initialize the communication buffer */
 
85
 
 
86
Vio *vio_new(my_socket sd, enum enum_vio_type type, my_bool localhost)
 
87
{
 
88
  Vio * vio = NULL;
 
89
  vio = (Vio *) my_malloc (sizeof(*vio),MYF(MY_WME|MY_ZEROFILL));
 
90
  if (vio)
 
91
  {
 
92
    init_alloc_root(&vio->root, 8192, 1024);
 
93
    vio->root.min_malloc = sizeof(char *) + 4;
 
94
    vio->last_packet = &vio->packets;
 
95
  }
 
96
  return (vio);
 
97
}
 
98
 
 
99
 
 
100
#ifdef __WIN__
 
101
 
 
102
Vio *vio_new_win32pipe(HANDLE hPipe)
 
103
{
 
104
  return (NULL);
 
105
}
 
106
 
 
107
#endif
 
108
 
 
109
void vio_delete(Vio * vio)
 
110
{
 
111
  if (vio)
 
112
  {
 
113
    if (vio->type != VIO_CLOSED) vio_close(vio);
 
114
    free_root(&vio->root, MYF(0));
 
115
    my_free((gptr)vio, MYF(0));
 
116
  }
 
117
}
 
118
 
 
119
void vio_reset(Vio *vio)
 
120
{
 
121
  free_root(&vio->root, MYF(MY_KEEP_PREALLOC));
 
122
  vio->packets = vio->where_in_packet = vio->end_of_packet = 0;
 
123
  vio->last_packet = &vio->packets;
 
124
}
 
125
 
 
126
int vio_errno(Vio *vio __attribute__((unused)))
 
127
{
 
128
  return errno;                 /* On Win32 this mapped to WSAGetLastError() */
 
129
}
 
130
 
 
131
int vio_read(Vio * vio, gptr buf, int size)
 
132
{
 
133
  vio->reading = 1;
 
134
  if (vio->where_in_packet >= vio->end_of_packet)
 
135
  {
 
136
    dbug_assert(vio->packets);
 
137
    vio->where_in_packet = vio->packets + sizeof(char *) + 4;
 
138
    vio->end_of_packet = vio->where_in_packet +
 
139
                         uint4korr(vio->packets + sizeof(char *));
 
140
    vio->packets = *(char **)vio->packets;
 
141
  }
 
142
  memcpy(buf, vio->where_in_packet, size);
 
143
  vio->where_in_packet += size;
 
144
  return (size);
 
145
}
 
146
 
 
147
int vio_write(Vio * vio, const gptr buf, int size)
 
148
{
 
149
  char *packet;
 
150
  if (vio->reading)
 
151
  {
 
152
    vio->reading = 0;
 
153
    vio_reset(vio);
 
154
  }
 
155
  if ((packet = alloc_root(&vio->root, sizeof(char*) + 4 + size)))
 
156
  {
 
157
    *vio->last_packet = packet;
 
158
    vio->last_packet = (char **)packet;
 
159
    *((char **)packet) = 0;     /* safety */
 
160
    packet += sizeof(char *);
 
161
    int4store(packet, size);
 
162
    memcpy(packet + 4, buf, size);
 
163
  }
 
164
  else
 
165
    size=0;
 
166
  return (size);
 
167
}
 
168
 
 
169
int vio_blocking(Vio * vio, my_bool set_blocking_mode)
 
170
{
 
171
  int r=0;
 
172
  return (r);
 
173
}
 
174
 
 
175
my_bool
 
176
vio_is_blocking(Vio * vio)
 
177
{
 
178
  my_bool r=0;
 
179
  return(r);
 
180
}
 
181
 
 
182
int vio_fastsend(Vio * vio)
 
183
{
 
184
  int r=0;
 
185
  return(r);
 
186
}
 
187
 
 
188
int vio_keepalive(Vio* vio, my_bool set_keep_alive)
 
189
{
 
190
  int r=0;
 
191
  return (r);
 
192
}
 
193
 
 
194
 
 
195
my_bool
 
196
vio_should_retry(Vio * vio __attribute__((unused)))
 
197
{
 
198
  int en = errno;
 
199
  return en == EAGAIN || en == EINTR || en == EWOULDBLOCK;
 
200
}
 
201
 
 
202
 
 
203
int vio_close(Vio * vio)
 
204
{
 
205
  int r=0;
 
206
  return(r);
 
207
}
 
208
 
 
209
 
 
210
const char *vio_description(Vio * vio)
 
211
{
 
212
  return "embedded vio";
 
213
}
 
214
 
 
215
enum enum_vio_type vio_type(Vio* vio)
 
216
{
 
217
  return VIO_CLOSED;
 
218
}
 
219
 
 
220
my_socket vio_fd(Vio* vio)
 
221
{
 
222
  return 0;
 
223
}
 
224
 
 
225
 
 
226
my_bool vio_peer_addr(Vio * vio, char *buf)
 
227
{
 
228
  return(0);
 
229
}
 
230
 
 
231
 
 
232
void vio_in_addr(Vio *vio, struct in_addr *in)
 
233
{
 
234
}
 
235
 
 
236
#endif /* HAVE_VIO */