Rev 999 | Rev 1026 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 999 | Rev 1024 | ||
|---|---|---|---|
| Line 47... | Line 47... | ||
| 47 | mySocket.setPort(port); |
47 | mySocket.setPort(port); |
| 48 | } |
48 | } |
| 49 | 49 | ||
| 50 | void Server::run() |
50 | void Server::run() |
| 51 | { |
51 | { |
| 52 |
|
52 | Logger &log = Logger::getInstance(); |
| 53 | 53 | ||
| 54 | try |
54 | try |
| 55 | { |
55 | { |
| 56 | mySocket.startListen(); |
56 | mySocket.startListen(); |
| 57 | 57 | ||
| 58 |
|
58 | log.add(myName + " is listening for connections on port " + itos(mySocket.getPort()) + ".\n"); |
| 59 | 59 | ||
| 60 | while (true) |
60 | while (true) |
| 61 | { |
61 | { |
| 62 | AsyncSocket *newSocket = new AsyncSocket(); |
62 | AsyncSocket *newSocket = new AsyncSocket(); |
| 63 | 63 | ||
| 64 | if (mySocket.accept(newSocket)) |
64 | if (mySocket.accept(newSocket)) |
| 65 | { |
65 | { |
| 66 | newSocket->sendData("Welcome to Atom " + ftos(VERSION) + " - " + myName + "\n"); |
66 | newSocket->sendData("Welcome to Atom " + ftos(VERSION) + " - " + myName + "\n"); |
| 67 | 67 | ||
| 68 |
|
68 | log.add(myName + " accepted new client with id " + itos(newSocket->getId()) + ".\n"); |
| 69 | myClientSockets[newSocket->getId()] = newSocket; |
69 | myClientSockets[newSocket->getId()] = newSocket; |
| 70 | myClientSockets[newSocket->getId()]->eventSetCallback(this); |
70 | myClientSockets[newSocket->getId()]->eventSetCallback(this); |
| 71 | myClientSockets[newSocket->getId()]->start(); |
71 | myClientSockets[newSocket->getId()]->start(); |
| 72 | } |
72 | } |
| 73 | } |
73 | } |
| 74 | } |
74 | } |
| 75 | catch (SocketException* e) |
75 | catch (SocketException* e) |
| 76 | { |
76 | { |
| 77 |
|
77 | log.add(myName + " caught an exception:" + e->getDescription() + ".\n"); |
| 78 | } |
78 | } |
| 79 | } |
79 | } |
| 80 | 80 | ||
| 81 | void Server::sendToAll(string data) |
81 | void Server::sendToAll(string data) |
| 82 | { |
82 | { |
| 83 |
|
83 | Logger &log = Logger::getInstance(); |
| 84 | 84 | ||
| 85 | for (map<int, AsyncSocket*>::iterator iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++) |
85 | for (map<int, AsyncSocket*>::iterator iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++) |
| 86 | { |
86 | { |
| 87 | if (iter->second->isConnected()) |
87 | if (iter->second->isConnected()) |
| 88 | { |
88 | { |
| 89 | // |
89 | //log.add("Server: sent data to client " + itos(iter->second->getSocket()) + ".\n"; |
| 90 | 90 | ||
| 91 | try |
91 | try |
| 92 | { |
92 | { |
| 93 | iter->second->sendData(data); |
93 | iter->second->sendData(data); |
| 94 | } |
94 | } |
| 95 | catch (SocketException* e) |
95 | catch (SocketException* e) |
| 96 | { |
96 | { |
| 97 |
|
97 | log.add(myName + " caught an exception:" + e->getDescription() + ".\n"); |
| 98 | } |
98 | } |
| 99 | } |
99 | } |
| 100 | else |
100 | else |
| 101 | { |
101 | { |
| 102 |
|
102 | log.add(myName + " had a client disconnect.\n"); |
| 103 | delete iter->second; |
103 | delete iter->second; |
| 104 | myClientSockets.erase(iter); |
104 | myClientSockets.erase(iter); |
| 105 | } |
105 | } |
| 106 | } |
106 | } |
| 107 | } |
107 | } |
| 108 | 108 | ||
| 109 | void Server::sendTo(int id, string data) |
109 | void Server::sendTo(int id, string data) |
| 110 | { |
110 | { |
| 111 |
|
111 | Logger &log = Logger::getInstance(); |
| 112 | 112 | ||
| 113 | if (myClientSockets.find(id) != myClientSockets.end()) |
113 | if (myClientSockets.find(id) != myClientSockets.end()) |
| 114 | { |
114 | { |
| 115 | if (myClientSockets[id]->isConnected()) |
115 | if (myClientSockets[id]->isConnected()) |
| 116 | { |
116 | { |
| 117 | // |
117 | //log.add("Server: sent data to client " + itos(iter->second->getSocket()) + ".\n"; |
| 118 | 118 | ||
| 119 | try |
119 | try |
| 120 | { |
120 | { |
| 121 | myClientSockets[id]->sendData(data); |
121 | myClientSockets[id]->sendData(data); |
| 122 | } |
122 | } |
| 123 | catch (SocketException* e) |
123 | catch (SocketException* e) |
| 124 | { |
124 | { |
| 125 |
|
125 | log.add(myName + " caught an exception:" + e->getDescription() + ".\n"); |
| 126 | } |
126 | } |
| 127 | } |
127 | } |
| 128 | else |
128 | else |
| 129 | { |
129 | { |
| 130 |
|
130 | log.add(myName + " had a client disconnect.\n"); |
| 131 | delete myClientSockets[id]; |
131 | delete myClientSockets[id]; |
| 132 | myClientSockets.erase(id); |
132 | myClientSockets.erase(id); |
| 133 | } |
133 | } |
| 134 | } |
134 | } |
| 135 | } |
135 | } |
| 136 | 136 | ||
| 137 | void Server::disconnectClient(int id) |
137 | void Server::disconnectClient(int id) |
| 138 | { |
138 | { |
| 139 |
|
139 | Logger &log = Logger::getInstance(); |
| 140 | 140 | ||
| 141 | if (myClientSockets.find(id) != myClientSockets.end()) |
141 | if (myClientSockets.find(id) != myClientSockets.end()) |
| 142 | { |
142 | { |
| 143 |
|
143 | log.add(myName + " had a client disconnect.\n"); |
| 144 | delete myClientSockets[id]; |
144 | delete myClientSockets[id]; |
| 145 | myClientSockets.erase(id); |
145 | myClientSockets.erase(id); |
| 146 | } |
146 | } |
| 147 | } |
147 | } |
| 148 | 148 | ||
| Line 162... | Line 162... | ||
| 162 | } |
162 | } |
| 163 | } |
163 | } |
| 164 | 164 | ||
| 165 | void Server::handleClientData(int id, string data) |
165 | void Server::handleClientData(int id, string data) |
| 166 | { |
166 | { |
| 167 |
|
167 | Logger &log = Logger::getInstance(); |
| 168 |
|
168 | log.add(myName + " received data from client " + itos(id) + ": \"" + data + "\"\n"); |
| 169 | } |
169 | } |