gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
socket_windows.h
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 
37 #include <windows.h>
38 
39 #include <grpc/support/sync.h>
40 #include <grpc/support/atm.h>
41 
42 /* This holds the data for an outstanding read or write on a socket.
43  The mutex to protect the concurrent access to that data is the one
44  inside the winsocket wrapper. */
46  /* This is supposed to be a WSAOVERLAPPED, but in order to get that
47  definition, we need to include ws2tcpip.h, which needs to be included
48  from the top, otherwise it'll clash with a previous inclusion of
49  windows.h that in turns includes winsock.h. If anyone knows a way
50  to do it properly, feel free to send a patch. */
51  OVERLAPPED overlapped;
52  /* The callback information for the pending operation. May be empty if the
53  caller hasn't registered a callback yet. */
54  void(*cb)(void *opaque, int success);
55  void *opaque;
56  /* A boolean to describe if the IO Completion Port got a notification for
57  that operation. This will happen if the operation completed before the
58  called had time to register a callback. We could avoid that behavior
59  altogether by forcing the caller to always register its callback before
60  proceeding queue an operation, but it is frequent for an IO Completion
61  Port to trigger quickly. This way we avoid a context switch for calling
62  the callback. We also simplify the read / write operations to avoid having
63  to hold a mutex for a long amount of time. */
64  int has_pending_iocp;
65  /* The results of the overlapped operation. */
66  DWORD bytes_transfered;
67  int wsa_error;
68  /* A boolean indicating that we started an operation. */
69  int outstanding;
71 
72 /* This is a wrapper to a Windows socket. A socket can have one outstanding
73  read, and one outstanding write. Doing an asynchronous accept means waiting
74  for a read operation. Doing an asynchronous connect means waiting for a
75  write operation. These are completely arbitrary ties between the operation
76  and the kind of event, because we can have one overlapped per pending
77  operation, whichever its nature is. So we could have more dedicated pending
78  operation callbacks for connect and listen. But given the scope of listen
79  and accept, we don't need to go to that extent and waste memory. Also, this
80  is closer to what happens in posix world. */
81 typedef struct grpc_winsocket {
82  SOCKET socket;
83 
86 
87  gpr_mu state_mu;
88 
89  /* You can't add the same socket twice to the same IO Completion Port.
90  This prevents that. */
91  int added_to_iocp;
92  /* A boolean to indicate that the caller has abandoned that socket, but
93  there is a pending operation that the IO Completion Port will have to
94  wait for. The socket will be collected at that time. */
95  int orphan;
97 
98 /* Create a wrapped windows handle. This takes ownership of it, meaning that
99  it will be responsible for closing it. */
100 grpc_winsocket *grpc_winsocket_create(SOCKET socket);
101 
102 /* Initiate an asynchronous shutdown of the socket. Will call off any pending
103  operation to cancel them. Returns the number of callbacks that got setup. */
104 int grpc_winsocket_shutdown(grpc_winsocket *socket);
105 
106 /* Abandon a socket. */
107 void grpc_winsocket_orphan(grpc_winsocket *socket);
108 
109 /* Destroy a socket. Should only be called by the IO Completion Port thread,
110  or by grpc_winsocket_orphan if there's no pending operation. */
111 void grpc_winsocket_destroy(grpc_winsocket *socket);
112 
113 #endif /* GRPC_INTERNAL_CORE_IOMGR_SOCKET_WINDOWS_H */
Definition: socket_windows.h:45
Definition: sync_win32.h:41
Definition: socket_windows.h:81