<commit_before>#pragma once

#include <iostream>
#include <functional>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <memory>
#include <thread>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>

namespace asio = boost::asio;
using asio::ip::tcp;

#define DEBUG
#ifdef DEBUG
#define PEREVOD_DEBUG_LOG(x) std::cout << x << std::endl;
#define PEREVOD_DEBUG_PRETTY_LOG(x) std::cout << __PRETTY_FUNCTION__ << " " << x << std::endl;
#endif

namespace Perevod
{
	class ImageFrame
	{
	protected:
		uint32_t x, y, width, height;
		std::vector<unsigned char> data;
	public:
		
		ImageFrame(uint32_t x, uint32_t y, uint32_t width, uint32_t height, unsigned char *data) {
			this->x = x;
			this->y = y;
			this->width = width;
			this->height = height;
			int image_data_size = width * height * 3;
			this->data.resize(image_data_size);
			std::copy(data, data + image_data_size, this->data.begin());
		};

		~ImageFrame() {};

		uint32_t position_x() {
			return this->x;
		}

		uint32_t position_y() {
			return this->y;
		}

		unsigned char* image_data() {
			std::string data_str(this->data.begin(), this->data.end());
			return (unsigned char *)data_str.c_str();
		}

		int image_width() {
			return this->width;
		}

		int image_height() {
			return this->height;
		}
		
		void read_raw_byte(unsigned char *frame_data) {
			int image_data_size = width * height * 3;
			std::memcpy(frame_data, &this->x, sizeof(uint32_t));
			std::memcpy(frame_data + sizeof(uint32_t), &this->y, sizeof(uint32_t));
			std::memcpy(frame_data + sizeof(uint32_t) * 2, &this->width, sizeof(uint32_t));
			std::memcpy(frame_data + sizeof(uint32_t) * 3, &this->height, sizeof(uint32_t));
			std::memcpy(frame_data + sizeof(uint32_t) * 4, this->image_data(), image_data_size);
		}

		int size() {
			return this->data.size();
		}

		int frame_size() {
			return this->size() + sizeof(uint32_t) * 4;
		}
	};

	template <typename T> class Queue
	{
		std::queue<T> queue;
		mutable std::mutex mutex;
		std::condition_variable	cond;
	public:
		Queue() {};
		~Queue() {};

		void push(T data) {
			std::unique_lock<std::mutex> lock(this->mutex);
			this->queue.push(data);
			lock.unlock();
			this->cond.notify_one();
		}

		T pop() {
			std::unique_lock<std::mutex> lock(this->mutex);
			while (this->queue.empty()) {
				this->cond.wait(lock);
		    }
			auto i = this->queue.front();
			this->queue.pop();

			return i;
		}

		T try_pop() {
			std::unique_lock<std::mutex> lock(this->mutex);
			if (this->queue.empty()) {
				return nullptr;
			}
			auto i = this->queue.front();
			this->queue.pop();
			return i;
		}

		bool empty() const {
			std::unique_lock<std::mutex> lock(this->mutex);
			return this->queue.empty();
		}
	};

	class ImageSocketImpl
	{
	public:
		ImageSocketImpl() {};
		~ImageSocketImpl() {};
	};

	class ImageSocketUDPImpl : public ImageSocketImpl
	{
	public:
		ImageSocketUDPImpl() {};
		~ImageSocketUDPImpl() {};
		
		void send_frame(std::shared_ptr<Perevod::ImageFrame>frame) {}

		std::shared_ptr<Perevod::ImageFrame> read_frame() {
			return nullptr;
		}
	};

	class ImageSocketTCPImpl : public ImageSocketImpl
	{
		asio::io_service send_io_service;
		tcp::socket send_socket;

		asio::io_service receive_io_service;
		tcp::socket receive_socket;
		asio::streambuf receive_buffer;
		tcp::acceptor acceptor;
		std::string ip_address;
		int send_port;
		int receive_port;
	public:
		Queue<std::shared_ptr<Perevod::ImageFrame>> send_queue;
		Queue<std::shared_ptr<Perevod::ImageFrame>> received_queue;

