Skip to content

Commit

Permalink
Some Work :*
Browse files Browse the repository at this point in the history
  • Loading branch information
Exmirai committed Aug 29, 2015
1 parent a5a9ee3 commit 06b7748
Show file tree
Hide file tree
Showing 17 changed files with 460 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ qrc_*.cpp
ui_*.h
Makefile*
*-build-*
Win32/

# QtCreator

Expand Down
42 changes: 42 additions & 0 deletions source/jknetapi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "launcher.h"

namespace JKNetAPI{
QHostAddress jknethost("");
bool available;

void Init(void){
QFuture<void> data = QtConcurrent::run([]{
/*Checking...connecting to main server etc... */
});
}

QString* Auth(QString &username, QString &password){

return new QString;
}

bool CheckSession(QString &username, QString &session){

return false;
}

bool ChangePassword(QString &username, QString &oldpassword, QString &newpassword){

return false;
}

QString* GetServers(void){

return new QString;
}

QString* DoRequest(QString &module, QString &request){

return new QString;
}

QString* DoRequest(const char *module, const char *request){

return new QString;
}
}
16 changes: 16 additions & 0 deletions source/jknetapi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once


namespace JKNetAPI{
extern QHostAddress jknethost;
extern bool available;

void Init(void);
QString* Auth(QString &username, QString &password);
bool CheckSession(QString &username, QString &session);
bool ChangePassword(QString &username, QString &oldpassword, QString &newpassword);
QString* GetServers(void);

QString* DoRequest(QString &module, QString &request);
QString* DoRequest(const char *module, const char *request);
}
12 changes: 11 additions & 1 deletion source/launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@
#include <QMap>
#include <QProcess>
#include <QDebug>
#include <QHostAddress>
#include <QTime>
#include <QList>
#include <QThread>
#include <QtConcurrent>

#include <QUdpSocket>
#include <QTcpSocket>

#include "smartconnect.h"
#include "settings.h"
#include "mainwindow.h"
#include "profiles.h"
#include "network.h"
#include "starter.h"
#include "serverapi.h"
#include "jknetapi.h"
#include "workshop.h"

#define QStringPTChar(x) (x)->toLocal8Bit().data()
#define QStringTChar(x) (x).toLocal8Bit().data()
10 changes: 8 additions & 2 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ int main(int argc, char *argv[])
QApplication app(argc, argv);
Settings::LauncherPath = app.applicationDirPath();
Settings::Load();
MainWindow mainWindow;

//JKNetAPI::Init();
//Modification::Init();

MainWindow mainWindow(&app);
//QObject::connect(&mainWindow, SIGNAL())
mainWindow.show();
Starter::Start();

//Starter::Start();
return app.exec();
}

4 changes: 3 additions & 1 deletion source/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
#include "ui_mainwindow.h"
#include "ui_settings.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow)
MainWindow::MainWindow(QApplication *app,QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow)
{
this->app = app;
ui->setupUi(this);
}

Expand All @@ -16,4 +17,5 @@ MainWindow::~MainWindow()

void MainWindow::closeEvent(QCloseEvent *event){
Settings::Save();
//app->quit();
}
3 changes: 2 additions & 1 deletion source/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class MainWindow : public QMainWindow
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
QApplication *app;
explicit MainWindow(QApplication *app,QWidget *parent = 0);
~MainWindow();
void closeEvent(QCloseEvent *event);

Expand Down
10 changes: 0 additions & 10 deletions source/network.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
#include "launcher.h"

#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>


void DoRequest(QString &request)
{

}
7 changes: 0 additions & 7 deletions source/network.h

This file was deleted.

185 changes: 185 additions & 0 deletions source/serverapi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#include "launcher.h"
#include <QRegExp>

#define SERVER_TIMEOUT 5000

ServerAPI::ServerAPI(QString &ip, quint16 port){
this->ip = ip;
this->port = port;

if (this->ip.isNull() || this->port == 0) return;

commsocket = new QUdpSocket();
commsocket->bind();

GetType(); // check server :d
}

ServerAPI::ServerAPI(QHostAddress &ip, quint16 port){
this->ip = ip;
this->port = port;

if (this->ip.isNull() || this->port == 0) return;

commsocket = new QUdpSocket();
commsocket->bind();


GetType(); // check server :d
}

ServerAPI::~ServerAPI(){
delete commsocket;
}

ServerAPI::ServerType ServerAPI::GetType(void){ // JKNet Servers
if (ip.isNull()) return SERVER_NULL;
const char *str = "\xFF\xFF\xFF\xFFjknet gettype";

commsocket->writeDatagram(str, ip, port);
QTime sendtime = QTime::currentTime();
sendtime.addMSecs(SERVER_TIMEOUT);

while (!commsocket->hasPendingDatagrams()) {
if (sendtime <= QTime::currentTime()) return SERVER_NULL;
}
while (commsocket->hasPendingDatagrams()){ // xddd
int len = commsocket->pendingDatagramSize();
char *response = (char *)malloc(len);
memset(response, 0, len);

QHostAddress sender;
quint16 senderPort;
commsocket->readDatagram(response, len, &sender, &senderPort);
if (/*(sender != ip) ||*/ (senderPort != port)){
continue;
}

QString data(response);
DecodeMessage(data);
/*
*/
}

return SERVER_NORMAL;
}

