1 #ifndef ERIS_POLL_WINSOCK_IMPL_H
2 #define ERIS_POLL_WINSOCK_IMPL_H
3 
4 #include "PollWinsock.h"
5 
6 namespace Eris {
7 
PollWinsock()8 PollWinsock::PollWinsock()
9 {
10 }
11 
~PollWinsock()12 PollWinsock::~PollWinsock()
13 {
14 }
15 
addStream(const basic_socket *,Check)16 void PollWinsock::addStream(const basic_socket*, Check)
17 {
18 }
19 
changeStream(const basic_socket *,Check)20 void PollWinsock::changeStream(const basic_socket*, Check)
21 {
22 }
23 
removeStream(const basic_socket *)24 void PollWinsock::removeStream(const basic_socket*)
25 {
26 }
27 
poll(unsigned long timeout)28 void PollWinsock::poll(unsigned long timeout)
29 {
30 }
31 
doPoll(unsigned long timeout)32 void PollWinsock::doPoll(unsigned long timeout)
33 {
34 }
35 
PollDataWinsock(const PollWinsock::MapType &,bool &,unsigned long)36 PollDataWinsock::PollDataWinsock(const PollWinsock::MapType&,
37                                  bool&,
38                                  unsigned long)
39 {
40 }
41 
isReady(const basic_socket *)42 bool PollDataWinsock::isReady(const basic_socket*)
43 {
44 }
45 
46 } // namespace Eris
47 
48 #if 0
49 #ifndef UNICODE
50 #define UNICODE
51 #endif
52 
53 #define WIN32_LEAN_AND_MEAN
54 
55 #include <winsock2.h>
56 #include <Ws2tcpip.h>
57 #include <stdio.h>
58 
59 // Link with ws2_32.lib
60 #pragma comment(lib, "Ws2_32.lib")
61 
62 #define DATA_BUFSIZE 4096
63 
64 int main()
65 {
66     //-----------------------------------------
67     // Declare and initialize variables
68     WSADATA wsaData = { 0 };
69     int iResult = 0;
70     BOOL bResult = TRUE;
71 
72     WSABUF DataBuf;
73     char buffer[DATA_BUFSIZE];
74 
75     DWORD EventTotal = 0;
76     DWORD RecvBytes = 0;
77     DWORD Flags = 0;
78     DWORD BytesTransferred = 0;
79 
80     WSAEVENT EventArray[WSA_MAXIMUM_WAIT_EVENTS];
81     WSAOVERLAPPED AcceptOverlapped;
82     SOCKET ListenSocket = INVALID_SOCKET;
83     SOCKET AcceptSocket = INVALID_SOCKET;
84 
85     DWORD Index;
86 
87     //-----------------------------------------
88     // Initialize Winsock
89     // Initialize Winsock
90     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
91     if (iResult != 0) {
92         wprintf(L"WSAStartup failed: %d\n", iResult);
93         return 1;
94     }
95     //-----------------------------------------
96     // Create a listening socket bound to a local
97     // IP address and the port specified
98     ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
99     if (ListenSocket == INVALID_SOCKET) {
100         wprintf(L"socket failed with error = %d\n", WSAGetLastError());
101         WSACleanup();
102         return 1;
103     }
104 
105     u_short port = 27015;
106     char *ip;
107     sockaddr_in service;
108     service.sin_family = AF_INET;
109     service.sin_port = htons(port);
110     hostent *thisHost;
111 
112     thisHost = gethostbyname("");
113     if (thisHost == NULL) {
114         wprintf(L"gethostbyname failed with error = %d\n", WSAGetLastError());
115         closesocket(ListenSocket);
116         WSACleanup();
117         return 1;
118     }
119 
120     ip = inet_ntoa(*(struct in_addr *) *thisHost->h_addr_list);
121 
122     service.sin_addr.s_addr = inet_addr(ip);
123 
124     //-----------------------------------------
125     // Bind the listening socket to the local IP address
126     // and port number
127     iResult = bind(ListenSocket, (SOCKADDR *) & service, sizeof (SOCKADDR));
128     if (iResult != 0) {
129         wprintf(L"bind failed with error = %d\n", WSAGetLastError());
130         closesocket(ListenSocket);
131         WSACleanup();
132         return 1;
133     }
134     //-----------------------------------------
135     // Set the socket to listen for incoming
136     // connection requests
137     iResult = listen(ListenSocket, 1);
138     if (iResult != 0) {
139         wprintf(L"listen failed with error = %d\n", WSAGetLastError());
140         closesocket(ListenSocket);
141         WSACleanup();
142         return 1;
143     }
144     wprintf(L"Listening...\n");
145 
146     //-----------------------------------------
147     // Accept and incoming connection request
148     AcceptSocket = accept(ListenSocket, NULL, NULL);
149     if (AcceptSocket == INVALID_SOCKET) {
150         wprintf(L"accept failed with error = %d\n", WSAGetLastError());
151         closesocket(ListenSocket);
152         WSACleanup();
153         return 1;
154     }
155     wprintf(L"Client Accepted...\n");
156 
157     //-----------------------------------------
158     // Create an event handle and setup an overlapped structure.
159     EventArray[EventTotal] = WSACreateEvent();
160     if (EventArray[EventTotal] == WSA_INVALID_EVENT) {
161         wprintf(L"WSACreateEvent failed with error = %d\n", WSAGetLastError());
162         closesocket(AcceptSocket);
163         closesocket(ListenSocket);
164         WSACleanup();
165         return 1;
166     }
167 
168     ZeroMemory(&AcceptOverlapped, sizeof (WSAOVERLAPPED));
169     AcceptOverlapped.hEvent = EventArray[EventTotal];
170 
171     DataBuf.len = DATA_BUFSIZE;
172     DataBuf.buf = buffer;
173 
174     EventTotal++;
175 
176     //-----------------------------------------
177     // Call WSARecv to receive data into DataBuf on
178     // the accepted socket in overlapped I/O mode
179     if (WSARecv(AcceptSocket, &DataBuf, 1, &RecvBytes, &Flags, &AcceptOverlapped, NULL) ==
180         SOCKET_ERROR) {
181         iResult = WSAGetLastError();
182         if (iResult != WSA_IO_PENDING)
183             wprintf(L"WSARecv failed with error = %d\n", iResult);
184     }
185     //-----------------------------------------
186     // Process overlapped receives on the socket
187     while (1) {
188 
189         //-----------------------------------------
190         // Wait for the overlapped I/O call to complete
191         Index = WSAWaitForMultipleEvents(EventTotal, EventArray, FALSE, WSA_INFINITE, FALSE);
192 
193         //-----------------------------------------
194         // Reset the signaled event
195         bResult = WSAResetEvent(EventArray[Index - WSA_WAIT_EVENT_0]);
196         if (bResult == FALSE) {
197             wprintf(L"WSAResetEvent failed with error = %d\n", WSAGetLastError());
198         }
199         //-----------------------------------------
200         // Determine the status of the overlapped event
201         bResult =
202             WSAGetOverlappedResult(AcceptSocket, &AcceptOverlapped, &BytesTransferred, FALSE,
203                                    &Flags);
204         if (bResult == FALSE) {
205             wprintf(L"WSAGetOverlappedResult failed with error = %d\n", WSAGetLastError());
206         }
207         //-----------------------------------------
208         // If the connection has been closed, close the accepted socket
209         if (BytesTransferred == 0) {
210             wprintf(L"Closing accept Socket %d\n", AcceptSocket);
211             closesocket(ListenSocket);
212             closesocket(AcceptSocket);
213             WSACloseEvent(EventArray[Index - WSA_WAIT_EVENT_0]);
214             WSACleanup();
215             return 1;
216         }
217         //-----------------------------------------
218         // If data has been received, echo the received data
219         // from DataBuf back to the client
220         iResult =
221             WSASend(AcceptSocket, &DataBuf, 1, &RecvBytes, Flags, &AcceptOverlapped, NULL);
222         if (iResult != 0) {
223             wprintf(L"WSASend failed with error = %d\n", WSAGetLastError());
224         }
225         //-----------------------------------------
226         // Reset the changed flags and overlapped structure
227         Flags = 0;
228         ZeroMemory(&AcceptOverlapped, sizeof (WSAOVERLAPPED));
229 
230         AcceptOverlapped.hEvent = EventArray[Index - WSA_WAIT_EVENT_0];
231 
232         //-----------------------------------------
233         // Reset the data buffer
234         DataBuf.len = DATA_BUFSIZE;
235         DataBuf.buf = buffer;
236     }
237 
238     closesocket(ListenSocket);
239     closesocket(AcceptSocket);
240     WSACleanup();
241 
242     return 0;
243 }
244 
245 #endif
246 
247 #if 0
248    WSAEventSelect(m_listeningSocket, m_acceptEvent.GetEvent(), FD_ACCEPT);
249 
250    do
251    {
252       for (size_t i = 0; i < numAcceptsToPost; ++i)
253       {
254          Accept();
255       }
256 
257       m_postMoreAcceptsEvent.Reset();
258       m_acceptEvent.Reset();
259 
260       HANDLE handlesToWaitFor[2];
261 
262       handlesToWaitFor[0] = m_postMoreAcceptsEvent.GetEvent();
263       handlesToWaitFor[1] = m_acceptEvent.GetEvent();
264 
265       waitResult = ::WaitForMultipleObjects(2, handlesToWaitFor, false, INFINITE);
266 
267       if (waitResult != WAIT_OBJECT_0 &&
268           waitResult != WAIT_OBJECT_0 + 1)
269       {
270          OnError(_T("CSocketServerEx::Run() - WaitForMultipleObjects: ")
271                  + GetLastErrorMessage(::GetLastError()));
272       }
273 
274       if (waitResult == WAIT_OBJECT_0 + 1)
275       {
276          Output(_T("Accept..."));
277       }
278    }
279    while (waitResult == WAIT_OBJECT_0 || waitResult == WAIT_OBJECT_0 + 1);
280 #endif
281 
282 #endif // ERIS_POLL_WINSOCK_IMPL_H
283