<commit_before>/* 
 * Jamoma OSC Receiver
 * Copyright © 2011, <NAME>
 * 
 * License: This code is licensed under the terms of the "New BSD License"
 * http://creativecommons.org/licenses/BSD/
 */

#include "TTOscSocket.h"

TTPtr TTOscSocketListener(TTPtr anArgument)
{
	TTOscSocketPtr anOscSocket= (TTOscSocketPtr) anArgument;
	
	anOscSocket->mSocketListener = new UdpListeningReceiveSocket(IpEndpointName(IpEndpointName::ANY_ADDRESS, anOscSocket->mPort), anOscSocket);
	anOscSocket->mSocketListener->Run();
	
	return NULL;
}

TTOscSocket::TTOscSocket(const TTObjectPtr owner, const TTUInt16 port)
{
	mOwner = owner;
	mPort = port;
	
	mSocketListener = NULL;
	mSocketListenerThread = new TTThread(TTOscSocketListener, this);
	
	mSocketTransmitter = NULL;
}

TTOscSocket::TTOscSocket(const TTString& address, const TTUInt16 port)
{
	mAddress = address;
	mPort = port;
	
	mSocketTransmitter = new UdpTransmitSocket(IpEndpointName(address.data(), port));
	
	mSocketListener = NULL;
}

TTOscSocket::~TTOscSocket()
{
	unsigned int usecToStopTheSelect = 20000;
	
	if (mSocketListener) {
		
		mSocketListener->AsynchronousBreak();
		
#ifdef TT_PLATFORM_WIN
		Sleep(usecToStopTheSelect/1000);
#else
		usleep(usecToStopTheSelect);
#endif
		
		delete mSocketListener;
		mSocketListener = NULL;
	}
	
	if (mSocketTransmitter) {
		
		delete mSocketTransmitter;
		mSocketTransmitter = NULL;
	}
}

void TTOscSocket::ProcessMessage(const osc::ReceivedMessage&m, const IpEndpointName& remoteEndPoint)
{
	TTValue		receivedMessage = TT(m.AddressPattern());
	
	osc::ReceivedMessage::const_iterator arguments = m.ArgumentsBegin(); // get arguments
	
	while (arguments != m.ArgumentsEnd()) {
		
		if (arguments->IsChar())
			receivedMessage.append(arguments->AsChar());
		
		else if (arguments->IsInt32()) {
			TTInt32 i = arguments->AsInt32();
			receivedMessage.append((TTInt64)i);
			
		} else if (arguments->IsFloat())
			receivedMessage.append(arguments->AsFloat());
		
		else if (arguments->IsString())
			receivedMessage.append(TT(arguments->AsString()));
		
		arguments++;
	}
	
	this->mOwner->sendMessage(TT("oscSocketReceive"), receivedMessage, kTTValNONE);
}

TTErr TTOscSocket::SendMessage(TTSymbolPtr message, const TTValue& arguments)
{
	TTUInt32 bufferSize = computeMessageSize(message, arguments);
	
	if (!bufferSize)
		return kTTErrGeneric;
	
#ifdef TT_PLATFORM_WIN
	char* buffer = (char*)malloc(bufferSize);
#else
	char buffer[bufferSize];
#endif
	
	osc::OutboundPacketStream oscStream(buffer, bufferSize);
	
	oscStream << osc::BeginMessage(message->getCString());
	
	
	TTSymbolPtr		symValue;
	TTInt32			intValue;
	TTFloat64		floatValue;
	TTBoolean		booleanValue;
	TTDataType		valueType;
	
	for (TTUInt32 i=0; i<arguments.getSize(); ++i) {
		valueType = arguments.getType(i);
		
		if (valueType == kTypeSymbol) {
			arguments.get(i, &symValue);
			oscStream << symValue->getCString();
		}
		else if (valueType == kTypeBoolean) {
			arguments.get(i, booleanValue);
			oscStream << booleanValue;
		}
		else if (valueType == kTypeUInt8 || valueType == kTypeUInt16 || valueType == kTypeUInt32 || valueType == kTypeUInt64) {
			arguments.get(i, intValue);
			oscStream << intValue;
		}
		else if (valueType == kTypeInt8 || valueType == kTypeInt16 || valueType == kTypeInt32 || valueType == kTypeInt64) {
			arguments.get(i, intValue);
			oscStream << intValue;
		}
		else if (valueType == kTypeFloat32 || valueType == kTypeFloat64) {
			arguments.get(i, floatValue);
			oscStream << (float)floatValue;
		}
		else
			return kTTErrGeneric;
	}
	
	oscStream << osc::EndMessage;
	
	mSocketTransmitter->Send(oscStream.Data(), oscStream.Size());
	oscStream.Clear();
	
	return kTTErrNone;
}

