Presentation is loading. Please wait.

Presentation is loading. Please wait.

Socket Models Different ways to manage your connections.

Similar presentations


Presentation on theme: "Socket Models Different ways to manage your connections."— Presentation transcript:

1 Socket Models Different ways to manage your connections

2 Agenda Modes Modes Blocking/Non-Blocking Blocking/Non-Blocking Models Models Blocking Blocking Select Variations Select Variations Select Select WSAAsyncSelect WSAAsyncSelect WSAEventSelect WSAEventSelect Overlapped I/O Overlapped I/O I/O Completion Port I/O Completion Port

3 Socket Modes To Block or Not to Block

4 Blocking Mode The default The default Easy to implement Easy to implement A call to send() or recv() only returns when calling conditions met. A call to send() or recv() only returns when calling conditions met.

5 Non-Blocking Must set with I/O control Must set with I/O control Ioctlsocket(sock,FIONBIO,true); Ioctlsocket(sock,FIONBIO,true); Nothing blocks (i.e., send, recv, close,…) Nothing blocks (i.e., send, recv, close,…) Returns WSAEWOULDBLOCK if not immediately successful Returns WSAEWOULDBLOCK if not immediately successful Essentially turns into a polling model Essentially turns into a polling model You must handle spinning by sleeping You must handle spinning by sleeping Only use in a non-thread environment Only use in a non-thread environment

6 Modes vs. Model Modes can be used with any Model Modes can be used with any Model Models cover several general issues Models cover several general issues How you discover when a socket is ready How you discover when a socket is ready Has data (recv) Has data (recv) Has room for data (send) Has room for data (send) How data is copied from Kernel to your buffer How data is copied from Kernel to your buffer Number of threads Number of threads

7 I/O Models BlockingSelect Overlapped I/O Completion Port

8 Blocking Easy Implementation Easy Implementation X-Platform X-Platform No event notification to avoid blocking necessitates multiple threads. No event notification to avoid blocking necessitates multiple threads. 2 Threads for each socket else starve 2 Threads for each socket else starve Send thread Send thread Receive thread Receive thread Poor scaling performance due to # of threads Poor scaling performance due to # of threads Good enough for most game clients Good enough for most game clients Socket calls are blocking by default Socket calls are blocking by default

9 Select Working with multiple sockets Tell me when you’re ready select (…) select (…) File Descriptor Sets (FD_SET) File Descriptor Sets (FD_SET) Read: sockets to check for pending data Read: sockets to check for pending data Write: sockets to check for writable Write: sockets to check for writable OOB: same as read for OOB data OOB: same as read for OOB data Macros FD_CLR(), FD_ISSET(), FD_SET(), FD_ZERO() Macros FD_CLR(), FD_ISSET(), FD_SET(), FD_ZERO() Always clear and set your FD_SET prior to select (even if using same descriptors). Always clear and set your FD_SET prior to select (even if using same descriptors). Limited to 64 sockets by winsock.h but can be overridden. Limited to 64 sockets by winsock.h but can be overridden. X-Platform X-Platform Additional limitations by O/S Additional limitations by O/S Only checks state. Still need to process the data with blocking call which won’t block because you’ve just been told it’s ready. Only checks state. Still need to process the data with blocking call which won’t block because you’ve just been told it’s ready. WSAAsyncSelect (…) WSAAsyncSelect (…) Notified via window message Notified via window message Windows only Windows only WSAEventSelect (…) WSAEventSelect (…) Notified via Event Object i.e., a signal Notified via Event Object i.e., a signal Windows only Windows only These methods provide asynchronous notification, not async data transfer as does overlapped model These methods provide asynchronous notification, not async data transfer as does overlapped model

10 Overlapped I/O Tell me when data is in my buffer. Async data transfer Async data transfer Scales very well Scales very well Max 64 sockets per thread Max 64 sockets per thread A little complexity overhead A little complexity overhead All socket requests return immediately. All socket requests return immediately. Process with event objects or completion routines. Process with event objects or completion routines. Window NT based O/S only (NT, 2000, xp) Window NT based O/S only (NT, 2000, xp)

11 I/O Completion Ports Best Scalability Best Scalability One thread per CPU One thread per CPU Unlimited sockets per thread Unlimited sockets per thread Complex setup Complex setup Uses overlapped I/O constructs Uses overlapped I/O constructs CreateIoCompletionPort ( … ) CreateIoCompletionPort ( … ) One completion port per socket is created One completion port per socket is created GetQueuedCompletionStatus (…) GetQueuedCompletionStatus (…) 1 thread per proc handles all status using this method in each thread 1 thread per proc handles all status using this method in each thread


Download ppt "Socket Models Different ways to manage your connections."

Similar presentations


Ads by Google