2016-05-17 03:04:11 +12:00
|
|
|
#pragma once
|
|
|
|
|
2016-03-19 06:57:51 +13:00
|
|
|
#ifndef TTCPIP_H
|
|
|
|
#define TTCPIP_H
|
|
|
|
|
2016-04-14 22:15:09 +12:00
|
|
|
#include <memory>
|
|
|
|
|
2016-03-19 06:57:51 +13:00
|
|
|
#include "tcommon.h"
|
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
|
|
|
|
#ifdef TFARMAPI
|
|
|
|
#undef TFARMAPI
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#ifdef TFARM_EXPORTS
|
|
|
|
#define TFARMAPI __declspec(dllexport)
|
|
|
|
#else
|
|
|
|
#define TFARMAPI __declspec(dllimport)
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define TFARMAPI
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
|
|
|
|
class TTcpIpServerImp;
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class TFARMAPI TTcpIpServer : public QThread {
|
|
|
|
int m_exitCode;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
TTcpIpServer(int port);
|
|
|
|
virtual ~TTcpIpServer();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int getPort() const;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void run();
|
|
|
|
int shutdown();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
virtual void onReceive(int socket, const QString &data) = 0;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void sendReply(int socket, const QString &reply);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int getExitCode() const;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
private:
|
2016-06-15 18:43:10 +12:00
|
|
|
std::shared_ptr<TTcpIpServerImp> m_imp;
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
|
|
|
|
enum {
|
2016-06-15 18:43:10 +12:00
|
|
|
OK,
|
|
|
|
STARTUP_FAILED,
|
|
|
|
HOST_UNKNOWN,
|
|
|
|
SOCKET_CREATION_FAILED,
|
|
|
|
CONNECTION_FAILED,
|
|
|
|
CONNECTION_REFUSED,
|
|
|
|
CONNECTION_TIMEDOUT,
|
|
|
|
SEND_FAILED,
|
|
|
|
RECEIVE_FAILED
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class TFARMAPI TTcpIpClient {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
TTcpIpClient();
|
|
|
|
~TTcpIpClient();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int connect(const QString &name, const QString &addr, int port, int &sock);
|
|
|
|
int disconnect(int sock);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int send(int sock, const QString &data);
|
|
|
|
int send(int sock, const QString &data, QString &reply);
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|