-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp.cpp
More file actions
133 lines (117 loc) · 2.88 KB
/
tcp.cpp
File metadata and controls
133 lines (117 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "tcp.hpp"
TCPServer::~TCPServer()
{
for( size_t n = 0; n < conns.size(); n++)
{
conns[n]->Disconnect();
delete conns[n];
}
conns.clear();
}
wxConnectionBase *TCPServer::OnAcceptConnection(const wxString& topic)
{
wxLogMessage(_T("OnAcceptConnection(\"%s\")"), topic.c_str());
wxString auth = topic.substr(0, 5);
wxString name = topic.substr(5,topic.size()-5);
if ( auth == "Logon" )
{
TCPConnection* conn = new TCPConnection(this, name);
conns.push_back(conn);
wxLogMessage(_T("Connection to %s accepted"), name.c_str());
//wxMessageBox(wxString::Format("%x", conn), "ptr_info");
ConnecTimer* t = new ConnecTimer(this, name);
t->Start(10, true);
//wxString msg = name + " connected.";
//conn->Advise("Logon", (char*)msg.c_str(), msg.size());
return conn;
}
// unknown topic
wxLogMessage("Connection denied!");
return NULL;
}
size_t TCPServer::IDManager()
{
size_t id = 0;
bool valid = false;
while( !valid)
{
valid = true;
for( size_t n = 0; n < conns.size(); n++)
{
if( conns[n]->id == id)
{
valid = false;
id++;
}
}
}
return id;
}
TCPConnection::TCPConnection(TCPServer* _server, wxString _nick) : wxTCPConnection()
{
server = _server;
id = server->IDManager();
nick = _nick;
}
bool TCPConnection::OnDisconnect()
{
wxLogMessage(_T("OnDisconnect()"));
wxString nick;
for( size_t n = 0; n < server->conns.size(); n++)
{
if( server->conns[n]->id == id)
{
nick = server->conns[n]->nick;
delete server->conns[n];
server->conns.erase(server->conns.begin()+n);
wxString msg = "[";
msg += wxDateTime::Now().FormatTime() + "] " + nick + " disconnected.";
for( size_t i = 0; i < server->conns.size(); i++)
{
server->conns[i]->Advise("logoff", (char*)msg.c_str(), msg.size());
}
break;
}
}
return true;
}
bool TCPConnection::OnPoke(const wxString& topic, const wxString& item, wxChar *data, int size, wxIPCFormat format)
{
wxLogMessage("OnPoke with item %s", item.c_str());
char* msg = new char[size+1];
memcpy(msg, data, size);
msg[size] = 0;
wxLogMessage("The data was %s", msg);
for( size_t i = 0; i < server->conns.size(); i++)
{
server->conns[i]->Advise("message", msg, size+1);
}
return true;
}
char* TCPConnection::OnRequest(const wxString& topic, const wxString& item, int *size, wxIPCFormat format)
{
if( item == "nicklist")
{
wxString nicks = "";
for( size_t i = 0; i < server->conns.size(); i++)
{
nicks += server->conns[i]->nick + "\n";
}
return (char*)nicks.c_str();
}
return 0;
}
ConnecTimer::ConnecTimer(TCPServer* _serv, wxString _nick)
{
serv = _serv;
nick = _nick;
}
void ConnecTimer::Notify()
{
wxString msg = "[";
msg += wxDateTime::Now().FormatTime() + "] " + nick + " connected.";
for( size_t i = 0; i < serv->conns.size(); i++)
{
serv->conns[i]->Advise("logon", (char*)msg.c_str(), msg.size());
}
}