Opto MMP Toolkit
 All Classes Functions Variables
Public Member Functions | Public Attributes | Protected Attributes | List of all members
O22SnapIoStream Class Reference

#include <O22SIOST.h>

Public Member Functions

int OpenStreaming (long nType, long nLength, long nPort)
 
int CloseStreaming ()
 
int SetCallbackFunctions (STREAM_CALLBACK_PROC pStartThreadCallbackFunc, void *pStartThreadParam, STREAM_EVENT_CALLBACK_PROC pStreamEventCallbackFunc, void *pStreamEventParam, STREAM_CALLBACK_PROC pStopThreadCallbackFunc, void *pStopThreadParam)
 
int StartStreamListening (char *pchIpAddressArg, long nTimeoutMS)
 
int StopStreamListening (char *pchIpAddressArg)
 
int GetLastStreamStandardBlockEx (SIOMM_StreamStandardBlock *pStreamData)
 
*int GetLastStreamCustomBlockEx (SIOMM_StreamCustomBlock *pStreamData)
 
int StreamHandler ()
 
int CheckStreamTimeouts ()
 

Public Attributes

bool m_bListenToStreaming
 A flag used in StreamThread() to know when to stop listening.
 
uint8_t * m_pbyLastStreamBlock
 Byte array containing the last block received.
 
SIOMM_StreamCustomBlock m_LastStreamBlock
 
STREAM_CALLBACK_PROC m_pStartThreadCallbackFunc
 User callback function.
 
STREAM_EVENT_CALLBACK_PROC m_pStreamEventCallbackFunc
 User callback function.
 
STREAM_CALLBACK_PROC m_pStopThreadCallbackFunc
 User callback functio.
 
void * m_pStartThreadParam
 User callback parameter.
 
void * m_pStreamEventParam
 User callback parameter.
 
void * m_pStopThreadParam
 User callback parameter.
 

Protected Attributes

SOCKET m_StreamSocket
 The handle to the UDP socket.
 
long m_nStreamType
 The type set in OpenStreaming()
 
long m_nStreamLength
 The length set in OpenStreaming()
 
O22StreamItempStreamList
 Head of a list of I/O units to listen for.
 
long nStreamListCount
 The number of items in pStreamList.
 

Detailed Description

The O22SnapIoStream C++ class is used to listen to UDP stream packets from multiple Opto 22 SNAP Ethernet I/O units streaming to the same port on the computer running this code.

The basic procedure for using this class is:

  1. Create an instance of the O22SnapIoStream class
  2. Call SetCallbackFuntions() to initialize several callback functions
  3. Call OpenStreaming() to initialize the stream type, length, and port
  4. Call StartStreamListening() for each I/O unit. A second thread will be created to listen for incoming UDP packets.
  5. The Stream Event callback function will be called every time a stream packet from a registered I/O unit is received. Every time this callback is called, the function GetLastStreamStandardBlockEx() or GetLastStreamCustomBlockEx() should be called, depending on the type set in step #3.
  6. StopStreamListening() may be called at any time to stop listening for a specified I/O unit.
  7. StartStreamListening() may be called at any time to add I/O units.
  8. When done, call CloseStreaming()

This class will eat all UDP packets sent to the port specified in OpenStreaming(). No other program on the same computer can listen to UDP packets on the same port while this code is running.

Member Function Documentation

int O22SnapIoStream::CheckStreamTimeouts ( )

Checks if any stream items have timed out.

NOTE: This method is not considered a public API and should not be called by user programs. It's public in the class sense only so that the StreadThread() function in O22SIOST.cpp can call it.

int O22SnapIoStream::CloseStreaming ( )

Stops all listening and closes the port.

Returns
SIOMM_OK if everything is OK, an error otherwise.
int O22SnapIoStream::GetLastStreamCustomBlockEx ( SIOMM_StreamCustomBlock pStreamData)

Returns the last stream packet received.

