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 134 135 136 137 138 139 140 141 142 143 144 145
|
#include <dashel/dashel.h>
#include <iostream>
#include <cassert>
using namespace std;
using namespace Dashel;
string readLine(Stream* stream)
{
char c;
string line;
do
{
stream->read(&c, 1);
line += c;
}
while (c != '\n' && c != '\r');
return line;
}
void sendString(Stream* stream, const string& line)
{
stream->write(line.c_str(), line.length());
stream->flush();
}
class ChatServer: public Hub
{
public:
ChatServer()
{
listenStream = connect("tcpin:port=8765");
}
protected:
Stream* listenStream;
map<Stream*, string> nicks;
protected:
void connectionCreated(Stream *stream)
{
cout << "+ Incoming connection from " << stream->getTargetName() << " (" << stream << ")" << endl;
string nick = readLine(stream);
nick.erase(nick.length() - 1);
nicks[stream] = nick;
cout << "+ User " << nick << " is connected." << endl;
}
void incomingData(Stream *stream)
{
string line = readLine(stream);
const string& nick = nicks[stream];
line = nick + " : " + line;
cout << "* Message from " << line;
for (StreamsSet::iterator it = dataStreams.begin(); it != dataStreams.end(); ++it)
sendString((*it), line);
}
void connectionClosed(Stream *stream, bool abnormal)
{
cout << "- Connection closed to " << stream->getTargetName() << " (" << stream << ")";
if (abnormal)
cout << " : " << stream->getFailReason();
cout << endl;
string nick = nicks[stream];
nicks.erase(stream);
cout << "- User " << nick << " is disconnected." << endl;
}
};
class ChatClient: public Hub
{
public:
ChatClient(string remoteTarget, const string& nick) :
inputStream(0), nick(nick)
{
remoteTarget += ";port=8765";
inputStream = connect("stdin:");
remoteStream = connect(remoteTarget);
this->nick += '\n';
sendString(remoteStream, this->nick);
}
protected:
Stream* inputStream;
Stream* remoteStream;
string nick;
void connectionCreated(Stream *stream)
{
cout << "Incoming connection " << stream->getTargetName() << " (" << stream << ")" << endl;
}
void incomingData(Stream *stream)
{
assert(inputStream);
assert(remoteStream);
if (stream == inputStream)
{
string line = readLine(inputStream);
sendString(remoteStream, line);
}
else
{
string line = readLine(remoteStream);
cout << line;
cout.flush();
}
}
void connectionClosed(Stream *stream, bool abnormal)
{
cout << "Connection closed to " << stream->getTargetName() << " (" << stream << ")";
if (abnormal)
cout << " : " << stream->getFailReason();
cout << endl;
stop();
}
};
int main(int argc, char* argv[])
{
try
{
if (argc > 2)
{
ChatClient client(argv[1], argv[2]);
client.run();
}
else
{
ChatServer server;
server.run();
}
}
catch(const DashelException &e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
|