<commit_before>#include <cstring>	/// for memset

#include "zlog/zlog.hpp"

#include "exemodel/wsserveree.hpp"

namespace exemodel {

class wsee final : public pollee {
public:
	wsee() = default;
	~wsee() = default;

	int init(struct lws * wsi, struct lws_context * context, int fd, uint32_t evts)
	{
		savefd(fd);
		saveevts(evts);
		m_wsi = wsi;
		m_context = context;
		return 0;
	}

	virtual int dispose(poller & /*mgr*/, uint32_t evts)
	{
		struct pollfd i = {
			this->fd(),
			(short)this->evts(),
			(short)evts
		};
		lws_service_fd(m_context, &i);
		return 0;
	}

	int sendTextMessage(void *buf, size_t len)
	{
		return lws_write(m_wsi, (unsigned char *)buf, len, LWS_WRITE_TEXT);
	}

	int sendBinaryMessage(void *buf, size_t len)
	{
		return lws_write(m_wsi, (unsigned char *)buf, len, LWS_WRITE_BINARY);
	}
private:
	struct lws * m_wsi;
	struct lws_context * m_context;
};

int wsserveree::callback_http(
	struct lws *wsi,
	enum lws_callback_reasons reason,
	void */*user*/,
	void *in,
	size_t len)
{
	struct lws_context * context = lws_get_context(wsi);
	wsserveree * svr = (wsserveree *)lws_context_user(context);

	int ret = 0;
	switch (reason) {
	case LWS_CALLBACK_ESTABLISHED:
		ret = svr->__receiveConnectState(wsi, true);
		break;

	case LWS_CALLBACK_CLOSED:
		ret = svr->__receiveConnectState(wsi, false);
		break;

	case LWS_CALLBACK_ADD_POLL_FD: {
			struct lws_pollargs & pa = *(struct lws_pollargs *)in;
			//printf("wsi(%p) fd(%d)\n", (void*)wsi, pa.fd);
			zlog_debug("add poll fd: %#x %d", (unsigned long)wsi, pa.fd);
			svr->__addSp(wsi, context, pa.fd, pa.events);
		}
		break;

	case LWS_CALLBACK_DEL_POLL_FD: {
			struct lws_pollargs & pa = *(struct lws_pollargs *)in;
			zlog_debug("del poll fd: %#x %d", (unsigned long)wsi, pa.fd);
			svr->__delSp(wsi, pa.fd);
		}
		break;

	case LWS_CALLBACK_CHANGE_MODE_POLL_FD: {
			struct lws_pollargs & pa = *(struct lws_pollargs *)in;
			svr->__modSp(wsi, pa.fd, pa.events);
		}
		break;

	case LWS_CALLBACK_RECEIVE: {
			const size_t remaining = lws_remaining_packet_payload(wsi);
			const bool is_final = lws_is_final_fragment(wsi);
			if (remaining == 0 && is_final) {
				//char namebuf[200];
				//printf("receive from %s\n", lws_get_peer_simple(wsi, namebuf, sizeof(namebuf)));
				const bool is_binary = lws_frame_is_binary(wsi);
				if (is_binary) {
					svr->__receiveBinaryMessage(wsi, in, len);
				}
				else {
					svr->__receiveTextMessage(wsi, in, len);<commit_msg>fix string format for zlog call<commit_after>#include <cstring>	/// for memset

#include "zlog/zlog.hpp"

#include "exemodel/wsserveree.hpp"

namespace exemodel {

class wsee final : public pollee {
public:
	wsee() = default;
	~wsee() = default;

	int init(struct lws * wsi, struct lws_context * context, int fd, uint32_t evts)
	{
		savefd(fd);
		saveevts(evts);
		m_wsi = wsi;
		m_context = context;
		return 0;
	}

	virtual int dispose(poller & /*mgr*/, uint32_t evts)
	{
		struct pollfd i = {
			this->fd(),
			(short)this->evts(),
			(short)evts
		};
		lws_service_fd(m_context, &i);
		return 0;
	}

	int sendTextMessage(void *buf, size_t len)
	{
		return lws_write(m_wsi, (unsigned char *)buf, len, LWS_WRITE_TEXT);
	}

	int sendBinaryMessage(void *buf, size_t len)
	{
		return lws_write(m_wsi, (unsigned char *)buf, len, LWS_WRITE_BINARY);
	}
private:
	struct lws * m_wsi;
	struct lws_context * m_context;
};

int wsserveree::callback_http(
	struct lws *wsi,
	enum lws_callback_reasons reason,
	void */*user*/,
	void *in,
	size_t len)
{
	struct lws_context * context = lws_get_context(wsi);
	wsserveree * svr = (wsserveree *)lws_context_user(context);

	int ret = 0;
	switch (reason) {
	case LWS_CALLBACK_ESTABLISHED:
		ret = svr->__receiveConnectState(wsi, true);
		break;

	case LWS_CALLBACK_CLOSED:
		ret = svr->__receiveConnectState(wsi, false);
		break;

	case LWS_CALLBACK_ADD_POLL_FD: {
			struct lws_pollargs & pa = *(struct lws_pollargs *)in;
			//printf("wsi(%p) fd(%d)\n", (void*)wsi, pa.fd);
			zlog_debug("add poll fd: %#lx %d", (unsigned long)wsi, pa.fd);
			svr->__addSp(wsi, context, pa.fd, pa.events);
		}
		break;

	case LWS_CALLBACK_DEL_POLL_FD: {
			struct lws_pollargs & pa = *(struct lws_pollargs *)in;
			zlog_debug("del poll fd: %#lx %d", (unsigned long)wsi, pa.fd);
			svr->__delSp(wsi, pa.fd);
		}
		break;

	case LWS_CALLBACK_CHANGE_MODE_POLL_FD: {
			struct lws_pollargs & pa = *(struct lws_pollargs *)in;
			svr->__modSp(wsi, pa.fd, pa.events);
		}
		break;

	case LWS_CALLBACK_RECEIVE: {
			const size_t remaining = lws_remaining_packet_payload(wsi);
			const bool is_final = lws_is_final_fragment(wsi);
			if (remaining == 0 && is_final) {
				//char namebuf[200];
				//printf("receive from %s\n", lws_get_peer_simple(wsi, namebuf, sizeof(namebuf)));
				const bool is_binary = lws_frame_is_binary(wsi);
				if (is_binary) {
					svr->__receiveBinaryMessage(wsi, in, len);
				}
				else {
					svr->__receiveTextMessage(wsi, in, len);
{'fraction_non_alphanumeric': 0.13926106374340236, 'fraction_numerical': 0.004466098254161592, 'mean_word_length': 4.164570230607967, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '213675', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2032, 'n_words': 436}