00001 #ifndef N2NC_SERVER_H
00002 #define N2NC_SERVER_H
00003
00004 #include "nixsys.h"
00005 #include "socket.h"
00006 #include "tcpsocket.h"
00007 #include "resolver.h"
00008 #include "socketevents.h"
00009 #include "socketeventshandler.h"
00010 #include "clientinfo.h"
00011 #include "mutex.h"
00012 #include "socketaddress.h"
00013
00014 using namespace n2nc ;
00015 namespace n2nc {
00016
00020 class Server : n2nc::net::SocketEvents{
00021 public:
00022 Server();
00023 ~Server();
00025 int listen(int port);
00027 int shutdown(std::string message);
00028
00030 int check_clients_status();
00031
00032 int show_clients();
00033
00034 enum server_command{
00035 CMD_REGISTER,
00036 CMD_REGISTER_ACK,
00037 CMD_PING ,
00038 CMD_LEAVE,
00039 CMD_GET_CLIENT_INFO,
00040 CMD_GET_CLIENT_INFO_REPLY,
00041 CMD_CONNECT,
00042 CMD_CONNECT_ACK,
00043 CMD_CONNECT_OK,
00044 CMD_CONNECT_OK_ACK,
00045 CMD_SMSG,
00046 CMD_SMSG_ACK,
00047 CMD_DISCONNECT
00048 };
00049
00050 enum server_messages{
00051 MSG_OK = 0,
00052 MSG_ALREADY_REGISTERED ,
00053 MSG_NOT_FOUND,
00054 };
00055
00056
00057 int sendCommand(enum server_command cmd, net::Socket& sock);
00058
00059
00060
00061 struct packet{
00062 enum server_command cmd ;
00063 n2nc::ClientInfo::ClientID src ;
00064 n2nc::ClientInfo::ClientID dst ;
00065 uint16_t src_port ;
00066 uint16_t dst_port ;
00067 n2nc::net::SocketAddress::Storage srv_addr ;
00068 n2nc::net::SocketAddress::Storage dst_addr ;
00069 uint16_t msg ;
00070 char smsg[128] ;
00071 };
00072
00073
00074 private:
00075
00076
00077
00078
00079
00080 typedef std::map<n2nc::net::Socket*,n2nc::ClientInfo*>::iterator m_peers_it ;
00081 n2nc::net::TcpSocket *m_sock ;
00082 n2nc::net::SocketEventsHandler* m_skev ;
00083 n2nc::sync::Mutex m_lock ;
00084
00085
00086
00087
00088
00089
00090 ClientInfo *getClientInfo(net::Socket* sock=NULL, ClientInfo::ClientID id=0);
00091 ClientInfo *newClientInfo(net::Socket* sock, ClientInfo::ClientID);
00092 int delClientInfo(ClientInfo::ClientID id);
00094 int delClientInfo(net::Socket* sock);
00095
00096
00097 std::map<ClientInfo::ClientID,ClientInfo*> m_online_peers_id;
00098
00099 typedef std::map<net::Socket*,ClientInfo*>::iterator m_peers_it_sk ;
00100 typedef std::map<ClientInfo::ClientID,ClientInfo*>::iterator m_peers_it_id ;
00101
00103 virtual int onReceive(n2nc::net::Socket& sock,int dtlen);
00104 virtual int onNewConnection(n2nc::net::Socket& sock);
00105 virtual int onWriteComplete(n2nc::net::Socket& sock);
00106 virtual int onClose(n2nc::net::Socket& sock);
00107 virtual int onError(n2nc::net::Socket& sock);
00108 virtual int onRawReceive(n2nc::net::Socket& sock);
00109
00110
00111
00112
00113
00114
00115 };
00116
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 }
00155 #endif