Last active
October 29, 2016 17:03
-
-
Save JAChapmanII/fd5431595e436f0a2e4644e88a03c8e9 to your computer and use it in GitHub Desktop.
current indigo chat server impl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
#include <oil/util.hpp> | |
#include <indigo/server.hpp> | |
#include <indigo/client.hpp> | |
#include "protocol.hpp" | |
#include "martial.hpp" | |
#include "protoserver.hpp" | |
using martial::word; | |
using Client = indigo::SecureClient; | |
using Server = indigo::SecureServer<Client>; | |
namespace client { | |
string nicklist(vector<string> nicks) { | |
return proto::code[proto::cmd::Nicklist] + util::join(nicks, " "); | |
} | |
string message(std::string nick, word where, string message) { | |
return proto::code[proto::cmd::Message] + nick + " " + where + " :" + message; | |
} | |
string disconnect(std::string nick) { | |
return proto::code[proto::cmd::Quit] + nick; | |
} | |
} | |
struct ChatServer : public ProtoServer<ChatServer, Client, Server> { | |
static void connect(Client *client) { | |
ProtoServer::connect(client); | |
} | |
static void disconnect(Client *client) { | |
if(client->authed()) { | |
client->wantsDisconnect = true; | |
ProtoServer::broadcast(client::disconnect(client->nick())); | |
ProtoServer::broadcast(client::nicklist(ChatServer::getNicks())); | |
} | |
ProtoServer::disconnect(client); | |
} | |
static vector<string> getNicks() { | |
vector<string> nicks; | |
nicks.reserve(ProtoServer::clients.size()); | |
for(auto &client : clients) | |
if(!client->wantsDisconnect) | |
nicks.push_back(client->nick()); | |
return nicks; | |
} | |
}; | |
namespace chat { | |
void version(Client *client, string version) { | |
if(version != proto::version) { | |
cerr << "client has mismatched version, disconnecting: " | |
<< version << " != " << proto::version << endl; | |
client->wantsDisconnect = true; | |
} else { | |
lwsl_notice("client has correct version"); | |
} | |
} | |
void nick(Client *client, word nick) { | |
client->setNick(nick); | |
Server::broadcast(client::nicklist(ChatServer::getNicks())); | |
} | |
void pass(Client *, word pass) { | |
cerr << "got pass command: " << pass << endl; | |
} | |
void message(Client *client, word where, string message) { | |
message = message.substr(1); // trim off ':' | |
cerr << "got message, client nick is '" << client->nick() << "'" << endl; | |
Server::broadcast(client::message(client->nick(), where, message)); | |
} | |
} | |
int main(int argc, char **argv) { | |
std::cout << "indigo chat server" << std::endl; | |
proto::init(); | |
proto::dumpInfo(); | |
ChatServer::funcs[proto::cmd::Message] = martial::make_wrapper(chat::message); | |
ChatServer::funcs[proto::cmd::Nick] = martial::make_wrapper(chat::nick); | |
ChatServer::funcs[proto::cmd::Pass] = martial::make_wrapper(chat::pass); | |
ChatServer::funcs[proto::cmd::Version] = martial::make_wrapper(chat::version); | |
return indigo::main<ChatServer, Client>(argc, argv); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment