#ifndef __EVIO_HPP_
#define __EVIO_HPP_

#include <string>
#include <vector>
#include <memory>

struct client:std::enable_shared_from_this<client>
{
	virtual std::string name()=0;
	virtual int type()=0;			//分站:0,WEB:1
	virtual int handle()=0;

	virtual void close()=0;
	virtual void send(std::vector<char>&&b)=0;

	virtual ~client(){}
};

struct service_callback;
struct service_handle
{
	virtual int run(int port)=0;
	virtual void stop()=0;
	virtual void boardcast(const std::vector<char>&msg)=0;
	virtual ~service_handle(){}

	static service_handle*instance(service_callback*sc);
};

struct service_callback:service_handle
{
	service_handle*m_handle{nullptr};

	void set_handle(service_handle*handle)
	{
		m_handle=handle;
	}

	int run(int port)
	{
		return m_handle->run(port);
	}

	void stop()
	{
		m_handle->stop();
	}

	void boardcast(const std::vector<char>&msg)
	{
		m_handle->boardcast(msg);
	}

	virtual void on_connect(std::shared_ptr<client> clt)
	{
	}

	virtual void on_close(std::shared_ptr<client> clt)
	{
		log_info("client(%s) closed",clt->name().c_str());
	}

	virtual void on_error(std::shared_ptr<client> clt,const char*error)
	{
		log_info("client(%s) error with:%s",clt->name().c_str(),error);
	}

	virtual void on_send_timeout(std::shared_ptr<client> clt)
	{
		log_info("client(%s) send time out",clt->name().c_str());
	}

	virtual void on_recv_timeout(std::shared_ptr<client> clt)
	{
		log_info("client(%s) recv time out",clt->name().c_str());
	}

	virtual void on_message(std::shared_ptr<client> clt,const char*data,size_t len)=0;
	virtual void on_timer()=0;

	virtual ~service_callback(){}
};

#endif

