~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to vio/vio.c

  • Committer: Brian Aker
  • Date: 2008-07-07 14:25:25 UTC
  • mto: (77.1.25 codestyle)
  • mto: This revision was merged to the branch mainline in revision 82.
  • Revision ID: brian@tangent.org-20080707142525-xzy2nl3ie2ebwfln
LL() cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 */
28
28
 
29
29
static void vio_init(Vio* vio, enum enum_vio_type type,
30
 
                     int sd, uint32_t flags)
 
30
                     my_socket sd, HANDLE hPipe, uint flags)
31
31
{
 
32
  DBUG_ENTER("vio_init");
 
33
  DBUG_PRINT("enter", ("type: %d  sd: %d  flags: %d", type, sd, flags));
 
34
 
32
35
#ifndef HAVE_VIO_READ_BUFF
33
36
  flags&= ~VIO_BUFFERED_READ;
34
37
#endif
35
 
  memset(vio, 0, sizeof(*vio));
 
38
  bzero((char*) vio, sizeof(*vio));
36
39
  vio->type     = type;
37
40
  vio->sd       = sd;
 
41
  vio->hPipe    = hPipe;
 
42
  vio->localhost= flags & VIO_LOCALHOST;
38
43
  if ((flags & VIO_BUFFERED_READ) &&
39
44
      !(vio->read_buffer= (char*)my_malloc(VIO_READ_BUFFER_SIZE, MYF(MY_WME))))
40
45
    flags&= ~VIO_BUFFERED_READ;
 
46
#ifdef __WIN__ 
 
47
  if (type == VIO_TYPE_NAMEDPIPE)
 
48
  {
 
49
    vio->viodelete      =vio_delete;
 
50
    vio->vioerrno       =vio_errno;
 
51
    vio->read           =vio_read_pipe;
 
52
    vio->write          =vio_write_pipe;
 
53
    vio->fastsend       =vio_fastsend;
 
54
    vio->viokeepalive   =vio_keepalive;
 
55
    vio->should_retry   =vio_should_retry;
 
56
    vio->was_interrupted=vio_was_interrupted;
 
57
    vio->vioclose       =vio_close_pipe;
 
58
    vio->peer_addr      =vio_peer_addr;
 
59
    vio->vioblocking    =vio_blocking;
 
60
    vio->is_blocking    =vio_is_blocking;
 
61
    vio->timeout        =vio_ignore_timeout;
 
62
  }
 
63
  else                                  /* default is VIO_TYPE_TCPIP */
 
64
#endif
 
65
#ifdef HAVE_SMEM 
 
66
  if (type == VIO_TYPE_SHARED_MEMORY)
 
67
  {
 
68
    vio->viodelete      =vio_delete;
 
69
    vio->vioerrno       =vio_errno;
 
70
    vio->read           =vio_read_shared_memory;
 
71
    vio->write          =vio_write_shared_memory;
 
72
    vio->fastsend       =vio_fastsend;
 
73
    vio->viokeepalive   =vio_keepalive;
 
74
    vio->should_retry   =vio_should_retry;
 
75
    vio->was_interrupted=vio_was_interrupted;
 
76
    vio->vioclose       =vio_close_shared_memory;
 
77
    vio->peer_addr      =vio_peer_addr;
 
78
    vio->vioblocking    =vio_blocking;
 
79
    vio->is_blocking    =vio_is_blocking;
 
80
    vio->timeout        =vio_ignore_timeout;
 
81
  }
 
82
  else
 
83
#endif   
41
84
  {
42
85
    vio->viodelete      =vio_delete;
43
86
    vio->vioerrno       =vio_errno;
53
96
    vio->is_blocking    =vio_is_blocking;
54
97
    vio->timeout        =vio_timeout;
55
98
  }
 
99
  DBUG_VOID_RETURN;
56
100
}
57
101
 
58
102
 
59
103
/* Reset initialized VIO to use with another transport type */
60
104
 
61
105
void vio_reset(Vio* vio, enum enum_vio_type type,
62
 
               int sd, uint32_t flags)
 
106
               my_socket sd, HANDLE hPipe, uint flags)
63
107
{
64
 
  free(vio->read_buffer);
65
 
  vio_init(vio, type, sd, flags);
 
108
  my_free(vio->read_buffer, MYF(MY_ALLOW_ZERO_PTR));
 
109
  vio_init(vio, type, sd, hPipe, flags);
66
110
}
67
111
 
68
112
 
69
113
/* Open the socket or TCP/IP connection and read the fnctl() status */
70
114
 
71
 
Vio *vio_new(int sd, enum enum_vio_type type, uint32_t flags)
 
