IEG 4180 Tutorial 4 - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

IEG 4180 Tutorial 4

Description:

When using overlapped I/O, the I/O operation will run in the background ... void CALLBACK CompletionRoutine( IN DWORD dwError, /* the error code ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 18
Provided by: courseIe
Category:

less

Transcript and Presenter's Notes

Title: IEG 4180 Tutorial 4


1
IEG 4180 Tutorial 4
  • Prepared by Shing
  • (Remark Modified by Zero)

2
Outline
  • Traditional Blocking I/O
  • Overlapped I/O
  • Event Object Signaling
  • Alertable I/O

3
Traditional Blocking I/O
Time Required A B C D
Max Rate Packet Size / Time Required
4
Overlapped I/O
  • When using overlapped I/O, the I/O operation will
    run in the background
  • While the I/O operation runs in the background,
    the application can do some other processing
  • When the I/O operation completes, the application
    is notified
  • There are multiple mechanisms for notifying the
    application that an I/O operation has been
    completed
  • Event Object Signaling
  • Alertable I/O

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

6
Overlapped I/O The Model - Use of Event Object
Need to Figure Out which Buffer is Being Filled
(or Returned)
7
Overlapped I/O Create Overlapped Socket
  • Use WSASocket() instead of socket()
  • Use normal bind(), accept(), connect() etc

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

9
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)

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

11
Overlapped I/O WSABUF
  • The definition of buffer for overlapped I/O
  • len
  • The of buffer
  • Have to be filled in in advance
  • buf
  • The memory space that actually hold the data

typedef struct __WSABUF u_long len char
FAR buf WSABUF, LPWSABUF
12
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

13
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)
14
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)
15
Overlapped I/O Alertable I/O
Move Data Processing to the Completion Routine
16
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

17
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 / )
Write a Comment
User Comments (0)
About PowerShow.com