<commit_before>#include "stdafx.h"
#include "LuaNode.h"
#include "luanode_net.h"
#include "luanode_net_acceptor.h"
#include "blogger.h"

#include <boost/asio/placeholders.hpp>

#include <boost/bind.hpp>

using namespace LuaNode::Net;

static unsigned long s_nextAcceptorId = 0;
static unsigned long s_acceptorCount = 0;

//////////////////////////////////////////////////////////////////////////
/// 
void LuaNode::Net::Acceptor::RegisterFunctions(lua_State* L) {}


const char* Acceptor::className = "Acceptor";
const Acceptor::RegType Acceptor::methods[] = {
	{"open", &Acceptor::Open},
	{"close", &Acceptor::Close},
	{"setoption", &Acceptor::SetOption},
	{"getsockname", &Acceptor::GetLocalAddress},
	{"bind", &Acceptor::Bind},
	{"listen", &Acceptor::Listen},
	{"accept", &Acceptor::Accept},
	{0}
};

const Acceptor::RegType Acceptor::setters[] = {
	{0}
};

const Acceptor::RegType Acceptor::getters[] = {
	{0}
};

Acceptor::Acceptor(lua_State* L) : 
	m_L(L),
	m_acceptorId(++s_nextAcceptorId),
	m_acceptor( GetIoService() )
{
	s_acceptorCount++;
	LogDebug("Constructing Acceptor (%p) (id=%d). Current acceptor count = %d", this, m_acceptorId, s_acceptorCount);
}

Acceptor::~Acceptor(void)
{
	s_acceptorCount--;
	LogDebug("Destructing Acceptor (%p) (id=%d). Current acceptor count = %d", this, m_acceptorId, s_acceptorCount);
}

//////////////////////////////////////////////////////////////////////////
/// 
/*static*/ int Acceptor::tostring_T(lua_State* L) {
	userdataType* ud = static_cast<userdataType*>(lua_touserdata(L, 1));
	Acceptor* obj = ud->pT;
	lua_pushfstring(L, "%s (%p) (id=%d)", className, obj, obj->m_acceptorId);
	return 1;
}

//////////////////////////////////////////////////////////////////////////
/// Open the acceptor using the specified protocol
int Acceptor::Open(lua_State* L) {
	const char* kind = luaL_checkstring(L, 2);
	LogDebug("Acceptor::Open (%p) (id=%d) - %s", this, m_acceptorId, kind);

	boost::system::error_code ec;
	if(strcmp(kind, "tcp4") == 0) {
		m_acceptor.open( boost::asio::ip::tcp::v4(), ec );
	}
	else if(strcmp(kind, "tcp6") == 0) {
		m_acceptor.open( boost::asio::ip::tcp::v6(), ec );
	}
	return BoostErrorCodeToLua(L, ec);
}

//////////////////////////////////////////////////////////////////////////
/// 
int Acceptor::Close(lua_State* L) {
	LogDebug("Acceptor::Close (%p) (id=%d)", this, m_acceptorId);
	boost::system::error_code ec;
	m_acceptor.close(ec);
	return BoostErrorCodeToLua(L, ec);
}

//////////////////////////////////////////////////////////////////////////
/// 
int Acceptor::SetOption(lua_State* L) {
	const char* option = luaL_checkstring(L, 2);
	LogDebug("Acceptor::SetOption (%p) (id=%d) - %s", this, m_acceptorId, option);

	if(strcmp(option, "reuseaddr") == 0) {
		bool value = lua_toboolean(L, 3) != 0;
		m_acceptor.set_option( boost::asio::socket_base::reuse_address(value) );
	}
	return 0;
}

//////////////////////////////////////////////////////////////////////////
/// 
int Acceptor::GetLocalAddress(lua_State* L) {
	const boost::asio::ip::tcp::endpoint& endpoint = m_acceptor.local_endpoint();

	lua_pushstring(L, endpoint.address().to_string().c_str());
	lua_pushinteger(L, endpoint.port());
	return 2;
}

