qnetdice

view src/main.cc @ 3:2e57c80653af

more stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Dec 2013 06:09:25 +0200
parents 92377189a5c6
children
line source
1 #include <stdlib.h>
2 #include <time.h>
3 #include <vector>
4 #include <QApplication>
5 #include <QStatusBar>
6 #include "main.h"
7 #include "mainwin.h"
8 #include "roll.h"
9 #include "logger.h"
11 static void new_client();
12 static void status_log(const char *msg, void *cls);
14 std::vector<Roll*> dice;
15 QTcpServer *server;
16 std::vector<QTcpSocket*> clients;
18 QTcpSocket *socket;
20 static std::string srv_passwd;
21 static MainWindow *win_main;
23 int main(int argc, char *argv[])
24 {
25 srand(time(0));
27 QApplication app(argc, argv);
29 win_main = new MainWindow;
30 win_main->show();
32 set_log_file(stdout);
33 set_log_callback(status_log, win_main->statusBar());
35 return app.exec();
36 }
39 void start_server(int port, const char *passwd)
40 {
41 if(server) return;
43 log_message("starting server on port: %d\n", port);
45 server = new QTcpServer;
46 server->listen(QHostAddress::Any, port);
47 srv_passwd = passwd;
49 QObject::connect(server, &QTcpServer::newConnection, new_client);
50 }
53 static void new_client()
54 {
55 QTcpSocket *sock = server->nextPendingConnection();
56 if(!sock) return;
58 QHostAddress client_addr = sock->peerAddress();
59 clients.push_back(sock);
61 log_message("received connection request from: %s\n", client_addr.toString().toUtf8().data());
62 }
64 void start_client(const char *host, int port, const char *passwd)
65 {
66 log_message("connecting to host: %s port: %d\n", host, port);
67 }
70 static void status_log(const char *msg, void *cls)
71 {
72 QStatusBar *sbar = (QStatusBar*)cls;
73 sbar->showMessage(msg);
74 }