void Bind( const IpEndpointName& localEndpoint )
    {
        struct sockaddr_in bindSockAddr;
        SockaddrFromIpEndpointName( bindSockAddr, localEndpoint );
        {
            IpEndpointName temp = IpEndpointNameFromSockaddr(bindSockAddr);
            char address[30];
            temp.AddressAndPortAsString(address);
        }
        if (bind(socket_, (struct sockaddr *)&bindSockAddr, sizeof(bindSockAddr)) < 0) {
            throw std::runtime_error("unable to bind udp socket\n");
        }

        isBound_ = true;
    }
示例#2
0
void OscListener::ProcessMessage(const osc::ReceivedMessage& message, const IpEndpointName& endpoint)
{
    char addressBuffer[256];

    endpoint.AddressAsString(addressBuffer);

    QList<QVariant> arguments;
    for (osc::ReceivedMessage::const_iterator iterator = message.ArgumentsBegin(); iterator != message.ArgumentsEnd(); ++iterator)
    {
        const osc::ReceivedMessageArgument& argument = *iterator;

        if (argument.IsBool())
            arguments.push_back(argument.AsBool());
        else if (argument.IsInt32())
            arguments.push_back(QVariant::fromValue<qint32>(argument.AsInt32()));
        else if (argument.IsInt64())
            arguments.push_back(QVariant::fromValue<qint64>(argument.AsInt64()));
        else if (argument.IsFloat())
            arguments.push_back(argument.AsFloat());
        else if (argument.IsDouble())
            arguments.push_back(argument.AsDouble());
        else if (argument.IsString())
            arguments.push_back(argument.AsString());
    }

    QString eventPath = QString("%1%2").arg(addressBuffer).arg(message.AddressPattern());

    QMutexLocker locker(&eventsMutex);
    this->events[eventPath] = arguments;
}
示例#3
0
//------------------------------------------------------------------------
void OSCListener::ProcessMessage( const osc::ReceivedMessage& m, const IpEndpointName& src )
{
    string address(m.AddressPattern());

    if (address == kGreensoundsAddr) {
        ReceivedMessageArgumentIterator i = m.ArgumentsBegin();
        while (i != m.ArgumentsEnd()) {
            if (i->IsString()) {
                string msg(i->AsStringUnchecked());
                if (msg == "hello") {
                    char buff[120];
                    src.AddressAsString(buff);
                    fSensors->connect(buff);
                }
                else if (msg == "version") {
                    fSensors->send (kGreensoundsAddr, "version", kVersion);
                }

            }
            else if (i->IsInt32()) {
            }
            else if (i->IsFloat()) {
            }
            i++;
        }
    }
}
示例#4
0
void OscListener::ProcessMessage( const ::osc::ReceivedMessage &m, const IpEndpointName& remoteEndpoint ) {
	Message* message = new Message();
	
	message->setAddress(m.AddressPattern());
	
	char endpoint_host[IpEndpointName::ADDRESS_STRING_LENGTH];
	remoteEndpoint.AddressAsString(endpoint_host);
	message->setRemoteEndpoint(endpoint_host, remoteEndpoint.port);
	
	for (::osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin(); arg != m.ArgumentsEnd(); ++arg){
		if (arg->IsInt32())
			message->addIntArg( arg->AsInt32Unchecked());
		else if (arg->IsFloat())
			message->addFloatArg(arg->AsFloatUnchecked());
		else if (arg->IsString())
			message->addStringArg(arg->AsStringUnchecked());
		else {
			assert(false && "message argument type unknown");
		}
	}
	
	lock_guard<mutex> lock(mMutex);
	
	if( mMessageReceivedCbs.empty() ){
		mMessages.push_back( message );
	}else{
		mMessageReceivedCbs.call( message );
		delete message;
	}
}
示例#5
0
	void Bind( const IpEndpointName& localEndpoint )
	{
		struct sockaddr_in bindSockAddr;
		SockaddrFromIpEndpointName( bindSockAddr, localEndpoint );
		
		// KEVIN: allow for multicast
		SetAllowReuse(true);
		struct ip_mreq group;
		if (localEndpoint.IsMulticastAddress()) {
			group.imr_interface.s_addr = htonl(INADDR_ANY);
			group.imr_multiaddr.s_addr = bindSockAddr.sin_addr.s_addr;
			if (setsockopt(socket_, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0) {
				throw std::runtime_error("error adding multicast group\n");
				closesocket(socket_); // winsock2 equivalent of close()
				exit(1);
			}
		}
		bindSockAddr.sin_addr.s_addr = INADDR_ANY;
		// KEVIN: the above line needs to be added for winsock2, otherwise bind
		// will fail with error 10049 WSAEADDRNOTAVAIL
		// solution suggested from: http://www.sockets.com/err_lst1.htm#WSAEADDRNOTAVAIL
        
        if (bind(socket_, (struct sockaddr *)&bindSockAddr, sizeof(bindSockAddr)) < 0) {
            throw std::runtime_error("unable to bind udp socket\n");
        }

		isBound_ = true;
	}
示例#6
0
void oscReceiver::ProcessMessage(const osc::ReceivedMessage &_m, const IpEndpointName &remoteEndpoint)
{
	boost::mutex::scoped_lock lock(listenerMutex);
	
	if (retrievalMode == SIGNAL_PARSER_ONLY_AFTER_RELEASE)
	{
		if (currentMessage != 0) return;
	}
	
	osc::ReceivedMessage::const_iterator arg = _m.ArgumentsBegin();
	
	currentMessage = new oscMessage(_m.AddressPattern(), oscMessage::NO_BUFFER);
	
	// set source IP
	char sourceString[ IpEndpointName::ADDRESS_STRING_LENGTH ];
    remoteEndpoint.AddressAsString( sourceString  );
	currentMessage->sourceIP = sourceString;
	
	while ( arg != _m.ArgumentsEnd() )
	{
		if 		( arg->IsBool() 	) { currentMessage->AppendBoolean	(arg->AsBool());}
		else if ( arg->IsInt32() 	) { currentMessage->AppendInteger	(arg->AsInt32());}
		else if ( arg->IsFloat() 	) { currentMessage->AppendFloat		(arg->AsFloat());}
		else if ( arg->IsString() 	) { currentMessage->AppendString	(arg->AsString());}
		else { std::cout << "Unrecognized osc argument type\n"; }
		arg++;
	}
	
	signalNewOscMessageReceived(currentMessage);
}
示例#7
0
void ofxOscReceiver::ProcessMessage( const osc::ReceivedMessage &m, const IpEndpointName& remoteEndpoint )
{
	// convert the message to an ofxOscMessage
	ofxOscMessage* ofMessage = new ofxOscMessage();

	// set the address
	ofMessage->setAddress( m.AddressPattern() );

	// set the sender ip/host
	char endpoint_host[ IpEndpointName::ADDRESS_STRING_LENGTH ];
	remoteEndpoint.AddressAsString( endpoint_host );
    ofMessage->setRemoteEndpoint( endpoint_host, remoteEndpoint.port );

	// transfer the arguments
	for ( osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin();
		  arg != m.ArgumentsEnd();
		  ++arg )
	{
		if ( arg->IsInt32() )
			ofMessage->addIntArg( arg->AsInt32Unchecked() );
		else if ( arg->IsFloat() )
			ofMessage->addFloatArg( arg->AsFloatUnchecked() );
		else if ( arg->IsString() )
			ofMessage->addStringArg( arg->AsStringUnchecked() );
		else if ( arg->IsBlob() )
		{
			osc::Blob blob;
			arg->AsBlobUnchecked( blob.data, blob.size );
			ofMessage->addBlobArg( blob );
		}
		else
		{
			assert( false && "message argument is not int, float, string, or blob" );
		}
	}

	// now add to the queue

	// at this point we are running inside the thread created by startThread,
	// so anyone who calls hasWaitingMessages() or getNextMessage() is coming
	// from a different thread

	// so we have to practise shared memory management

	// grab a lock on the queue
	grabMutex();

	// add incoming message on to the queue
	messages.push_back( ofMessage );

	// release the lock
	releaseMutex();
}
示例#8
0
static void UpdateEndpoint( IpEndpointName& dest, const IpEndpointName& src,
        const char *userName, const char *whichEndpoint )
{
    if( dest != src ){

		char endpointString[ IpEndpointName::ADDRESS_AND_PORT_STRING_LENGTH ];
		src.AddressAndPortAsString( endpointString );

        Log() << "updating " << whichEndpoint << " endpoint for user '"
                << userName << "' to " << endpointString << "." << std::endl;

        dest = src;
    }
}
	void Bind( const IpEndpointName& localEndpoint )
	{
		struct sockaddr_in bindSockAddr;
		SockaddrFromIpEndpointName( bindSockAddr, localEndpoint );

        if (localEndpoint.IsMulticastAddress()) {
            group.imr_interface.s_addr = htonl(INADDR_ANY);
            group.imr_multiaddr.s_addr = bindSockAddr.sin_addr.s_addr;
            if (setsockopt(socket_, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0) {
                throw std::runtime_error("error adding multicast group\n");
                close(socket_);
            }
        }
        
        if (bind(socket_, (struct sockaddr *)&bindSockAddr, sizeof(bindSockAddr)) < 0) {
            throw std::runtime_error("unable to bind udp socket\n");
        }

		isBound_ = true;
	}