qnetdice

view src/main.cc @ 1:92377189a5c6

moving along
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Dec 2013 04:08:50 +0200
parents 0e9c16d77e47
children 2e57c80653af
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);
28 win_main.show();
30 set_log_file(stdout);
31 set_log_callback(status_log, win_main.statusBar());
33 return app.exec();
34 }
37 void start_server(int port, const char *passwd)
38 {
39 if(server) return;
41 log_message("starting server on port: %d\n", port);
43 server = new QTcpServer;
44 server->listen(QHostAddress::Any, port);
45 srv_passwd = passwd;
47 QObject::connect(server, &QTcpServer::newConnection, new_client);
48 }
51 static void new_client()
52 {
53 QTcpSocket *sock = server->nextPendingConnection();
54 if(!sock) return;
56 QHostAddress client_addr = sock->peerAddress();
57 clients.push_back(sock);
59 log_message("received connection request from: %s\n", client_addr.toString().toUtf8().data());
60 }
62 void connect(const char *host, int port, const char *passwd)
63 {
64 log_message("connecting to host: %s port: %d\n", host, port);
65 }
68 static void status_log(const char *msg, void *cls)
69 {
70 QStatusBar *sbar = (QStatusBar*)cls;
71 sbar->showMessage(msg);
72 }