This method should be called immediately after receiving a packet event via the pStreamEventCallbackFunc callback function set in SetCallbackFunctions.

This method is used if you're using custom streams. If you're using standard streams, use GetLastStreamStandardBlockEx.

Parameters
pStreamDataPointer to a SIOMM_StreamCustomBlock instance to hold the received data.
int O22SnapIoStream::GetLastStreamStandardBlockEx ( SIOMM_StreamStandardBlock pStreamData)

Returns the last stream packet received.

This method should be called immediately after receiving a packet event via the pStreamEventCallbackFunc callback function set in SetCallbackFunctions.

This method is used if you're using standard streams. If you're using custom streams, use GetLastStreamCustomBlockEx.

Parameters
pStreamDataPointer to a SIOMM_StreamStandardBlock instance to hold the received data.
int O22SnapIoStream::OpenStreaming ( long  nType,
long  nLength,
long  nPort 
)

Opens and initializes the computer's port ot start listening for stream packets.

Parameters
nTypeSIOMM_STREAM_TYPE_STANDARD for standard streams, SIOMM_STREAM_TYPE_CUSTOM for custom streams. The type is set in the I/O unit's stream configuration. This must match it and all I/O units streaming to the same port should be of the same type.
nLengthIf using custom streams, the size of a single custom block.
nPortThe port being streamed to
Returns
SIOMM_OK if everything initialized correctly.
int O22SnapIoStream::SetCallbackFunctions ( STREAM_CALLBACK_PROC  pStartThreadCallbackFunc,
void *  pStartThreadParam,
STREAM_EVENT_CALLBACK_PROC  pStreamEventCallbackFunc,
void *  pStreamEventParam,
STREAM_CALLBACK_PROC  pStopThreadCallbackFunc,
void *  pStopThreadParam 
)

Sets the three callback functions used by the stream listening thread.

Parameters
pStartThreadCallbackFuncThis callback function is called whenever the stream listening thread is started. Can be NULL if the callback function is not needed.
pStartThreadParamUser data that will be passed back to pStartThreadCallbackFunc. Can be NULL.
pStreamEventCallbackFuncThis callback funciton is called whenver a stream packet from an I/O unit is received.
pStreamEventParamUser data that will be passed back to pStreamEventCallbackFunc. Can be NULL.
pStopThreadCallbackFuncThis callback function is called when the stream listening thread is stopped. Can be NULL.
pStopThreadParamUser data that will be passed back to pStopThreadCallbackFunc. Can be NULL.
Returns
SIOMM_OK
int O22SnapIoStream::StartStreamListening ( char *  pchIpAddressArg,
long  nTimeoutMS 
)

Starts listening for packets from an I/O unit at the specified IP address.

More than one I/O unit may be listened to by multiple calls to this function.

Parameters
pchIpAddressArgIP address of I/O unit in string form, e.g. "1.2.3.4"
nTimeoutMSTimeout value used to generate error events if the specified length of time has passed since a stream packet has been received. Different I/O units can have different timeout values.
Returns
SIOMM_OK if everything is OK, an error otherwise.
int O22SnapIoStream::StopStreamListening ( char *  pchIpAddressArg)

Stops listening to the I/O unit at the specified address.

Parameters
pchIpAddressArgIP address of I/O unit in string form, e.g. "1.2.3.4"
Returns
SIOMM_OK if everything is OK, an error otherwise.
int O22SnapIoStream::StreamHandler ( )

Main worker method.

NOTE: This method is not considered a public API and should not be called by user programs. It's public in the class sense only so that the StreadThread() function in O22SIOST.cpp can call it.

Called repeatedly by the StreamThread() function. This method checks the socket for new packets and determines if the user is interested in them.

Member Data Documentation

SIOMM_StreamCustomBlock O22SnapIoStream::m_LastStreamBlock

Structure containing the last stream block received. It can be converted to a SIOMM_StreadStandardBlock instance in GetLastStreamStandardBlockEx


The documentation for this class was generated from the following files: