Title: IEG4180 Tutorial 6 Overlapping IO Project 3 Discussion
1IEG4180 Tutorial 6 Overlapping I/O Project 3
Discussion
(Acknowledgement some materials in this tutorial
are adopted from previous works by Shing and
Zero.)
2Outline
- Traditional Blocking I/O Overlapped I/O
- Event Object Signaling
- Alertable I/O
3Overlapped I/O
- Advantages
- Non-blocking
- Use application buffers to receive data directly
- Allow posting multiple receive calls
4Overlapped I/O Create Overlapped Socket
- Use WSASocket() instead of socket()
- Use normal bind(), accept(), connect() etc
5Overlapped I/O Send Receive Data
- For TCP, use
- WSASend()
- WSARecv()
- For UDP, use
- WSASendTo()
- WSARecvFrom()
6Overlapped I/O Receive
- Important parameters for WSARecv and WSARecvFrom
- Socket
- Array of WSABUF structures
- Number of elements in WSABUF array
- WSAOVERLAPPED structure
- Pointer to I/O completion routine (used for
alertable I/O)
7Overlapped I/O Receive
- The return value
- Does not return the number of bytes received.
- Only tell you it success or error.
- SOCKET_ERROR may be returned even there was no
error. - Use WSAGetLastError() to check, if error code is
WSA_IO_PENDING, it means there is no error!!!
8Overlapped I/O WSABUF
- The definition of buffer for overlapped I/O
- len
- The length of buffer
- Have to be filled in advance
- buf
- The memory space that actually hold the data
typedef struct __WSABUF u_long len char
FAR buf WSABUF, LPWSABUF
9Overlapped I/O WSAOVERLAPPED structure
typedef struct _WSAOVERLAPPED DWORD Internal
DWORD InternalHigh DWORD Offset DWORD
OffsetHigh WSAEVENT hEvent WSAOVERLAPPED,
LPWSAOVERLAPPED
- hEvent
- Function call returns immediately, some
mechanisms are needed to determine the status and
the completion of the request - Used in event object notification
10Overlapped I/O The Model - Use of Event Object
Need to Figure Out which Buffer is Being Filled
(or Returned)
11Overlapped I/O Event Object Notification
- Create an event object
- Similar to Mutex and Semaphore, event objects
also have signaled or nonsignaled state - Pass this object to hEvent of the WSAOVERLAPPED
structure - To know when the I/O operation complete
- WSAWaitForMultipleEvents()
- To retrieve the results of overlapped operations
- WSAGetOverlappedResult()
- Reset the event object to nonsignaled state
- WSAResetEvent()
- To free resources occupied by the event object
- WSACloseEvent()
WSAEVENT WSACreateEvent(void)
12Overlapped I/O Alertable I/O-Introduction
- Instead of using event object notification, make
the OS calls one of your functions when I/O
operations complete - Completion routines
- Functions that will be called when I/O complete
- Specified in the last parameter of WSASend() /
WSASendTo() / WSARecv() / WSARecvFrom()
int i WSARecvFrom(..., lpOverlapped,
lpCompletionRoutine)
13Overlapped I/O Alertable I/O
Move Data Processing to the Completion Routine
14Overlapped I/O Alertable I/O -Completion Routines
void CALLBACK CompletionRoutine( IN DWORD
dwError, / the error code / IN
DWORD cbTransferred, / in bytes / IN
LPWSAOVERLAPPED lpOverlapped, / the structure of
this I/O / IN DWORD dwFlags )
- cbTransferred
- Number of bytes transferred
- Equals to zero when connection is closed
- lpOverlapped
- hEvent can be freely used by your code, just like
the LPVOID parameter in thread procedure - You have to manage the buffer usage yourself!
- For example, you issued 10 WSARecv() with 10
buffers - The data will be filled in the buffers according
to the calling order - Reissue WSARecv() on processed buffers
15Overlapped I/O Alertable Wait state
- The thread using alertable I/O has to enter
alertable wait state, so that the completion
routines can be called - To enter alertable wait state
- Just like the ordinary Sleep()
- Return when timeout or completion
DWORD SleepEx( DWORD dwMilliseconds, BOOL
bAlertable / set to true / )
16Project 3 Overview
- Project 3 is divided into three parts.
- Web Console NetProbe Server
- SuperNetProbe
- JavaNetProbe
17Web Console NetProbe Server
- Extend the NetProbe Server in Project 2
- Web-UI open a new TCP port to accept HTTP
request from a web browser (Not necessary to use
select-based I/O for this port) - Use web browser to connect to the NetProbe
Server, returning a webpage(a form) for user to
configure - Maximum number of connections
- Start/Stop the Server in receiving Clients
connections - Killing a particular client connection
- Another page (with AJAX) to show
- Number of concurrent transmissions
- Statistics of each connection
18SuperNetProbe JavaNetProbe
- SuperNetProbe
- Threading (Project 2)
- Message-Driven I/O (Project 2)
- Alertable overlapped I/O
- JavaNetProbe (with GUI)
- Threading
- New I/O
19HTTP Request
- Browser normally analyze the URL in
- If no port given, browser normally determines the
port number by protocol - And send (suppose HTTP is used) the following to
host at port
protocol//hostport/path
GET /path HTTP/version supported
20HTTP Response
- The server then responds
- Status
HTTP/1.0 200 OK Date Fri, 31 Dec 1999 235959
GMT Content-Type text/html Content-Length 1354
lthtmlgt ltbodygt lt/bodygt lt/htmlgt
200 OK The request succeeded, and the resulting
resource (e.g. file or script output) is returned
in the message body.
21Introduction
- AJAX Asynchronous JavaScript and XML
- Mainly based on
- HTML (DOM)
- JavaScript
- XML
- Goods
- Smoother experience
- No need to refresh the whole page
- Asynchronize request
- Rich Internet Application
- Reference
- http//www.w3schools.com
22Idea
- On specific event (e.g. onload, onClick,)
- Create XMLHttpRequest object
- Use XMLHttpRequest to submit further requests
- Handle the response when necessary
23Example
24Project Code