		ImageSocketTCPImpl(std::string ip_address, int send_port, int receive_port) : send_socket(this->send_io_service), receive_socket(this->receive_io_service), acceptor(this->receive_io_service, tcp::endpoint(tcp::v4(), receive_port)) {
			this->ip_address = ip_address;
			this->send_port = send_port;
			this->receive_port = receive_port;
			asio::socket_base::reuse_address option(true);
			this->acceptor.set_option(option);
			asio::socket_base::keep_alive keep_alive(true);<commit_msg>use ducktyping<commit_after>#pragma once

#include <iostream>
#include <functional>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <memory>
#include <thread>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>

namespace asio = boost::asio;
using asio::ip::tcp;

#define DEBUG
#ifdef DEBUG
#define PEREVOD_DEBUG_LOG(x) std::cout << x << std::endl;
#define PEREVOD_DEBUG_PRETTY_LOG(x) std::cout << __PRETTY_FUNCTION__ << " " << x << std::endl;
#endif

namespace Perevod
{
	class ImageFrame
	{
	protected:
		uint32_t x, y, width, height;
		std::vector<unsigned char> data;
	public:
		
		ImageFrame(uint32_t x, uint32_t y, uint32_t width, uint32_t height, unsigned char *data) {
			this->x = x;
			this->y = y;
			this->width = width;
			this->height = height;
			int image_data_size = width * height * 3;
			this->data.resize(image_data_size);
			std::copy(data, data + image_data_size, this->data.begin());
		};

		~ImageFrame() {};

		uint32_t position_x() {
			return this->x;
		}

		uint32_t position_y() {
			return this->y;
		}

		unsigned char* image_data() {
			std::string data_str(this->data.begin(), this->data.end());
			return (unsigned char *)data_str.c_str();
		}

		int image_width() {
			return this->width;
		}

		int image_height() {
			return this->height;
		}
		
		void read_raw_byte(unsigned char *frame_data) {
			int image_data_size = width * height * 3;
			std::memcpy(frame_data, &this->x, sizeof(uint32_t));
			std::memcpy(frame_data + sizeof(uint32_t), &this->y, sizeof(uint32_t));
			std::memcpy(frame_data + sizeof(uint32_t) * 2, &this->width, sizeof(uint32_t));
			std::memcpy(frame_data + sizeof(uint32_t) * 3, &this->height, sizeof(uint32_t));
			std::memcpy(frame_data + sizeof(uint32_t) * 4, this->image_data(), image_data_size);
		}

		int size() {
			return this->data.size();
		}

		int frame_size() {
			return this->size() + sizeof(uint32_t) * 4;
		}
	};

	template <typename T> class Queue
	{
		std::queue<T> queue;
		mutable std::mutex mutex;
		std::condition_variable	cond;
	public:
		Queue() {};
		~Queue() {};

		void push(T data) {
			std::unique_lock<std::mutex> lock(this->mutex);
			this->queue.push(data);
			lock.unlock();
			this->cond.notify_one();
		}

		T pop() {
			std::unique_lock<std::mutex> lock(this->mutex);
			while (this->queue.empty()) {
				this->cond.wait(lock);
		    }
			auto i = this->queue.front();
			this->queue.pop();

			return i;
		}

		T try_pop() {
			std::unique_lock<std::mutex> lock(this->mutex);
			if (this->queue.empty()) {
				return nullptr;
			}
			auto i = this->queue.front();
			this->queue.pop();
			return i;
		}

		bool empty() const {
			std::unique_lock<std::mutex> lock(this->mutex);
			return this->queue.empty();
		}
	};

	class ImageSocketTCPImpl
	{
		asio::io_service send_io_service;
		tcp::socket send_socket;

		asio::io_service receive_io_service;
		tcp::socket receive_socket;
		asio::streambuf receive_buffer;
		tcp::acceptor acceptor;
		std::string ip_address;
		int send_port;
		int receive_port;
	public:
		Queue<std::shared_ptr<Perevod::ImageFrame>> send_queue;
		Queue<std::shared_ptr<Perevod::ImageFrame>> received_queue;

		ImageSocketTCPImpl(std::string ip_address, int send_port, int receive_port) : send_socket(this->send_io_service), receive_socket(this->receive_io_service), acceptor(this->receive_io_service, tcp::endpoint(tcp::v4(), receive_port)) {
			this->ip_address = ip_address;
			this->send_port = send_port;
			this->receive_port = receive_port;
			asio::socket_base::reuse_address option(true);
			this->acceptor.set_option(option);
			asio::socket_base::keep_alive keep_alive(true);
{'fraction_non_alphanumeric': 0.15834522111269614, 'fraction_numerical': 0.010115419530540785, 'mean_word_length': 4.461756373937677, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 127, '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': '5636506', 'n_tokens_mistral': 3172, 'n_tokens_neox': 2766, 'n_words': 536}