QMap<QString, QString>* ServerAPI::GetStatus(void){
if (ip.isNull()) return nullptr;

QMap<QString, QString>* map;
const char *str = "\xFF\xFF\xFF\xFFgetstatus";

commsocket->writeDatagram(str, ip, port);

QTime sendtime = QTime::currentTime();
sendtime.addMSecs(SERVER_TIMEOUT);

while (!commsocket->hasPendingDatagrams()) {
if (sendtime <= QTime::currentTime()) return nullptr;
}
while (commsocket->hasPendingDatagrams()){ // xddd
int len = commsocket->pendingDatagramSize();
char *response = (char *)malloc(len);
memset(response, 0, len);
QHostAddress sender;
quint16 senderPort;
commsocket->readDatagram(response, len, &sender, &senderPort);
if (/*(sender != ip) ||*/ (senderPort != port)){
continue;
}
map = new QMap < QString, QString >;

QString data(response);
DecodeMessage(data);
QChar delim('\\');
QStringList list = data.split(delim);
for (int i = 1; i < list.count(); i++ , i++){ // i++ tw0 times
map->insert(list[i], list[i + 1]);
}
}
return map; ///
}

QMap<QString, QString>* ServerAPI::GetInfo(void){
if (ip.isNull()) return nullptr;

QMap<QString, QString>* map;
const char *str = "\xFF\xFF\xFF\xFFgetinfo";

commsocket->writeDatagram(str, ip, port);

QTime sendtime = QTime::currentTime();
sendtime.addMSecs(SERVER_TIMEOUT);

while (!commsocket->hasPendingDatagrams()) {
if (sendtime <= QTime::currentTime()) return nullptr;
}
while (commsocket->hasPendingDatagrams()){ // xddd
int len = commsocket->pendingDatagramSize();
char *response = (char *)malloc(len);
memset(response, 0, len);
QHostAddress sender;
quint16 senderPort;
commsocket->readDatagram(response, len, &sender, &senderPort);
if (/*(sender != ip) ||*/ (senderPort != port)){
continue;
}
map = new QMap < QString, QString >;

QString data(response);
DecodeMessage(data);
QChar delim('\\');
QStringList list = data.split(delim);
for (int i = 1; i < list.count(); i++, i++){ // i++ tw0 times
map->insert(list[i], list[i + 1]);
}
}
return map; ///
}

void ServerAPI::SendCommand(QString &command){
if (ip.isNull()) return;
EncodeMessage(command);
commsocket->writeDatagram(command.toLocal8Bit(), ip, port);
}

int ServerAPI::Ping(void){
if (ip.isNull()) return -1;

const char *str = "\xFF\xFF\xFF\xFFjknet ping";

commsocket->writeDatagram(str, ip, port);

QTime sendtime = QTime::currentTime();
int receivetime = 0;
sendtime.addMSecs(SERVER_TIMEOUT);
sendtime.start();

while (!commsocket->hasPendingDatagrams()) {
if (sendtime<= QTime::currentTime()) return -1;
}
while (commsocket->hasPendingDatagrams()){ // xddd
int len = commsocket->pendingDatagramSize();
receivetime = sendtime.elapsed();
QHostAddress sender;
quint16 senderPort;
commsocket->readDatagram(NULL, len, &sender, &senderPort);
if ((sender != ip) || (senderPort != port)){
receivetime = 0; //still zero
continue;
}
}
return receivetime;
}

void ServerAPI::EncodeMessage(QString &message){
message.insert(0, QString("\xFF\xFF\xFF\xFF"));
}

void ServerAPI::DecodeMessage(QString &message){
message.replace(0, 4, "");
}
31 changes: 31 additions & 0 deletions source/serverapi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once


class ServerAPI : public QObject{
private:
QUdpSocket *commsocket;

public:
typedef enum{
SERVER_NULL = 0,
SERVER_NORMAL,
SERVER_JKNET,
}ServerType;

QHostAddress ip;
quint16 port;
ServerType type;

ServerAPI(QString &ip, quint16 port = 29070);
ServerAPI(QHostAddress &ip, quint16 port = 29070);
~ServerAPI();

int Ping(void);
void SendCommand(QString &command);
QMap<QString, QString>* GetStatus(void);
QMap<QString, QString>* GetInfo(void);
ServerType GetType(void);

static void EncodeMessage(QString &message);
static void DecodeMessage(QString &message);
};
Loading

0 comments on commit 06b7748

Please sign in to comment.