115
Vio *vio_new(my_socket sd, enum enum_vio_type type, uint flags)
72
116
{
73
117
  Vio *vio;
74
 
 
 
118
  DBUG_ENTER("vio_new");
 
119
  DBUG_PRINT("enter", ("sd: %d", sd));
75
120
  if ((vio = (Vio*) my_malloc(sizeof(*vio),MYF(MY_WME))))
76
121
  {
77
 
    vio_init(vio, type, sd, flags);
78
 
    sprintf(vio->desc, "TCP/IP (%d)", vio->sd);
 
122
    vio_init(vio, type, sd, 0, flags);
 
123
    sprintf(vio->desc,
 
124
            (vio->type == VIO_TYPE_SOCKET ? "socket (%d)" : "TCP/IP (%d)"),
 
125
            vio->sd);
 
126
#if !defined(__WIN__)
 
127
#if !defined(NO_FCNTL_NONBLOCK)
79
128
    /*
80
129
      We call fcntl() to set the flags and then immediately read them back
81
130
      to make sure that we and the system are in agreement on the state of
88
137
    */
89
138
    fcntl(sd, F_SETFL, 0);
90
139
    vio->fcntl_mode= fcntl(sd, F_GETFL);
91
 
  }
92
 
  return vio;
93
 
}
 
140
#elif defined(HAVE_SYS_IOCTL_H)                 /* hpux */
 
141
    /* Non blocking sockets doesn't work good on HPUX 11.0 */
 
142
    (void) ioctl(sd,FIOSNBIO,0);
 
143
    vio->fcntl_mode &= ~O_NONBLOCK;
 
144
#endif
 
145
#else /* !defined(__WIN__) */
 
146
    {
 
147
      /* set to blocking mode by default */
 
148
      ulong arg=0, r;
 
149
      r = ioctlsocket(sd,FIONBIO,(void*) &arg);
 
150
      vio->fcntl_mode &= ~O_NONBLOCK;
 
151
    }
 
152
#endif
 
153
  }
 
154
  DBUG_RETURN(vio);
 
155
}
 
156
 
 
157
 
 
158
#ifdef __WIN__
 
159
 
 
160
Vio *vio_new_win32pipe(HANDLE hPipe)
 
161
{
 
162
  Vio *vio;
 
163
  DBUG_ENTER("vio_new_handle");
 
164
  if ((vio = (Vio*) my_malloc(sizeof(Vio),MYF(MY_WME))))
 
165
  {
 
166
    vio_init(vio, VIO_TYPE_NAMEDPIPE, 0, hPipe, VIO_LOCALHOST);
 
167
    strmov(vio->desc, "named pipe");
 
168
  }
 
169
  DBUG_RETURN(vio);
 
170
}
 
171
 
 
172
#ifdef HAVE_SMEM
 
173
Vio *vio_new_win32shared_memory(NET *net,HANDLE handle_file_map, HANDLE handle_map,
 
174
                                HANDLE event_server_wrote, HANDLE event_server_read,
 
175
                                HANDLE event_client_wrote, HANDLE event_client_read,
 
176
                                HANDLE event_conn_closed)
 
177
{
 
178
  Vio *vio;
 
179
  DBUG_ENTER("vio_new_win32shared_memory");
 
180
  if ((vio = (Vio*) my_malloc(sizeof(Vio),MYF(MY_WME))))
 
181
  {
 
182
    vio_init(vio, VIO_TYPE_SHARED_MEMORY, 0, 0, VIO_LOCALHOST);
 
183
    vio->handle_file_map= handle_file_map;
 
184
    vio->handle_map= handle_map;
 
185
    vio->event_server_wrote= event_server_wrote;
 
186
    vio->event_server_read= event_server_read;
 
187
    vio->event_client_wrote= event_client_wrote;
 
188
    vio->event_client_read= event_client_read;
 
189
    vio->event_conn_closed= event_conn_closed;
 
190
    vio->shared_memory_remain= 0;
 
191
    vio->shared_memory_pos= handle_map;
 
192
    vio->net= net;
 
193
    strmov(vio->desc, "shared memory");
 
194
  }
 
195
  DBUG_RETURN(vio);
 
196
}
 
197
#endif
 
198
#endif
94
199
 
95
200
 
96
201
void vio_delete(Vio* vio)
100
205
 
101
206
  if (vio->type != VIO_CLOSED)
102
207
    vio->vioclose(vio);
103
 
  free((unsigned char*) vio->read_buffer);
104
 
  free((unsigned char*) vio);
 
208
  my_free((uchar*) vio->read_buffer, MYF(MY_ALLOW_ZERO_PTR));
 
209
  my_free((uchar*) vio,MYF(0));
105
210
}
106
211
 
107
212