Inherits WOscReceiver.
Inherited by WOscTcpClient, and WOscTcpServerHandler.
Classes | |
class | SelectRxState |
Public Types | |
enum | Error { WOS_ERR_NO_ERROR = 0, WOS_ERR_ALREADY_RUNNING, WOS_ERR_ALREADY_CONNECTED, WOS_ERR_SOCKET_CREATE, WOS_ERR_SOCKET_CONNECT, WOS_ERR_CREATE_RX_THREAD, WOS_ERR_NO_SOCKET, WOS_ERR_CONNECTION_LOST, WOS_ERR_ALREADY_CLOSED, WOS_ERR_DISCONNECTED = WOS_ERR_ALREADY_CLOSED, WOS_ERR_CONNECTION_CLOSED, WOS_ERR_CONNECTION_FAILURE, WOS_ERR_NOT_WHEN_THREADING, WOS_ERR_CAN_NOT_SET_NONBLOCKING, WOS_ERR_INVALID_SOCKET, WOS_ERR_USER } |
Public Member Functions | |
WOscTcpHandler (bool threading=true) | |
Error | NetworkSend (const char *data, int dataLen) |
Error | NetworkReceive () |
Error | Start (int socketID, const TheNetReturnAddress &peer) |
Error | Stop () |
int | GetSocketID () const |
int | GetRxThreadID () const |
bool | GetThreading () const |
const TheNetReturnAddress & | GetPeer () const |
Static Public Member Functions | |
static const char * | GetErrStr (Error err) |
Protected Member Functions | |
virtual void | NetworkSend (const char *data, int dataLen, const WOscNetReturn *networkReturnAddress) |
virtual void | ReceiveThread () |
Error | NetworkReceiveInternal () |
Friends | |
void * | wosc_tcp_handler_rx_thread (void *argument) |
Definition at line 52 of file WOscTcpHandler.h.
Errors which can occur in the network layer.
Definition at line 58 of file WOscTcpHandler.h.
00059 { 00060 /** No error. */ 00061 WOS_ERR_NO_ERROR = 0, 00062 /** Seems that a reception thread is already running. You have to 00063 * disconnect first. 00064 */ 00065 WOS_ERR_ALREADY_RUNNING, 00066 /** Seems that we're already connected. */ 00067 WOS_ERR_ALREADY_CONNECTED, 00068 /** Socket creation failed. */ 00069 WOS_ERR_SOCKET_CREATE, 00070 /** Socket connect failed. */ 00071 WOS_ERR_SOCKET_CONNECT, 00072 /** Creation of receive thread failed. */ 00073 WOS_ERR_CREATE_RX_THREAD, 00074 /** Socket not connected. */ 00075 WOS_ERR_NO_SOCKET, 00076 /** Socket connection lost. */ 00077 WOS_ERR_CONNECTION_LOST, 00078 /** Trying to close an already closed socket. */ 00079 WOS_ERR_ALREADY_CLOSED, 00080 /** */ 00081 WOS_ERR_DISCONNECTED = WOS_ERR_ALREADY_CLOSED, 00082 /** Tried to receive with WOscTcpClient::NetworkReceive() from a 00083 * connection which has been closed. */ 00084 WOS_ERR_CONNECTION_CLOSED, 00085 /** Receiving from WOscTcpClient::NetworkReceive() failed. */ 00086 WOS_ERR_CONNECTION_FAILURE, 00087 /** The select/polling receive function can only be used with 00088 * threading disabled. 00089 */ 00090 WOS_ERR_NOT_WHEN_THREADING, 00091 /** Unable to set socket into non blocking mode. */ 00092 WOS_ERR_CAN_NOT_SET_NONBLOCKING, 00093 /** Invalid socket handler passed. */ 00094 WOS_ERR_INVALID_SOCKET, 00095 /** User defined errors start here. */ 00096 WOS_ERR_USER, 00097 } Error;
WOscTcpHandler::WOscTcpHandler | ( | bool | threading = true |
) |
Implements a full featured OSC client with TCP transport.
threading | If true, all reception happens in a separate thread and does not require any select logic in the main loop, but you have to take care when calling main thread code from the OSC message handlers/methods. If false, threading is disabled and the reception must be implemented by a select statement. On input WOscTcpClient::NetworkReceive() must be called. The socket is switched to non blocking automatically. See the streaming server example for further details. |
Definition at line 68 of file WOscTcpHandler.cpp.
int WOscTcpHandler::GetSocketID | ( | ) | const [inline] |
Get Socket file descriptor for use in select or similar.
Definition at line 104 of file WOscTcpHandler.h.
Referenced by WOscTcpClient::Close(), and WOscTcpClient::Connect().
void WOscTcpHandler::NetworkSend | ( | const char * | data, | |
int | dataLen, | |||
const WOscNetReturn * | networkReturnAddress | |||
) | [protected, virtual] |
Virtual function which has to be overridden by the user. It then can be used to send OSC messages from within WOscReceiverMethod message callbacks. It is the interface to the network layer for sending messages.
data | Pointer to buffer containning the data to be sent. | |
dataLen | Length of the data in the buffer. | |
networkReturnAddress | Destination network address for the data. |
Implements WOscReceiver.
Definition at line 133 of file WOscTcpHandler.cpp.
References NetworkSend().
00135 { 00136 NetworkSend(data, dataLen); 00137 }
WOscTcpHandler::Error WOscTcpHandler::NetworkSend | ( | const char * | data, | |
int | dataLen | |||
) |
Transmit data (preferrably OSC messages and bundles) to the server.
Definition at line 109 of file WOscTcpHandler.cpp.
References WOS_ERR_CONNECTION_LOST, WOS_ERR_NO_ERROR, and WOS_ERR_NO_SOCKET.
Referenced by NetworkSend().
00110 { 00111 pthread_mutex_lock(&m_mutexTx); 00112 if ( m_socketID < 0 ) { 00113 pthread_mutex_unlock(&m_mutexTx); 00114 return WOS_ERR_NO_SOCKET; 00115 } 00116 00117 int len = htonl(dataLen); 00118 len = wosc_send_over_socket(m_socketID, (const char*)&len, sizeof(len)); 00119 if ( len <= 0 ) { 00120 pthread_mutex_unlock(&m_mutexTx); 00121 return WOS_ERR_CONNECTION_LOST; 00122 } 00123 len = wosc_send_over_socket(m_socketID, data, dataLen); 00124 if ( len <= 0 ) { 00125 pthread_mutex_unlock(&m_mutexTx); 00126 return WOS_ERR_CONNECTION_LOST; 00127 } 00128 pthread_mutex_unlock(&m_mutexTx); 00129 return WOS_ERR_NO_ERROR; 00130 }
WOscTcpHandler::Error WOscTcpHandler::Stop | ( | ) |
Shutdown and join the receiving thread if in threading mode. The socket must be closed beforehand.
Definition at line 203 of file WOscTcpHandler.cpp.
References WOS_ERR_ALREADY_CLOSED, WOS_ERR_DISCONNECTED, and WOS_ERR_NO_ERROR.
Referenced by WOscTcpClient::Close().
00204 { 00205 // Check if reception thread is already running 00206 if ( m_threading && m_receptionThreadID == 0 ) 00207 return WOS_ERR_ALREADY_CLOSED; 00208 00209 // Check if someone stopped us already 00210 if ( m_socketID < 0 ) 00211 return WOS_ERR_DISCONNECTED; 00212 00213 // Check if reception thread is already running 00214 if ( m_threading ) { 00215 // join rx thread with main thread 00216 void* threadReturn = NULL; 00217 int ret = pthread_cancel(m_receptionThreadID); 00218 if ( ret ) { 00219 // std::cerr<<"Receive thread join failed."<<std::endl; 00220 } 00221 ret = pthread_join(m_receptionThreadID, &threadReturn); 00222 if ( ret ) { 00223 // std::cerr<<"Receive thread join failed."<<std::endl; 00224 } 00225 m_receptionThreadID = 0; 00226 } 00227 m_socketID = -1; 00228 00229 // set 00230 return WOS_ERR_NO_ERROR; 00231 }
void* wosc_tcp_handler_rx_thread | ( | void * | argument | ) | [friend] |
The bootstrapper is our friend.
WOscTCPClient.cpp - A streaming OSC client using posix threads and sockets
Created on: Apr 21, 2010 Author: uli Bootstrapper for receive thread member function.
Definition at line 28 of file WOscTcpHandler.cpp.
00029 { 00030 WOscTcpHandler* handler = static_cast<WOscTcpHandler*>(argument); 00031 handler->ReceiveThread(); 00032 pthread_exit(NULL); 00033 }