TTUInt32 TTOscSocket::computeMessageSize(TTSymbolPtr message, const TTValue& arguments)
 {
	 TTUInt32 result = 0;
	 
	 result += 8;														//#bundle
	 result += 8;														//timetag
	 result += 4;														//datasize
	 
	 TTUInt32 messageSize = message->getCString().size();
	 messageSize += 1;													// /0 for end of string
	 
	 result += ((messageSize/4) + 1) * 4;
	 
	 TTUInt32 argumentSize = arguments.getSize();
	 argumentSize += 1;													// , for indicating this is an argument string information
	 
	 result += ((argumentSize/4) + 1) * 4;								// ArgumentTag Size
	 
	 for (TTUInt32 i=0; i<arguments.getSize(); ++i) {
		 
		 if (arguments.getType(i) == kTypeSymbol) {
			 
			 TTSymbolPtr symValue;
			 arguments.get(i, &symValue);
			 TTUInt32 stringSize = symValue->getCString().size();
			 stringSize += 1;											// /0 for end of string
			 result += ((stringSize/4) + 1) * 4;						// String Size
		 }
		 else if (arguments.getType(i) == kTypeBoolean) {
			 result += 1;												// Boolean size
		 }
		 else if (arguments.getType(i) == kTypeUInt8 || arguments.getType(i) == kTypeInt8) {
			 result += 2;												// Int8 size
		 }
		 else if (arguments.getType(i) == kTypeUInt16 || arguments.getType(i) == kTypeInt16) {
			 result += 4;												// Int16 size
		 }
		 else if (arguments.getType(i) == kTypeUInt32 || arguments.getType(i) == kTypeInt32 || arguments.getType(i) == kTypeFloat32) {
			 result += 4;												// Float32/Int32 size
		 }
		 else if (arguments.getType(i) == kTypeUInt64 || arguments.getType(i) == kTypeInt64 || arguments.getType(i) == kTypeFloat64) {
			 result += 8;												// Float64/Int64 size
		 }
		 else<commit_msg>NetworkLib compiling again. Closes #1117<commit_after>/* 
 * Jamoma OSC Receiver
 * Copyright © 2011, <NAME>
 * 
 * License: This code is licensed under the terms of the "New BSD License"
 * http://creativecommons.org/licenses/BSD/
 */

#include "TTOscSocket.h"

TTPtr TTOscSocketListener(TTPtr anArgument)
{
	TTOscSocketPtr anOscSocket= (TTOscSocketPtr) anArgument;
	
	anOscSocket->mSocketListener = new UdpListeningReceiveSocket(IpEndpointName(IpEndpointName::ANY_ADDRESS, anOscSocket->mPort), anOscSocket);
	anOscSocket->mSocketListener->Run();
	
	return NULL;
}

TTOscSocket::TTOscSocket(const TTObjectPtr owner, const TTUInt16 port)
{
	mOwner = owner;
	mPort = port;
	
	mSocketListener = NULL;
	mSocketListenerThread = new TTThread(TTOscSocketListener, this);
	
	mSocketTransmitter = NULL;
}

TTOscSocket::TTOscSocket(const TTString& address, const TTUInt16 port)
{
	mAddress = address;
	mPort = port;
	
	mSocketTransmitter = new UdpTransmitSocket(IpEndpointName(address.data(), port));
	
	mSocketListener = NULL;
}

TTOscSocket::~TTOscSocket()
{
	unsigned int usecToStopTheSelect = 20000;
	
	if (mSocketListener) {
		
		mSocketListener->AsynchronousBreak();
		
#ifdef TT_PLATFORM_WIN
		Sleep(usecToStopTheSelect/1000);
#else
		usleep(usecToStopTheSelect);
#endif
		
		delete mSocketListener;
		mSocketListener = NULL;
	}
	
	if (mSocketTransmitter) {
		
		delete mSocketTransmitter;
		mSocketTransmitter = NULL;
	}
}

void TTOscSocket::ProcessMessage(const osc::ReceivedMessage&m, const IpEndpointName& remoteEndPoint)
{
	TTValue		receivedMessage = TT(m.AddressPattern());
	
	osc::ReceivedMessage::const_iterator arguments = m.ArgumentsBegin(); // get arguments
	
	while (arguments != m.ArgumentsEnd()) {
		
		if (arguments->IsChar())
			receivedMessage.append(arguments->AsChar());
		
		else if (arguments->IsInt32()) {
			TTInt32 i = arguments->AsInt32();
			receivedMessage.append((TTInt64)i);
			
		} else if (arguments->IsFloat())
			receivedMessage.append(arguments->AsFloat());
		
		else if (arguments->IsString())
			receivedMessage.append(TT(arguments->AsString()));
		
		arguments++;
	}
	
	this->mOwner->sendMessage(TT("oscSocketReceive"), receivedMessage, kTTValNONE);
}

TTErr TTOscSocket::SendMessage(TTSymbolPtr message, const TTValue& arguments)
{
	TTUInt32 bufferSize = computeMessageSize(message, arguments);
	
	if (!bufferSize)
		return kTTErrGeneric;
	
#ifdef TT_PLATFORM_WIN
	char* buffer = (char*)malloc(bufferSize);
#else
	char buffer[bufferSize];
#endif
	
	osc::OutboundPacketStream oscStream(buffer, bufferSize);
	
	oscStream << osc::BeginMessage(message->getCString());
	
	
	TTSymbolPtr		symValue;
	TTInt32			intValue;
	TTFloat64		floatValue;
	TTBoolean		booleanValue;
	TTDataType		valueType;
	
	for (TTUInt32 i=0; i<arguments.getSize(); ++i) {
		valueType = arguments.getType(i);
		
		if (valueType == kTypeSymbol) {
			arguments.get(i, &symValue);
			oscStream << symValue->getCString();
		}
		else if (valueType == kTypeBoolean) {
			arguments.get(i, booleanValue);
			oscStream << booleanValue;
		}
		else if (valueType == kTypeUInt8 || valueType == kTypeUInt16 || valueType == kTypeUInt32 || valueType == kTypeUInt64) {
			arguments.get(i, intValue);
			oscStream << intValue;
		}
		else if (valueType == kTypeInt8 || valueType == kTypeInt16 || valueType == kTypeInt32 || valueType == kTypeInt64) {
			arguments.get(i, intValue);
			oscStream << intValue;
		}
		else if (valueType == kTypeFloat32 || valueType == kTypeFloat64) {
			arguments.get(i, floatValue);
			oscStream << (float)floatValue;
		}
		else
			return kTTErrGeneric;
	}
	
	oscStream << osc::EndMessage;
	
	mSocketTransmitter->Send(oscStream.Data(), oscStream.Size());
	oscStream.Clear();
	
	return kTTErrNone;
}

TTUInt32 TTOscSocket::computeMessageSize(TTSymbolPtr message, const TTValue& arguments)
 {
	 TTUInt32 result = 0;
	 
	 result += 8;														//#bundle
	 result += 8;														//timetag
	 result += 4;														//datasize
	 
	 TTUInt32 messageSize = string(message->getCString()).size();
	 messageSize += 1;													// /0 for end of string
	 
	 result += ((messageSize/4) + 1) * 4;
	 
	 TTUInt32 argumentSize = arguments.getSize();
	 argumentSize += 1;													// , for indicating this is an argument string information
	 
	 result += ((argumentSize/4) + 1) * 4;								// ArgumentTag Size
	 
	 for (TTUInt32 i=0; i<arguments.getSize(); ++i) {
		 
		 if (arguments.getType(i) == kTypeSymbol) {
			 
			 TTSymbolPtr symValue;
			 arguments.get(i, &symValue);
			 TTUInt32 stringSize = string(symValue->getCString()).size();
			 stringSize += 1;											// /0 for end of string
			 result += ((stringSize/4) + 1) * 4;						// String Size
		 }
		 else if (arguments.getType(i) == kTypeBoolean) {
			 result += 1;												// Boolean size
		 }
		 else if (arguments.getType(i) == kTypeUInt8 || arguments.getType(i) == kTypeInt8) {
			 result += 2;												// Int8 size
		 }
		 else if (arguments.getType(i) == kTypeUInt16 || arguments.getType(i) == kTypeInt16) {
			 result += 4;												// Int16 size
		 }
		 else if (arguments.getType(i) == kTypeUInt32 || arguments.getType(i) == kTypeInt32 || arguments.getType(i) == kTypeFloat32) {
			 result += 4;												// Float32/Int32 size
		 }
		 else if (arguments.getType(i) == kTypeUInt64 || arguments.getType(i) == kTypeInt64 || arguments.getType(i) == kTypeFloat64) {
			 result += 8;												// Float64/Int64 size
		 }
		 else
{'fraction_non_alphanumeric': 0.13040659442437713, 'fraction_numerical': 0.022043160137075114, 'mean_word_length': 3.76522506619594, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5516524', 'n_tokens_mistral': 4303, 'n_tokens_neox': 3487, 'n_words': 755}