GRPC Core  0.10.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
socket_windows.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPC_INTERNAL_CORE_IOMGR_SOCKET_WINDOWS_H
35 #define GRPC_INTERNAL_CORE_IOMGR_SOCKET_WINDOWS_H
36 
38 #include <winsock2.h>
39 
40 #include <grpc/support/sync.h>
41 #include <grpc/support/atm.h>
42 
44 
45 /* This holds the data for an outstanding read or write on a socket.
46  The mutex to protect the concurrent access to that data is the one
47  inside the winsocket wrapper. */
49  /* This is supposed to be a WSAOVERLAPPED, but in order to get that
50  definition, we need to include ws2tcpip.h, which needs to be included
51  from the top, otherwise it'll clash with a previous inclusion of
52  windows.h that in turns includes winsock.h. If anyone knows a way
53  to do it properly, feel free to send a patch. */
54  OVERLAPPED overlapped;
55  /* The callback information for the pending operation. May be empty if the
56  caller hasn't registered a callback yet. */
57  void(*cb)(void *opaque, int success);
58  void *opaque;
59  /* A boolean to describe if the IO Completion Port got a notification for
60  that operation. This will happen if the operation completed before the
61  called had time to register a callback. We could avoid that behavior
62  altogether by forcing the caller to always register its callback before
63  proceeding queue an operation, but it is frequent for an IO Completion
64  Port to trigger quickly. This way we avoid a context switch for calling
65  the callback. We also simplify the read / write operations to avoid having
66  to hold a mutex for a long amount of time. */
68  /* The results of the overlapped operation. */
70  int wsa_error;
71  /* A boolean indicating that we started an operation. */
74 
75 /* This is a wrapper to a Windows socket. A socket can have one outstanding
76  read, and one outstanding write. Doing an asynchronous accept means waiting
77  for a read operation. Doing an asynchronous connect means waiting for a
78  write operation. These are completely arbitrary ties between the operation
79  and the kind of event, because we can have one overlapped per pending
80  operation, whichever its nature is. So we could have more dedicated pending
81  operation callbacks for connect and listen. But given the scope of listen
82  and accept, we don't need to go to that extent and waste memory. Also, this
83  is closer to what happens in posix world. */
84 typedef struct grpc_winsocket {
85  SOCKET socket;
86 
89 
91 
92  /* You can't add the same socket twice to the same IO Completion Port.
93  This prevents that. */
95  /* A boolean to indicate that the caller has abandoned that socket, but
96  there is a pending operation that the IO Completion Port will have to
97  wait for. The socket will be collected at that time. */
98  int orphan;
99 
101 
102  /* A label for iomgr to track outstanding objects */
105 
106 /* Create a wrapped windows handle. This takes ownership of it, meaning that
107  it will be responsible for closing it. */
108 grpc_winsocket *grpc_winsocket_create(SOCKET socket, const char *name);
109 
110 /* Initiate an asynchronous shutdown of the socket. Will call off any pending
111  operation to cancel them. Returns the number of callbacks that got setup. */
113 
114 /* Abandon a socket. */
116 
117 /* Destroy a socket. Should only be called by the IO Completion Port thread,
118  or by grpc_winsocket_orphan if there's no pending operation. */
120 
121 #endif /* GRPC_INTERNAL_CORE_IOMGR_SOCKET_WINDOWS_H */
grpc_winsocket * grpc_winsocket_create(SOCKET socket, const char *name)
int added_to_iocp
Definition: socket_windows.h:94
int orphan
Definition: socket_windows.h:98
SOCKET socket
Definition: socket_windows.h:85
DWORD bytes_transfered
Definition: socket_windows.h:69
gpr_mu state_mu
Definition: socket_windows.h:90
int outstanding
Definition: socket_windows.h:72
int wsa_error
Definition: socket_windows.h:70
grpc_winsocket_callback_info read_info
Definition: socket_windows.h:88
struct grpc_winsocket_callback_info grpc_winsocket_callback_info
Definition: iomgr_internal.h:40
Definition: socket_windows.h:48
grpc_iomgr_object iomgr_object
Definition: socket_windows.h:103
Definition: sync_win32.h:39
A closure over a grpc_iomgr_cb_func.
Definition: iomgr.h:45
Definition: socket_windows.h:84
void * opaque
Definition: socket_windows.h:58
grpc_winsocket_callback_info write_info
Definition: socket_windows.h:87
int grpc_winsocket_shutdown(grpc_winsocket *socket)
void grpc_winsocket_orphan(grpc_winsocket *socket)
void(* cb)(void *opaque, int success)
Definition: socket_windows.h:57
struct grpc_winsocket grpc_winsocket
grpc_iomgr_closure shutdown_closure
Definition: socket_windows.h:100
void grpc_winsocket_destroy(grpc_winsocket *socket)
int has_pending_iocp
Definition: socket_windows.h:67
OVERLAPPED overlapped
Definition: socket_windows.h:54