<commit_before>/*
	This file is part of the E_Util library.
	Copyright (C) 2009-2012 <NAME> <<EMAIL>>
	Copyright (C) 2009-2012 <NAME> <<EMAIL>>
	Copyright (C) 2009-2012 <NAME> <<EMAIL>>
	
	This library is subject to the terms of the Mozilla Public License, v. 2.0.
	You should have received a copy of the MPL along with this library; see the 
	file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "E_NetworkTCP.h"

#include <EScript/EScript.h>


using namespace EScript;
using namespace Util::Network;

namespace E_Util {
namespace Network {

//! (static)
EScript::Type * E_TCPConnection::getTypeObject() {
	// E_TCPConnection ---|> Object
	static EScript::ERef<EScript::Type> typeObject = new EScript::Type(EScript::Object::getTypeObject());
	return typeObject.get();
}

//! [E_TCPConnection] initMembers
void E_TCPConnection::init(EScript::Namespace & lib) {
	EScript::Type * typeObject = getTypeObject();
	declareConstant(&lib,getClassName(),typeObject);

	//! [ESF] (static) TCPConnection|false TCPConnection.connect(host,port)
	ES_FUNCTION(typeObject,"connect",2,2,{
		Util::Reference<TCPConnection> s=TCPConnection::connect(IPv4Address::resolveHost(parameter[0].toString(), parameter[1].toInt()));
		if (s.isNull()) {
			return Bool::create(false);
		} else {
			return new E_TCPConnection(s);
		}
	})
	// ----

	//! [ESMF] thisObj TCPConnection.close()
	ES_MFUN(typeObject,E_TCPConnection,"close",0,0,((**thisObj)->close(),thisEObj))

	//! [ESMF] thisObj TCPConnection.isOpen()
	ES_MFUN(typeObject,E_TCPConnection,"isOpen",0,0,(**thisObj)->isOpen())

	//! [ESMF] String|false TCPConnection.receiveString([delimiter='\0'])
	ES_MFUNCTION(typeObject,E_TCPConnection,"receiveString",0,1,{
		std::string s=(**thisObj)->receiveString(parameter[0].toString("\0").c_str()[0]);
		if (s.empty()) {
			return EScript::create(false);
		} else {
			return EScript::create(s);
		}
	})

	//! [ESMF] thisObj TCPConnection.sendString(String,[delimiter='\0'])
	ES_MFUN(typeObject,E_TCPConnection,"sendString",1,2,
				((**thisObj)->sendString(parameter[0].toString()+parameter[1].toString("\0").c_str()[0]),thisEObj))
}

//---

// ------------------------------------------------------------------------------

//! (static)
EScript::Type * E_TCPServer::getTypeObject() {
	// E_TCPServer ---|> Object
	static EScript::ERef<EScript::Type> typeObject = new EScript::Type(EScript::Object::getTypeObject());
	return typeObject.get();
}

//! [E_TCPServer] initMembers
void E_TCPServer::init(EScript::Namespace & lib) {

	EScript::Type * typeObject = getTypeObject();
	declareConstant(&lib,getClassName(),typeObject);

	//! [ESF] (static) TCPServer|false TCPServer.create(port)
	ES_FUNCTION(typeObject,"create",1,1,{
		TCPServer * s=TCPServer::create(parameter[0].toInt());
		if (s == nullptr) {
			return Bool::create(false);
		} else {
			return new E_TCPServer(s);
		}
	})<commit_msg>ignore delimiter when exlicitly passing empty string as delimiter in TCPConnection.sendString<commit_after>/*
	This file is part of the E_Util library.
	Copyright (C) 2009-2012 <NAME> <<EMAIL>>
	Copyright (C) 2009-2012 <NAME> <<EMAIL>>
	Copyright (C) 2009-2012 <NAME> <<EMAIL>>
	
	This library is subject to the terms of the Mozilla Public License, v. 2.0.
	You should have received a copy of the MPL along with this library; see the 
	file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "E_NetworkTCP.h"

#include <EScript/EScript.h>


using namespace EScript;
using namespace Util::Network;

namespace E_Util {
namespace Network {

//! (static)
EScript::Type * E_TCPConnection::getTypeObject() {
	// E_TCPConnection ---|> Object
	static EScript::ERef<EScript::Type> typeObject = new EScript::Type(EScript::Object::getTypeObject());
	return typeObject.get();
}

//! [E_TCPConnection] initMembers
void E_TCPConnection::init(EScript::Namespace & lib) {
	EScript::Type * typeObject = getTypeObject();
	declareConstant(&lib,getClassName(),typeObject);

	//! [ESF] (static) TCPConnection|false TCPConnection.connect(host,port)
	ES_FUNCTION(typeObject,"connect",2,2,{
		Util::Reference<TCPConnection> s=TCPConnection::connect(IPv4Address::resolveHost(parameter[0].toString(), parameter[1].toInt()));
		if (s.isNull()) {
			return Bool::create(false);
		} else {
			return new E_TCPConnection(s);
		}
	})
	// ----

	//! [ESMF] thisObj TCPConnection.close()
	ES_MFUN(typeObject,E_TCPConnection,"close",0,0,((**thisObj)->close(),thisEObj))

	//! [ESMF] thisObj TCPConnection.isOpen()
	ES_MFUN(typeObject,E_TCPConnection,"isOpen",0,0,(**thisObj)->isOpen())

	//! [ESMF] String|false TCPConnection.receiveString([delimiter='\0'|length])
	ES_MFUNCTION(typeObject,E_TCPConnection,"receiveString",0,1,{
		EScript::Number* len = parameter[0].toType<EScript::Number>();
		std::string s;
		if(len) {
			const std::vector<uint8_t> d = (**thisObj)->receiveData(len->toUInt());
			if(!d.empty())
				s.assign(d.begin(), d.end());
		} else {
			s=(**thisObj)->receiveString(parameter[0].toString("\0").c_str()[0]);
		}
		if (s.empty()) {
			return EScript::create(false);
		} else {
			return EScript::create(s);
		}
	})

	//! [ESMF] thisObj TCPConnection.sendString(String,[delimiter='\0'])
	ES_MFUN(typeObject,E_TCPConnection,"sendString",1,2,
				((**thisObj)->sendString((parameter[1].isNotNull() && parameter[1].toString().empty()) ? parameter[0].toString() : parameter[0].toString()+parameter[1].toString("\0").c_str()[0]),thisEObj))
}

//---

// ------------------------------------------------------------------------------

//! (static)
EScript::Type * E_TCPServer::getTypeObject() {
	// E_TCPServer ---|> Object
	static EScript::ERef<EScript::Type> typeObject = new EScript::Type(EScript::Object::getTypeObject());
	return typeObject.get();
}

//! [E_TCPServer] initMembers
void E_TCPServer::init(EScript::Namespace & lib) {

	EScript::Type * typeObject = getTypeObject();
	declareConstant(&lib,getClassName(),typeObject);

	//! [ESF] (static) TCPServer|false TCPServer.create(port)
	ES_FUNCTION(typeObject,"create",1,1,{
		TCPServer * s=TCPServer::create(parameter[0].toInt());
		if (s == nullptr) {
			return Bool::create(false);
		} else {
			return new E_TCPServer(s);
		}
	})
{'fraction_non_alphanumeric': 0.20341715022566087, 'fraction_numerical': 0.017891682785299807, 'mean_word_length': 4.815370196813496, 'pattern_counts': {'":': 0, '<': 31, '<?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': '1843619', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2054, 'n_words': 380}