IEG4180 Tutorial 6 Overlapping IO Project 3 Discussion - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

IEG4180 Tutorial 6 Overlapping IO Project 3 Discussion

Description:

Instead of using event object notification, make the OS calls one of your ... hEvent can be freely used by your code, just like the LPVOID parameter in thread ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 25
Provided by: bos86
Category:

less

Transcript and Presenter's Notes

Title: IEG4180 Tutorial 6 Overlapping IO Project 3 Discussion


1
IEG4180 Tutorial 6 Overlapping I/O Project 3
Discussion
  • Bosco, Fong Chi Hang

(Acknowledgement some materials in this tutorial
are adopted from previous works by Shing and
Zero.)
2
Outline
  • Traditional Blocking I/O Overlapped I/O
  • Event Object Signaling
  • Alertable I/O

3
Overlapped I/O
  • Advantages
  • Non-blocking
  • Use application buffers to receive data directly
  • Allow posting multiple receive calls

4
Overlapped I/O Create Overlapped Socket
  • Use WSASocket() instead of socket()
  • Use normal bind(), accept(), connect() etc

5
Overlapped I/O Send Receive Data
  • For TCP, use
  • WSASend()
  • WSARecv()
  • For UDP, use
  • WSASendTo()
  • WSARecvFrom()

6
Overlapped 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)

7
Overlapped 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!!!

8
Overlapped 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
9
Overlapped I/O WSAOVERLAPPED structure
  • A mean for notification

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

10
Overlapped I/O The Model - Use of Event Object
Need to Figure Out which Buffer is Being Filled
(or Returned)
11
Overlapped 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)
12
Overlapped 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)
13
Overlapped I/O Alertable I/O
Move Data Processing to the Completion Routine
14
Overlapped 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

15
Overlapped 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 / )
16
Project 3 Overview
  • Project 3 is divided into three parts.
  • Web Console NetProbe Server
  • SuperNetProbe
  • JavaNetProbe

17
Web 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

18
SuperNetProbe JavaNetProbe
  • SuperNetProbe
  • Threading (Project 2)
  • Message-Driven I/O (Project 2)
  • Alertable overlapped I/O
  • JavaNetProbe (with GUI)
  • Threading
  • New I/O

19
HTTP 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
20
HTTP 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.
21
Introduction
  • 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

22
Idea
  • On specific event (e.g. onload, onClick,)
  • Create XMLHttpRequest object
  • Use XMLHttpRequest to submit further requests
  • Handle the response when necessary

23
Example
24
Project Code
Write a Comment
User Comments (0)
About PowerShow.com