//////////////////////////////////////////////////////////////////////////
/// 
int Acceptor::Bind(lua_State* L) {
	const char* ip = luaL_checkstring(L, 2);
	unsigned short port = luaL_checkinteger(L, 3);

	LogDebug("Acceptor::Bind (%p) (id=%d) - (%s,%d)", this, m_acceptorId, ip, port);

	boost::system::error_code ec;<commit_msg>Getting the bound address of an acceptor not yet bound was throwing an exception.<commit_after>#include "stdafx.h"
#include "LuaNode.h"
#include "luanode_net.h"
#include "luanode_net_acceptor.h"
#include "blogger.h"

#include <boost/asio/placeholders.hpp>

#include <boost/bind.hpp>

using namespace LuaNode::Net;

static unsigned long s_nextAcceptorId = 0;
static unsigned long s_acceptorCount = 0;

//////////////////////////////////////////////////////////////////////////
/// 
void LuaNode::Net::Acceptor::RegisterFunctions(lua_State* L) {}


const char* Acceptor::className = "Acceptor";
const Acceptor::RegType Acceptor::methods[] = {
	{"open", &Acceptor::Open},
	{"close", &Acceptor::Close},
	{"setoption", &Acceptor::SetOption},
	{"getsockname", &Acceptor::GetLocalAddress},
	{"bind", &Acceptor::Bind},
	{"listen", &Acceptor::Listen},
	{"accept", &Acceptor::Accept},
	{0}
};

const Acceptor::RegType Acceptor::setters[] = {
	{0}
};

const Acceptor::RegType Acceptor::getters[] = {
	{0}
};

Acceptor::Acceptor(lua_State* L) : 
	m_L(L),
	m_acceptorId(++s_nextAcceptorId),
	m_acceptor( GetIoService() )
{
	s_acceptorCount++;
	LogDebug("Constructing Acceptor (%p) (id=%d). Current acceptor count = %d", this, m_acceptorId, s_acceptorCount);
}

Acceptor::~Acceptor(void)
{
	s_acceptorCount--;
	LogDebug("Destructing Acceptor (%p) (id=%d). Current acceptor count = %d", this, m_acceptorId, s_acceptorCount);
}

//////////////////////////////////////////////////////////////////////////
/// 
/*static*/ int Acceptor::tostring_T(lua_State* L) {
	userdataType* ud = static_cast<userdataType*>(lua_touserdata(L, 1));
	Acceptor* obj = ud->pT;
	lua_pushfstring(L, "%s (%p) (id=%d)", className, obj, obj->m_acceptorId);
	return 1;
}

//////////////////////////////////////////////////////////////////////////
/// Open the acceptor using the specified protocol
int Acceptor::Open(lua_State* L) {
	const char* kind = luaL_checkstring(L, 2);
	LogDebug("Acceptor::Open (%p) (id=%d) - %s", this, m_acceptorId, kind);

	boost::system::error_code ec;
	if(strcmp(kind, "tcp4") == 0) {
		m_acceptor.open( boost::asio::ip::tcp::v4(), ec );
	}
	else if(strcmp(kind, "tcp6") == 0) {
		m_acceptor.open( boost::asio::ip::tcp::v6(), ec );
	}
	return BoostErrorCodeToLua(L, ec);
}

//////////////////////////////////////////////////////////////////////////
/// 
int Acceptor::Close(lua_State* L) {
	LogDebug("Acceptor::Close (%p) (id=%d)", this, m_acceptorId);
	boost::system::error_code ec;
	m_acceptor.close(ec);
	return BoostErrorCodeToLua(L, ec);
}

//////////////////////////////////////////////////////////////////////////
/// 
int Acceptor::SetOption(lua_State* L) {
	const char* option = luaL_checkstring(L, 2);
	LogDebug("Acceptor::SetOption (%p) (id=%d) - %s", this, m_acceptorId, option);

	if(strcmp(option, "reuseaddr") == 0) {
		bool value = lua_toboolean(L, 3) != 0;
		m_acceptor.set_option( boost::asio::socket_base::reuse_address(value) );
	}
	return 0;
}

//////////////////////////////////////////////////////////////////////////
/// 
int Acceptor::GetLocalAddress(lua_State* L) {
	boost::system::error_code ec;
	const boost::asio::ip::tcp::endpoint& endpoint = m_acceptor.local_endpoint(ec);

	if(!ec) {
		lua_pushstring(L, endpoint.address().to_string().c_str());
		lua_pushinteger(L, endpoint.port());
		return 2;
	}
	return luaL_error(L, "Acceptor::GetLocalAddress - %s:%d", ec.message().c_str(), ec.value());
}

//////////////////////////////////////////////////////////////////////////
/// 
int Acceptor::Bind(lua_State* L) {
	const char* ip = luaL_checkstring(L, 2);
	unsigned short port = luaL_checkinteger(L, 3);

	LogDebug("Acceptor::Bind (%p) (id=%d) - (%s,%d)", this, m_acceptorId, ip, port);

	boost::system::error_code ec;
{'fraction_non_alphanumeric': 0.3059536934950386, 'fraction_numerical': 0.006063947078280044, 'mean_word_length': 2.7484504132231407, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '4200572', 'n_tokens_mistral': 2510, 'n_tokens_neox': 2432, 'n_words': 477}