Polls the stream for events, such as whether it is possible to send or receive without blocking.

Namespace:  Quanser.Communications
Assembly:  Quanser.Communications (in Quanser.Communications.dll)

Syntax

Visual Basic (Declaration)
Public Function Poll ( _
	timeout As Timeout, _
	flags As Integer _
) As Integer
C#
public int Poll(
	Timeout timeout,
	int flags
)
Visual C++
public:
int Poll(
	Timeout^ timeout, 
	int flags
)
JavaScript
function poll(timeout, flags);

Parameters

timeout
Type: Quanser.Common..::.Timeout
A relative or absolute timeout which determines the maximum time that this method will wait for one of the conditions to occur before returning. A value of nullptr or System.DBNull indicates an infinite timeout.
flags
Type: System..::.Int32
A bit mask indicating the conditions for which to check. Combine one or more of the following flags using a bitwise-OR:
FlagDescription
Receive On a listening stream, check for pending connections from clients. On a client stream, check whether there is any data available to receive.
Send Not valid on a listening stream. On a client stream, check whether there is any space in the stream buffer to store any data.
Flush Not valid on a listening stream. On a client stream, check whether it is possible to flush any more data without blocking.
Accept Not valid on a client stream. On a listening stream, check whether there is a pending client connection.
Connect Not valid on a listening stream. On a client stream, check whether the connection has completed.

Return Value

A bit mask containing the conditions which were satisfied. It has the same definition as the flags argument. If none of the specified conditions occurs before the timeout, then 0 is returned.

Remarks

This method polls the stream to determine whether it is possible to send, receive, accept a connection, or complete a connection without blocking. The flags argument determines the conditions for which to check. The return value indicates the conditions which occurred. This method returns after the given timeout with a value of 0 if none of the conditions occurs. If an error occurs, then it throws a StreamException. The method will return before the timeout if at least one of the conditions occurs prior to the timeout.

Examples

This example connects to a remote host serving on TCP/IP port 18000 and receives doubles from that remote host using non-blocking I/O.
C# Copy Code
Timeout   timeout           = new Timeout(30);      /* 30 seconds */
Timeout   smallTimeout      = new Timeout(0.010);   /* 10 milliseconds */
Stream    stream            = new Stream();
String    uri               = "tcpip://localhost:18000";
int       sendBufferSize    = 1024;
int       receiveBufferSize = 1024;
bool      done              = false;
double [] buffer            = new double [2];

try {
    if (!stream.Connect(uri, true, sendBufferSize, receiveBufferSize)) {
        /* Connection is in progress. Wait up to 30 seconds for connection to complete. */
        if (stream.Poll(timeout, Stream.PollFlag.Connect) != Stream.PollFlag.Connect) {
            throw new Exception("Timed out waiting to connect");
        }
    }

    try {
        while (!done) {
            /*
                all then we could just omit the Poll (since the stream is non-blocking anyway) or
                use a zero timeout.
            */
            if (stream.Poll(smallTimeout, Stream.PollFlag.Receive) == Stream.PollFlag.Receive) {
                if (stream.ReceiveArray(buffer) == 1) { /* then the full array of doubles received (2 doubles) */
                    /* ... process data received ... */
                }
            } else {
                /* ... do some other work (e.g. progress bar, set done to true if user quits) ... */
            }
        }
    } catch (Exception ex) {
        Console.WriteLine("Error communicating with client on URI '" + uri + "'. " + ex);
    }

    stream.Close();
} catch (Exception ex) {
    Console.WriteLine("Unable to connect to URI '" + uri + "'. " + ex);
}
Visual Basic Copy Code
Dim timeout As New Timeout(30) ' 30 seconds
Dim smallTimeout As New Timeout(0.010) ' 10 milliseconds
Dim stream As New Stream()
Dim uri As String = "tcpip://localhost:18000"
Dim sendBufferSize As Integer = 1024
Dim receiveBufferSize As Integer = 1024
Dim done As Boolean = False
Dim buffer(1) As Double ' array of 2 doubles

Try
    If Not stream.Connect(uri, true, sendBufferSize, receiveBufferSize) Then
        ' Connection is in progress. Wait up to 30 seconds for connection to complete.
        If stream.Poll(timeout, Stream.PollFlag.Connect) <> Stream.PollFlag.Connect Then
            Throw New Exception("Timed out waiting to connect")
        End If
    End If

    Try
        While Not done
            ' Wait briefly for at least one byte to arrive. Note that if we did not want to wait
            ' all then we could just omit the Poll (since the stream is non-blocking anyway) or
            ' use a zero timeout.
            If stream.Poll(smallTimeout, Stream.PollFlag.Receive) = Stream.PollFlag.Receive Then
                If stream.ReceiveArray(buffer) = 1 Then ' then the full array of doubles received (2 doubles)
                    ' ... process data received ...
                End If
            Else
                ' ... do some other work (e.g. progress bar, set done to true if user quits) ...
            End If
        End While
    Catch ex As Exception
        Console.WriteLine("Error communicating with client on URI '" & uri & "'. " & ex.ToString())
    End Try

    stream.Close()
Catch ex As Exception
    Console.WriteLine("Unable to connect to URI '" & uri & "'. " & ex.ToString())
End Try
Visual C++ Copy Code
Timeout^ timeout           = gcnew Timeout(30);     /* 30 seconds */
Timeout^ smallTimeout      = gcnew Timeout(0.010);  /* 10 milliseconds */
Stream^  stream            = new Stream();
String^  uri               = L"tcpip://localhost:18000";
int      sendBufferSize    = 1024;
int      receiveBufferSize = 1024;
bool     done              = false;

array<double>^ buffer = gcnew array<double>(2);

try {
    if (!stream->Connect(uri, true, sendBufferSize, receiveBufferSize)) {
        /* Connection is in progress. Wait up to 30 seconds for connection to complete. */
        if (stream->Poll(timeout, Stream::PollFlag::Connect) != Stream::PollFlag::Connect) {
            throw gcnew Exception(L"Timed out waiting to connect");
        }
    }

    try {
        while (!done) {
            /*
                all then we could just omit the Poll (since the stream is non-blocking anyway) or
                use a zero timeout.
            */
            if (stream->Poll(smallTimeout, Stream::PollFlag::Receive) == Stream::PollFlag::Receive) {
                if (stream->ReceiveArray(buffer) == 1) { /* then the full array of doubles received (2 doubles) */
                    /* ... process data received ... */
                }
            } else {
                /* ... do some other work (e.g. progress bar, set done to true if user quits) ... */
            }
        }
    } catch (Exception ex) {
        Console::WriteLine("Error communicating with client on URI '" + uri + "'. " + ex);
    }

    stream->Close();
} catch (Exception ex) {
    Console::WriteLine("Unable to connect to URI '" + uri + "'. " + ex);
}

Exceptions

ExceptionCondition
Quanser.Communications..::.StreamException If an error occurs then an exception is thrown. This situation typically arises if one of the flags used is inconsistent with the type of stream or an error has occurred in the underlying communication channel.

See Also