#ifndef NETWORK_H
#define NETWORK_H
#include "RakNet/BitStream.h"
#include "RakNet/RakClientInterface.h"
#include "RakNet/RakNetworkFactory.h"
#include "RakNet/RakServerInterface.h"
#include "RakNet/PacketEnumerations.h"
#ifdef _WIN32
#include "Windows.h" //sleep
#endif
{
Q_OBJECT
public:
~Network(void);
public slots:
void connect(void);
void disconnect(void);
void sendInt(char *outmessage);
void sendData(char *outmessage);
signals:
void connecting(bool success);
void connectionAccepted(void);
void disconnection(void);
void chatMessage
(const QString &message
);
private:
RakClientInterface* clientInterface;
Packet* p;
void receive(void);
void handleBitStream(Packet* a);
void handleIntStream(Packet* a);
};
#endif
#ifndef NETWORK_H
#define NETWORK_H
#include "RakNet/BitStream.h"
#include "RakNet/RakClientInterface.h"
#include "RakNet/RakNetworkFactory.h"
#include "RakNet/RakServerInterface.h"
#include "RakNet/PacketEnumerations.h"
#ifdef _WIN32
#include "Windows.h" //sleep
#endif
class Network : public QObject
{
Q_OBJECT
public:
Network(QObject *parent=0);
~Network(void);
public slots:
void connect(void);
void disconnect(void);
void sendInt(char *outmessage);
void sendData(char *outmessage);
signals:
void connecting(bool success);
void connectionAccepted(void);
void disconnection(void);
void chatMessage(const QString &message);
private:
RakClientInterface* clientInterface;
Packet* p;
void receive(void);
void handleBitStream(Packet* a);
void handleIntStream(Packet* a);
};
#endif
To copy to clipboard, switch view to plain text mode
#include <QObject>
#include "network.h"
{
clientInterface = RakNetworkFactory::GetRakClientInterface();
}
Network::~Network()
{
//shut down
if(clientInterface)
RakNetworkFactory::DestroyRakClientInterface(clientInterface);
}
void Network::connect()
{
char ip[14]="127.0.0.1";
char port[6]="44394";
if(clientInterface->Connect(ip, atoi(port), 0, 0, 30))
{
emit connecting(true);
}
else
{
emit connecting(false);
}
}
void Network::disconnect()
{
clientInterface->Disconnect(30);
}
void Network::sendInt(char* outmessage)
{
RakNet::BitStream outstream;
outstream.Write(ID_INT);
outstream.Write(atoi(outmessage));
if(clientInterface)
clientInterface->Send(&outstream, HIGH_PRIORITY, RELIABLE_ORDERED, 0 );
outstream.Reset();
}
void Network::sendData(char* outmessage)
{
RakNet::BitStream outstream;
outstream.Write(ID_TEST);
outstream.Write((int)strlen(outmessage));
outstream.Write(outmessage, (int)strlen(outmessage));
if(clientInterface)
clientInterface->Send(&outstream, HIGH_PRIORITY, RELIABLE_ORDERED, 0 );
outstream.Reset();
}
void Network::receive()
{
if(clientInterface)
{
p = clientInterface->Receive();
}
if(p)
{
switch(p->data[0])
{
case ID_CONNECTION_REQUEST_ACCEPTED:
emit verbindingGeaccepteerd();
break;
case ID_RECEIVED_STATIC_DATA:
printf("Static data received.\n");
break;
case ID_DISCONNECTION_NOTIFICATION:
emit disconnect();
break;
case ID_TEST:
//printf("Test message coming in.\n");
handleBitStream(p);
break;
case ID_INT:
//printf("Int message coming in.\n");
handleIntStream(p);
break;
default:
printf("Packet incoming with identifier %i.\n", p->data[0]);
break;
}
if(clientInterface)
clientInterface->DeallocatePacket(p);
}
}
void Network::handleBitStream(Packet* a)
{
RakNet::BitStream bitStream((char*)a->data, a->length, false);
char* incmessage;
unsigned int messagelength=0;
bitStream.IgnoreBits(32); //sizeof(unsigned char)*8); // Ignore the packet type enum
bitStream.Read(messagelength);
//printf("%i\n",messagelength);
incmessage=new char[messagelength];
if(bitStream.Read(incmessage, messagelength))
{
emit chatMessage
(QString(incmessage
));
//incmessage); //printf("\tDe ander zegt: %s\n",incmessage); }
else
{
//printf("Message error.\n");
}
bitStream.Reset();
}
void Network::handleIntStream(Packet* a)
{
RakNet::BitStream bitStream((char*)a->data, a->length, false);
int intje=0;
bitStream.IgnoreBits(sizeof(ID_INT)*8); // Ignore the packet type enum
if(bitStream.Read(intje))
{
}
else
{
//printf("Message error.\n");
}
bitStream.Reset();
}
#include <QObject>
#include "network.h"
Network::Network(QObject *parent)
: QObject(parent)
{
clientInterface = RakNetworkFactory::GetRakClientInterface();
}
Network::~Network()
{
//shut down
if(clientInterface)
RakNetworkFactory::DestroyRakClientInterface(clientInterface);
}
void Network::connect()
{
char ip[14]="127.0.0.1";
char port[6]="44394";
if(clientInterface->Connect(ip, atoi(port), 0, 0, 30))
{
emit connecting(true);
}
else
{
emit connecting(false);
}
}
void Network::disconnect()
{
clientInterface->Disconnect(30);
}
void Network::sendInt(char* outmessage)
{
RakNet::BitStream outstream;
outstream.Write(ID_INT);
outstream.Write(atoi(outmessage));
if(clientInterface)
clientInterface->Send(&outstream, HIGH_PRIORITY, RELIABLE_ORDERED, 0 );
outstream.Reset();
}
void Network::sendData(char* outmessage)
{
RakNet::BitStream outstream;
outstream.Write(ID_TEST);
outstream.Write((int)strlen(outmessage));
outstream.Write(outmessage, (int)strlen(outmessage));
if(clientInterface)
clientInterface->Send(&outstream, HIGH_PRIORITY, RELIABLE_ORDERED, 0 );
outstream.Reset();
}
void Network::receive()
{
if(clientInterface)
{
p = clientInterface->Receive();
}
if(p)
{
switch(p->data[0])
{
case ID_CONNECTION_REQUEST_ACCEPTED:
emit verbindingGeaccepteerd();
break;
case ID_RECEIVED_STATIC_DATA:
printf("Static data received.\n");
break;
case ID_DISCONNECTION_NOTIFICATION:
emit disconnect();
break;
case ID_TEST:
//printf("Test message coming in.\n");
handleBitStream(p);
break;
case ID_INT:
//printf("Int message coming in.\n");
handleIntStream(p);
break;
default:
printf("Packet incoming with identifier %i.\n", p->data[0]);
break;
}
if(clientInterface)
clientInterface->DeallocatePacket(p);
}
}
void Network::handleBitStream(Packet* a)
{
RakNet::BitStream bitStream((char*)a->data, a->length, false);
char* incmessage;
unsigned int messagelength=0;
bitStream.IgnoreBits(32); //sizeof(unsigned char)*8); // Ignore the packet type enum
bitStream.Read(messagelength);
//printf("%i\n",messagelength);
incmessage=new char[messagelength];
if(bitStream.Read(incmessage, messagelength))
{
emit chatMessage(QString(incmessage)); //incmessage); //printf("\tDe ander zegt: %s\n",incmessage);
}
else
{
//printf("Message error.\n");
}
bitStream.Reset();
}
void Network::handleIntStream(Packet* a)
{
RakNet::BitStream bitStream((char*)a->data, a->length, false);
int intje=0;
bitStream.IgnoreBits(sizeof(ID_INT)*8); // Ignore the packet type enum
if(bitStream.Read(intje))
{
emit chatBericht(QString(QChar(intje)));
}
else
{
//printf("Message error.\n");
}
bitStream.Reset();
}
To copy to clipboard, switch view to plain text mode
Bookmarks