]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengines/socketengine_iocp.cpp
Ugly-ish hack to select SocketEngine on windows until I or someone else finds a bette...
[user/henk/code/inspircd.git] / src / socketengines / socketengine_iocp.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd_config.h"
15 #ifdef CONFIG_USE_IOCP
16
17 #ifndef __SOCKETENGINE_IOCP__
18 #define __SOCKETENGINE_IOCP__
19
20 #include "inspircd_win32wrapper.h"
21 #include "inspircd.h"
22 #include "socketengine.h"
23
24 #define READ_BUFFER_SIZE 600
25 #define USING_IOCP 1
26
27 /** Socket overlapped event types
28  */
29 enum SocketIOEvent
30 {
31         /** Read ready */
32         SOCKET_IO_EVENT_READ_READY                      = 0,
33         /** Write ready */
34         SOCKET_IO_EVENT_WRITE_READY                     = 1,
35         /** Accept ready */
36         SOCKET_IO_EVENT_ACCEPT                          = 2,
37         /** Error occured */
38         SOCKET_IO_EVENT_ERROR                           = 3,
39         /** Number of events */
40         NUM_SOCKET_IO_EVENTS                            = 4,
41 };
42
43 /** Represents a windows overlapped IO event
44  */
45 class Overlapped
46 {
47  public:
48         /** Overlap event */
49         OVERLAPPED m_overlap;
50         /** Type of event */
51         SocketIOEvent m_event;
52 #ifdef WIN64
53         /** Parameters */
54         unsigned __int64 m_params;
55 #else
56         /** Parameters */
57         unsigned long m_params;
58 #endif
59         /** Create an overlapped event
60          */
61         Overlapped(SocketIOEvent ev, int params) : m_event(ev), m_params(params)
62         {
63                 memset(&m_overlap, 0, sizeof(OVERLAPPED));
64         }
65 };
66
67 /** Specific to UDP sockets with overlapped IO
68  */
69 struct udp_overlap
70 {
71         unsigned char udp_buffer[600];
72         unsigned long udp_len;
73         sockaddr udp_sockaddr[2];
74         unsigned long udp_sockaddr_len;
75 };
76
77 /** Specific to accepting sockets with overlapped IO
78  */
79 struct accept_overlap
80 {
81         int socket;
82         char buf[1024];
83 };
84
85 /** Implementation of SocketEngine that implements windows IO Completion Ports
86  */
87 class IOCPEngine : public SocketEngine
88 {
89         /** Creates a "fake" file descriptor for use with an IOCP socket.
90          * This is a little slow, but it isnt called too much. We'll fix it
91          * in a future release.
92          * @return -1 if there are no free slots, and an integer if it finds one.
93          */
94         __inline int GenerateFd(int RealFd)
95         {
96                 int index_hash = RealFd % MAX_DESCRIPTORS;
97                 if(ref[index_hash] == 0)
98                         return index_hash;
99                 else
100                 {
101                         register int i = 0;
102                         for(; i < MAX_DESCRIPTORS; ++i)
103                                 if(ref[i] == 0)
104                                         return i;
105                 }
106                 return -1;
107         }
108
109         /** Global I/O completion port that sockets attach to.
110          */
111         HANDLE m_completionPort;
112
113         /** This is kinda shitty... :/ for getting an address from a real fd.
114          */
115         std::map<int, EventHandler*> m_binding;
116
117         LocalIntExt fdExt;
118         LocalIntExt readExt;
119         LocalIntExt writeExt;
120         LocalIntExt acceptExt;
121
122 public:
123         /** Holds the preallocated buffer passed to WSARecvFrom
124          * function. Yes, I know, it's a dirty hack.
125          */
126         udp_overlap * udp_ov;
127
128         /** Creates an IOCP Socket Engine
129          * @param Instance The creator of this object
130          */
131         IOCPEngine();
132
133         /** Deletes an IOCP socket engine and all the attached sockets
134          */
135         ~IOCPEngine();
136
137         /** Adds an event handler to the completion port, and sets up initial events.
138          * @param eh EventHandler to add
139          * @return True if success, false if no room
140          */
141         bool AddFd(EventHandler* eh, int event_mask);
142
143         /** Gets the maximum number of file descriptors that this engine can handle.
144          * @return The number of file descriptors
145          */
146         __inline int GetMaxFds() { return MAX_DESCRIPTORS; }
147
148         /** Gets the number of free/remaining file descriptors under this engine.
149          * @return Remaining count
150          */
151         __inline int GetRemainingFds()
152         {
153                 register int count = 0;
154                 register int i = 0;
155                 for(; i < MAX_DESCRIPTORS; ++i)
156                         if(ref[i] == 0)
157                                 ++count;
158                 return count;
159         }
160
161         /** Removes a file descriptor from the set, preventing it from receiving any more events
162          * @return True if remove was successful, false otherwise
163          */
164         bool DelFd(EventHandler* eh, bool force = false);
165
166         /** Called every loop to handle input/output events for all sockets under this engine
167          * @return The number of "changed" sockets.
168          */
169         int DispatchEvents();
170
171         /** Gets the name of this socket engine as a string.
172          * @return string of socket engine name
173          */
174         std::string GetName();
175
176         void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
177
178         /** Posts a completion event on the specified socket.
179          * @param eh EventHandler for message
180          * @param type Event Type
181          * @param param Event Parameter
182          * @return True if added, false if not
183          */
184         bool PostCompletionEvent(EventHandler* eh, SocketIOEvent type, int param);
185
186         /** Posts a read event on the specified socket
187          * @param eh EventHandler (socket)
188          */
189         void PostReadEvent(EventHandler* eh);
190
191         /** Posts an accept event on the specified socket
192          * @param eh EventHandler (socket)
193          */
194         void PostAcceptEvent(EventHandler* eh);
195
196         /** Returns the EventHandler attached to a specific fd.
197          * If the fd isnt in the socketengine, returns NULL.
198          * @param fd The event handler to look for
199          * @return A pointer to the event handler, or NULL
200          */
201         EventHandler* GetRef(int fd);
202
203         /** Returns true if a file descriptor exists in
204          * the socket engine's list.
205          * @param fd The event handler to look for
206          * @return True if this fd has an event handler
207          */
208         bool HasFd(int fd);
209
210         /** Returns the EventHandler attached to a specific fd.
211          * If the fd isnt in the socketengine, returns NULL.
212          * @param fd The event handler to look for
213          * @return A pointer to the event handler, or NULL
214          */
215         EventHandler* GetIntRef(int fd);
216
217         bool BoundsCheckFd(EventHandler* eh);
218
219         virtual int Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen);
220
221         virtual int RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
222
223         virtual int Blocking(int fd);
224
225         virtual int NonBlocking(int fd);
226
227         virtual int GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen);
228
229         virtual int Close(int fd);
230
231         virtual int Close(EventHandler* fd);
232 };
233
234 #endif
235
236 #include "exitcodes.h"
237 #include <mswsock.h>
238
239 IOCPEngine::IOCPEngine()
240 : fdExt("internal_fd", NULL),
241   readExt("windows_readevent", NULL),
242   writeExt("windows_writeevent", NULL),
243   acceptExt("windows_acceptevent", NULL)
244 {
245         MAX_DESCRIPTORS = 10240;
246
247         /* Create completion port */
248         m_completionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)0, 0);
249
250         if (!m_completionPort)
251         {
252                 ServerInstance->Logs->Log("SOCKET",DEFAULT, "ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
253                 ServerInstance->Logs->Log("SOCKET",DEFAULT, "ERROR: this is a fatal error, exiting now.");
254                 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.\n");
255                 printf("ERROR: this is a fatal error, exiting now.\n");
256                 ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
257         }
258
259         /* Null variables out. */
260         CurrentSetSize = 0;
261         MAX_DESCRIPTORS = 10240;
262         ref = new EventHandler* [10240];
263         memset(ref, 0, sizeof(EventHandler*) * MAX_DESCRIPTORS);
264 }
265
266 IOCPEngine::~IOCPEngine()
267 {
268         /* Clean up winsock and close completion port */
269         CloseHandle(m_completionPort);
270         WSACleanup();
271         delete[] ref;
272 }
273
274 bool IOCPEngine::AddFd(EventHandler* eh, int event_mask)
275 {
276         /* Does it at least look valid? */
277         if (!eh)
278                 return false;
279
280         int* fake_fd = new int(GenerateFd(eh->GetFd()));
281         int is_accept = 0;
282         int opt_len = sizeof(int);
283
284         /* In range? */
285         if ((*fake_fd < 0) || (*fake_fd > MAX_DESCRIPTORS))
286         {
287                 delete fake_fd;
288                 return false;
289         }
290
291         /* Already an entry here */
292         if (ref[*fake_fd])
293         {
294                 delete fake_fd;
295                 return false;
296         }
297
298         /* are we a listen socket? */
299         getsockopt(eh->GetFd(), SOL_SOCKET, SO_ACCEPTCONN, (char*)&is_accept, &opt_len);
300
301         /* set up the read event so the socket can actually receive data :P */
302         fdExt.set(eh, *fake_fd);
303
304         unsigned long completion_key = (ULONG_PTR)*fake_fd;
305         /* assign the socket to the completion port */
306         if (!CreateIoCompletionPort((HANDLE)eh->GetFd(), m_completionPort, completion_key, 0))
307                 return false;
308
309         /* setup initial events */
310         if(is_accept)
311                 PostAcceptEvent(eh);
312         else
313                 PostReadEvent(eh);
314
315         /* log message */
316         ServerInstance->Logs->Log("SOCKET",DEBUG, "New fake fd: %u, real fd: %u, address 0x%p", *fake_fd, eh->GetFd(), eh);
317
318         /* post a write event if there is data to be written */
319         if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
320                 OnSetEvent(eh, event_mask, event_mask);
321
322         /* we're all good =) */
323         try
324         {
325                 m_binding.insert( std::map<int, EventHandler*>::value_type( eh->GetFd(), eh ) );
326         }
327         catch (...)
328         {
329                 /* Ohshi-, map::insert failed :/ */
330                 return false;
331         }
332
333         ++CurrentSetSize;
334         SocketEngine::SetEventMask(eh, event_mask);
335         ref[*fake_fd] = eh;
336
337         return true;
338 }
339
340 bool IOCPEngine::DelFd(EventHandler* eh, bool force /* = false */)
341 {
342         if (!eh)
343                 return false;
344
345         int* fake_fd = (int*)fdExt.get(eh);
346
347         if (!fake_fd)
348                 return false;
349
350         int fd = eh->GetFd();
351
352         void* m_readEvent = (void*)readExt.get(eh);
353         void* m_writeEvent = (void*)writeExt.get(eh);
354         void* m_acceptEvent = (void*)acceptExt.get(eh);
355
356         ServerInstance->Logs->Log("SOCKET",DEBUG, "Removing fake fd %u, real fd %u, address 0x%p", *fake_fd, eh->GetFd(), eh);
357
358         /* Cancel pending i/o operations. */
359         if (CancelIo((HANDLE)fd) == FALSE)
360                 return false;
361
362         /* Free the buffer, and delete the event. */
363         if (m_readEvent)
364         {
365                 if(((Overlapped*)m_readEvent)->m_params != 0)
366                         delete ((udp_overlap*)((Overlapped*)m_readEvent)->m_params);
367
368                 delete ((Overlapped*)m_readEvent);
369                 readExt.free(eh);
370         }
371
372         if(m_writeEvent)
373         {
374                 delete ((Overlapped*)m_writeEvent);
375                 writeExt.free(eh);
376         }
377
378         if(m_acceptEvent)
379         {
380                 delete ((accept_overlap*)((Overlapped*)m_acceptEvent)->m_params);
381                 delete ((Overlapped*)m_acceptEvent);
382                 acceptExt.free(eh);
383         }
384
385         /* Clear binding */
386         ref[*fake_fd] = 0;
387         m_binding.erase(eh->GetFd());
388
389         delete fake_fd;
390         fdExt.free(eh);
391
392         /* decrement set size */
393         --CurrentSetSize;
394
395         /* success */
396         return true;
397 }
398
399 void IOCPEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
400 {
401         if (!eh)
402                 return;
403
404         void* m_writeEvent = (void*)writeExt.get(eh);
405
406         int* fake_fd = (int*)fdExt.get(eh);
407         if (!fake_fd)
408                 return;
409
410         /* Post event - write begin */
411         if((new_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE)) && !m_writeEvent)
412         {
413                 ULONG_PTR completion_key = (ULONG_PTR)*fake_fd;
414                 Overlapped * ov = new Overlapped(SOCKET_IO_EVENT_WRITE_READY, 0);
415                 writeExt.free(eh);
416                 writeExt.set(eh, (intptr_t)ov);
417                 PostQueuedCompletionStatus(m_completionPort, 0, completion_key, &ov->m_overlap);
418         }
419 }
420
421 bool IOCPEngine::PostCompletionEvent(EventHandler * eh, SocketIOEvent type, int param)
422 {
423         if (!eh)
424                 return false;
425
426         int* fake_fd = (int*)fdExt.get(eh);
427         if (!fake_fd)
428                 return false;
429
430         Overlapped * ov = new Overlapped(type, param);
431         ULONG_PTR completion_key = (ULONG_PTR)*fake_fd;
432         return PostQueuedCompletionStatus(m_completionPort, 0, completion_key, &ov->m_overlap);
433 }
434
435 void IOCPEngine::PostReadEvent(EventHandler * eh)
436 {
437         if (!eh)
438                 return;
439
440         Overlapped * ov = new Overlapped(SOCKET_IO_EVENT_READ_READY, 0);
441         DWORD flags = 0;
442         DWORD r_length = 0;
443         WSABUF buf;
444
445         /* by passing a null buffer pointer, we can have this working in the same way as epoll..
446          * its slower, but it saves modifying all network code.
447          */
448         buf.buf = 0;
449         buf.len = 0;
450
451         /* determine socket type. */
452         DWORD sock_type;
453         int sock_len = sizeof(DWORD);
454         if(getsockopt(eh->GetFd(), SOL_SOCKET, SO_TYPE, (char*)&sock_type, &sock_len) == -1)
455         {
456                 /* wtfhax? */
457                 PostCompletionEvent(eh, SOCKET_IO_EVENT_ERROR, 0);
458                 delete ov;
459                 return;
460         }
461         switch(sock_type)
462         {
463                 case SOCK_DGRAM:                        /* UDP Socket */
464                 {
465                         udp_overlap * uv = new udp_overlap;
466                         uv->udp_sockaddr_len = sizeof(sockaddr);
467                         buf.buf = (char*)uv->udp_buffer;
468                         buf.len = sizeof(uv->udp_buffer);
469                         ov->m_params = (unsigned long)uv;
470                         if(WSARecvFrom(eh->GetFd(), &buf, 1, &uv->udp_len, &flags, uv->udp_sockaddr, (LPINT)&uv->udp_sockaddr_len, &ov->m_overlap, 0))
471                         {
472                                 int err = WSAGetLastError();
473                                 if(err != WSA_IO_PENDING)
474                                 {
475                                         delete ov;
476                                         PostCompletionEvent(eh, SOCKET_IO_EVENT_ERROR, 0);
477                                         return;
478                                 }
479                         }
480                 }
481                 break;
482
483                 case SOCK_STREAM:                       /* TCP Socket */
484                 {
485                         if(WSARecv(eh->GetFd(), &buf, 1, &r_length, &flags, &ov->m_overlap, 0) == SOCKET_ERROR)
486                         {
487                                 if(WSAGetLastError() != WSA_IO_PENDING)
488                                 {
489                                         delete ov;
490                                         PostCompletionEvent(eh, SOCKET_IO_EVENT_ERROR, 0);
491                                         return;
492                                 }
493                         }
494                 }
495                 break;
496
497                 default:
498                 {
499                         printf("unknwon socket type: %u\n", sock_type);
500                         return;
501                 }
502                 break;
503         }
504         readExt.set(eh, (intptr_t)ov);
505 }
506
507 int IOCPEngine::DispatchEvents()
508 {
509         DWORD len;
510         LPOVERLAPPED overlap;
511         Overlapped * ov;
512         EventHandler * eh;
513         ULONG_PTR intfd;
514         int ret;
515         unsigned long bytes_recv;
516
517         while (GetQueuedCompletionStatus(m_completionPort, &len, &intfd, &overlap, 1000))
518         {
519                 if (intfd > (unsigned long)MAX_DESCRIPTORS)
520                         continue;
521
522                 // woot, we got an event on a socket :P
523                 eh = ref[intfd];
524                 ov = CONTAINING_RECORD(overlap, Overlapped, m_overlap);
525
526                 if (eh == 0)
527                         continue;
528
529                 TotalEvents++;
530
531                 switch(ov->m_event)
532                 {
533                         case SOCKET_IO_EVENT_WRITE_READY:
534                         {
535                                 WriteEvents++;
536                                 writeExt.free(eh);
537                                 SetEventMask(eh, eh->GetEventMask() & ~FD_WRITE_WILL_BLOCK);
538                                 eh->HandleEvent(EVENT_WRITE, 0);
539                         }
540                         break;
541
542                         case SOCKET_IO_EVENT_READ_READY:
543                         {
544                                 ReadEvents++;
545                                 SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
546                                 if(ov->m_params)
547                                 {
548                                         // if we had params, it means we are a udp socket with a udp_overlap pointer in this long.
549                                         udp_overlap * uv = (udp_overlap*)ov->m_params;
550                                         uv->udp_len = len;
551                                         this->udp_ov = uv;
552                                         readExt.free(eh);
553                                         eh->HandleEvent(EVENT_READ, 0);
554                                         this->udp_ov = 0;
555                                         delete uv;
556                                         PostReadEvent(eh);
557                                 }
558                                 else
559                                 {
560                                         ret = ioctlsocket(eh->GetFd(), FIONREAD, &bytes_recv);
561                                         readExt.free(eh);
562                                         if(ret != 0 || bytes_recv == 0)
563                                         {
564                                                 /* end of file */
565                                                 PostCompletionEvent(eh, SOCKET_IO_EVENT_ERROR, EIO); /* Old macdonald had an error, EIEIO. */
566                                         }
567                                         else
568                                         {
569                                                 eh->HandleEvent(EVENT_READ, 0);
570                                                 PostReadEvent(eh);
571                                         }
572                                 }
573                         }
574                         break;
575
576                         case SOCKET_IO_EVENT_ACCEPT:
577                         {
578                                 /* this is kinda messy.. :/ */
579                                 ReadEvents++;
580                                 eh->HandleEvent(EVENT_READ, ov->m_params);
581                                 delete ((accept_overlap*)ov->m_params);
582                                 acceptExt.free(eh);
583                                 PostAcceptEvent(eh);
584                         }
585                         break;
586
587                         case SOCKET_IO_EVENT_ERROR:
588                         {
589                                 ErrorEvents++;
590                                 eh->HandleEvent(EVENT_ERROR, ov->m_params);
591                         }
592                         break;
593                 }
594
595                 delete ov;
596         }
597
598         return 0;
599 }
600
601 void IOCPEngine::PostAcceptEvent(EventHandler * eh)
602 {
603         if (!eh)
604                 return;
605
606         int on = 1;
607         u_long arg = 1;
608         struct linger linger = { 0 };
609
610         int fd = WSASocket(AF_INET, SOCK_STREAM, 0, 0, 0, WSA_FLAG_OVERLAPPED);
611
612         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
613         /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
614         linger.l_onoff = 1;
615         linger.l_linger = 1;
616         setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&linger,sizeof(linger));
617         ioctlsocket(fd, FIONBIO, &arg);
618
619         int len = sizeof(sockaddr_in) + 16;
620         DWORD dwBytes;
621         accept_overlap* ao = new accept_overlap;
622         memset(ao->buf, 0, 1024);
623         ao->socket = fd;
624
625         Overlapped* ov = new Overlapped(SOCKET_IO_EVENT_ACCEPT, (int)ao);
626         acceptExt.set(eh, (intptr_t)ov);
627
628         if(AcceptEx(eh->GetFd(), fd, ao->buf, 0, len, len, &dwBytes, &ov->m_overlap) == FALSE)
629         {
630                 int err = WSAGetLastError();
631                 if(err != WSA_IO_PENDING)
632                 {
633                         printf("PostAcceptEvent err: %d\n", err);
634                 }
635         }
636 }
637
638
639 std::string IOCPEngine::GetName()
640 {
641         return "iocp";
642 }
643
644 EventHandler * IOCPEngine::GetRef(int fd)
645 {
646         std::map<int, EventHandler*>::iterator itr = m_binding.find(fd);
647         return (itr == m_binding.end()) ? 0 : itr->second;
648 }
649
650 bool IOCPEngine::HasFd(int fd)
651 {
652         return (GetRef(fd) != 0);
653 }
654
655 bool IOCPEngine::BoundsCheckFd(EventHandler* eh)
656 {
657         int* internal_fd = (int*)fdExt.get(eh);
658         if (!eh || eh->GetFd() < 0)
659                 return false;
660
661         if(!internal_fd)
662                 return false;
663
664         if(*internal_fd > MAX_DESCRIPTORS)
665                 return false;
666
667         return true;
668 }
669
670 EventHandler * IOCPEngine::GetIntRef(int fd)
671 {
672         if(fd < 0 || fd > MAX_DESCRIPTORS)
673                 return 0;
674         return ref[fd];
675 }
676
677 int IOCPEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen)
678 {
679         //SOCKET s = fd->GetFd();
680
681         Overlapped* acceptevent = (Overlapped*)acceptExt.get(fd);
682         if (!acceptevent)
683                 /* Shit, no accept event on this socket! :( */
684                 return -1;
685
686         Overlapped* ovl = acceptevent;
687         accept_overlap* ov = (accept_overlap*)ovl->m_params;
688
689         //sockaddr_in* server_address = (sockaddr_in*)&ov->buf[10];
690         sockaddr_in* client_address = (sockaddr_in*)&ov->buf[38];
691
692         memcpy(addr, client_address, sizeof(sockaddr_in));
693         *addrlen = sizeof(sockaddr_in);
694
695         return ov->socket;
696 }
697
698 int IOCPEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen)
699 {
700         Overlapped* ovl = (Overlapped*)acceptExt.get(fd);
701
702         if (!ovl)
703                 return -1;
704
705         accept_overlap* ov = (accept_overlap*)ovl->m_params;
706
707         sockaddr_in* server_address = (sockaddr_in*)&ov->buf[10];
708         //sockaddr_in* client_address = (sockaddr_in*)&ov->buf[38];
709
710         memcpy(name, server_address, sizeof(sockaddr_in));
711         *namelen = sizeof(sockaddr_in);
712
713         return 0;
714 }
715
716 int IOCPEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
717 {
718         this->UpdateStats(len, 0);
719         udp_overlap* ov = (udp_overlap*)readExt.get(fd);
720         if (!ov)
721                 return -1;
722         memcpy(buf, ov->udp_buffer, ov->udp_len);
723         memcpy(from, ov->udp_sockaddr, *fromlen);
724         return ov->udp_len;
725 }
726
727 int IOCPEngine::Blocking(int fd)
728 {
729         unsigned long opt = 0;
730         return ioctlsocket(fd, FIONBIO, &opt);
731 }
732
733 int IOCPEngine::NonBlocking(int fd)
734 {
735         unsigned long opt = 1;
736         return ioctlsocket(fd, FIONBIO, &opt);
737 }
738
739 int IOCPEngine::Close(int fd)
740 {
741         return closesocket(fd);
742 }
743
744 int IOCPEngine::Close(EventHandler* fd)
745 {
746         return this->Close(fd->GetFd());
747 }
748
749 SocketEngine* CreateSocketEngine()
750 {
751         return new IOCPEngine;
752 }
753 #endif