63 lines
1.2 KiB
C++

#include "interface/module.h"
#include "interface/server.h"
#include "interface/event.h"
#include "interface/tcpsocket.h"
//#include "network/include/api.h"
#include <iostream>
using interface::Event;
namespace network {
struct Module: public interface::Module
{
interface::Server *m_server;
sp_<interface::TCPSocket> m_socket;
Module(interface::Server *server):
m_server(server),
m_socket(interface::createTCPSocket())
{
std::cout<<"network construct"<<std::endl;
}
void init()
{
std::cout<<"network init"<<std::endl;
m_server->sub_event(this, interface::Event::t("core:start"));
}
~Module()
{
std::cout<<"network destruct"<<std::endl;
}
void event(const interface::Event &event)
{
if(event.type == interface::Event::t("core:start")){
start();
}
}
void start()
{
ss_ address = "any4";
ss_ port = "20000";
if(!m_socket->bind_fd(address, port) ||
!m_socket->listen_fd()){
std::cerr<<"Failed to bind to "<<address<<":"<<port<<std::endl;
} else {
std::cerr<<"Listening at "<<address<<":"<<port<<std::endl;
}
//m_server->add_socket(m_socket);
}
};
extern "C" {
EXPORT void* createModule_network(interface::Server *server){
return (void*)(new Module(server));
}
}
}