SoundBand/sync/node.h

99 lines
2.2 KiB
C
Raw Normal View History

2017-11-06 01:33:16 +03:00
#ifndef NODE_H
#define NODE_H
#include <QTcpServer>
2017-11-18 01:29:14 +03:00
#include "ETcpSocket.h"
2017-11-13 23:05:59 +03:00
#include <song.h>
2017-11-11 14:03:14 +03:00
class Syncer;
2017-11-06 01:33:16 +03:00
namespace syncLib {
2017-11-20 00:37:12 +03:00
/**
* @brief The TypePackage enum
* t_void - this package empty and not valid.
* t_close - the information about close channel.
* t_sync - the infomation about sync playning media file on network.
* t_song - the package with this type is necessary for translite media data on network.
* t_stop - the package with type 'stop' necessary for stoping playning media files.
*/
enum TypePackage{
2017-11-21 16:44:55 +03:00
t_void = 0x00,
t_play = 0x01,
t_sync = 0x02,
t_song_h = 0x04,
t_song = 0x08,
t_close = 0x10,
t_stop = 0x20
2017-11-20 00:37:12 +03:00
};
2017-11-11 14:03:14 +03:00
/**
* @brief The package class. Package for translite media data on network
*
* parse map:
* 1 byle - type
* 4 byte - size of data of package (it avelable if type is t_sync or t_song)
* data
*/
2017-11-06 01:33:16 +03:00
class package
{
2017-11-06 13:20:52 +03:00
/*parse map */
/*
* 1 byle - type
* 4 byte - size of data of package (it avelable if type is t_sync or t_song)
* data
*/
2017-11-20 00:37:12 +03:00
2017-11-06 01:33:16 +03:00
private:
TypePackage type;
Song source;
2017-11-20 00:37:12 +03:00
SongHeader header;
2017-11-11 14:03:14 +03:00
Syncer playdata;
2017-11-06 01:33:16 +03:00
public:
package();
package(const QByteArray& array);
~package();
2017-11-11 14:03:14 +03:00
/**
* @brief getSong
* @return Song
*/
2017-11-06 01:33:16 +03:00
const Song& getSong() const;
2017-11-11 14:03:14 +03:00
/**
* @brief getPlayTime
* @return time of playning media data
*/
Syncer getPlayData() const;
2017-11-13 23:05:59 +03:00
TypePackage getType() const;
2017-11-06 01:33:16 +03:00
bool isValid() const;
2017-11-20 00:37:12 +03:00
void clear();
2017-11-13 23:05:59 +03:00
QByteArray parseTo();
2017-11-06 01:33:16 +03:00
bool parseFrom(const QByteArray& array);
2017-11-20 00:37:12 +03:00
friend class Sync;
2017-11-06 01:33:16 +03:00
};
class Node:public QTcpServer{
Q_OBJECT
protected:
2017-11-18 01:29:14 +03:00
QList<ETcpSocket*> clients;
2017-11-06 01:33:16 +03:00
private slots:
2017-11-18 01:29:14 +03:00
void acceptError_(ETcpSocket*);
2017-11-06 01:33:16 +03:00
void newConnection_();
2017-11-18 01:29:14 +03:00
void readData(ETcpSocket*_client);
2017-11-06 01:33:16 +03:00
public:
2017-11-11 20:35:30 +03:00
Node();
2017-11-06 01:33:16 +03:00
void WriteAll(const QByteArray&);
2017-11-18 01:29:14 +03:00
void disconnectClient(ETcpSocket*);
QList<ETcpSocket*>* getClients();
2017-11-09 23:09:59 +03:00
bool addNode(const QString &node, int port = DEDAULT_PORT);
2017-11-18 01:29:14 +03:00
bool addNode(ETcpSocket* node);
2017-11-11 20:35:30 +03:00
~Node();
2017-11-06 01:33:16 +03:00
signals:
void Error(QString);
2017-11-18 01:29:14 +03:00
void Message(const package&,ETcpSocket*);
void ClientDisconnected(ETcpSocket*);
void ClientConnected(ETcpSocket*);
2017-11-06 01:33:16 +03:00
};
}
#endif // NODE_H