103 lines
2.4 KiB
C++
Raw Normal View History

#include "interface/module.h"
#include "interface/server.h"
#include "interface/event.h"
#include "interface/tcpsocket.h"
2014-09-17 16:43:05 +03:00
#include "network/include/api.h"
#include <iostream>
using interface::Event;
namespace network {
2014-09-17 16:43:05 +03:00
struct Peer
{
typedef size_t Id;
Id id = 0;
sp_<interface::TCPSocket> socket;
Peer(){}
Peer(Id id, sp_<interface::TCPSocket> socket):
id(id), socket(socket){}
};
struct Module: public interface::Module
{
interface::Server *m_server;
2014-09-17 16:43:05 +03:00
sp_<interface::TCPSocket> m_listening_socket;
sm_<Peer::Id, Peer> m_peers;
size_t m_next_peer_id = 1;
Module(interface::Server *server):
m_server(server),
2014-09-17 16:43:05 +03:00
m_listening_socket(interface::createTCPSocket())
{
std::cout<<"network construct"<<std::endl;
}
void init()
{
std::cout<<"network init"<<std::endl;
2014-09-17 16:43:05 +03:00
m_server->sub_event(this, Event::t("core:start"));
m_server->sub_event(this, Event::t("network:send"));
2014-09-17 18:26:58 +03:00
m_server->sub_event(this, Event::t("network:listen_event"));
}
~Module()
{
std::cout<<"network destruct"<<std::endl;
}
2014-09-17 18:52:59 +03:00
void event(const Event::Type &type, const Event::Private *p)
{
2014-09-17 20:15:46 +03:00
EVENT_VOIDN("core:start", on_start)
EVENT_TYPEN("network:send", on_send_packet, Packet)
EVENT_TYPEN("network:listen_event", on_listen_event, interface::SocketEvent)
}
2014-09-17 16:43:05 +03:00
void on_start()
{
2014-09-17 13:37:34 +03:00
ss_ address = "any4";
ss_ port = "20000";
2014-09-17 16:43:05 +03:00
if(!m_listening_socket->bind_fd(address, port) ||
2014-09-17 18:27:30 +03:00
!m_listening_socket->listen_fd()){
std::cerr<<"Failed to bind to "<<address<<":"<<port<<std::endl;
} else {
std::cerr<<"Listening at "<<address<<":"<<port<<std::endl;
}
2014-09-17 16:43:05 +03:00
m_server->add_socket_event(m_listening_socket->fd(),
2014-09-17 18:27:30 +03:00
Event::t("network:listen_event"));
2014-09-17 16:43:05 +03:00
}
void on_send_packet(const Packet &packet)
{
// TODO
}
2014-09-17 18:26:58 +03:00
void on_listen_event(const interface::SocketEvent &event)
2014-09-17 16:43:05 +03:00
{
2014-09-17 18:26:58 +03:00
std::cerr<<"network: on_listen_event(): fd="<<event.fd<<std::endl;
2014-09-17 20:15:46 +03:00
// Create socket
2014-09-17 18:26:58 +03:00
sp_<interface::TCPSocket> socket(interface::createTCPSocket());
2014-09-17 20:15:46 +03:00
// Accept connection
2014-09-17 18:26:58 +03:00
socket->accept_fd(*m_listening_socket.get());
2014-09-17 20:15:46 +03:00
// Store socket
2014-09-17 18:26:58 +03:00
Peer::Id peer_id = m_next_peer_id++;
m_peers[peer_id] = Peer(peer_id, socket);
2014-09-17 20:15:46 +03:00
// Emit event
PeerInfo pinfo;
pinfo.id = peer_id;
pinfo.address = socket->get_remote_address();
m_server->emit_event("network:new_client", new NewClient(pinfo));
}
};
extern "C" {
EXPORT void* createModule_network(interface::Server *server){
return (void*)(new Module(server));
}
}
}