qnetdice
diff 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 diff
1.1 --- a/src/main.cc Thu Dec 19 13:34:40 2013 +0200 1.2 +++ b/src/main.cc Sun Dec 22 04:08:50 2013 +0200 1.3 @@ -1,11 +1,72 @@ 1.4 +#include <stdlib.h> 1.5 +#include <time.h> 1.6 +#include <vector> 1.7 +#include <QApplication> 1.8 +#include <QStatusBar> 1.9 +#include "main.h" 1.10 #include "mainwin.h" 1.11 -#include <QApplication> 1.12 +#include "roll.h" 1.13 +#include "logger.h" 1.14 + 1.15 +static void new_client(); 1.16 +static void status_log(const char *msg, void *cls); 1.17 + 1.18 +std::vector<Roll*> dice; 1.19 +QTcpServer *server; 1.20 +std::vector<QTcpSocket*> clients; 1.21 + 1.22 +QTcpSocket *socket; 1.23 + 1.24 +static std::string srv_passwd; 1.25 +static MainWindow win_main; 1.26 1.27 int main(int argc, char *argv[]) 1.28 { 1.29 - QApplication a(argc, argv); 1.30 - MainWindow w; 1.31 - w.show(); 1.32 + srand(time(0)); 1.33 1.34 - return a.exec(); 1.35 + QApplication app(argc, argv); 1.36 + win_main.show(); 1.37 + 1.38 + set_log_file(stdout); 1.39 + set_log_callback(status_log, win_main.statusBar()); 1.40 + 1.41 + return app.exec(); 1.42 } 1.43 + 1.44 + 1.45 +void start_server(int port, const char *passwd) 1.46 +{ 1.47 + if(server) return; 1.48 + 1.49 + log_message("starting server on port: %d\n", port); 1.50 + 1.51 + server = new QTcpServer; 1.52 + server->listen(QHostAddress::Any, port); 1.53 + srv_passwd = passwd; 1.54 + 1.55 + QObject::connect(server, &QTcpServer::newConnection, new_client); 1.56 +} 1.57 + 1.58 + 1.59 +static void new_client() 1.60 +{ 1.61 + QTcpSocket *sock = server->nextPendingConnection(); 1.62 + if(!sock) return; 1.63 + 1.64 + QHostAddress client_addr = sock->peerAddress(); 1.65 + clients.push_back(sock); 1.66 + 1.67 + log_message("received connection request from: %s\n", client_addr.toString().toUtf8().data()); 1.68 +} 1.69 + 1.70 +void connect(const char *host, int port, const char *passwd) 1.71 +{ 1.72 + log_message("connecting to host: %s port: %d\n", host, port); 1.73 +} 1.74 + 1.75 + 1.76 +static void status_log(const char *msg, void *cls) 1.77 +{ 1.78 + QStatusBar *sbar = (QStatusBar*)cls; 1.79 + sbar->showMessage(msg); 1.80 +}