#ifndef TCPCLIENT_H
#define TCPCLIENT_H

#include <QObject>
#include <QTimer>
#include <QHostAddress>
#include <QTcpSocket>
#include <QUdpSocket>
#include <QTcpServer>
#include <QThread>

#include "qbindtcpsocket.h"

class ConnectDetectThread;


class TcpClient : public QObject
{
    Q_OBJECT
public:
    explicit TcpClient(QObject *parent = NULL);
    virtual ~TcpClient();

public:
    void setConfigFileName(QString configfilename);
signals:
    void siConnectSta(int sta);          // 网络连接信号
    void siConnectErr(QString errinfo);  // 网络错误信息
    void siReceiveData(QByteArray dat);  // 收到数据包信号
signals:
    void siDetectHost(void);
public:


public slots:
    void slotSendData(QByteArray dat);  // 发送数据的槽
    void connectToServer();//连接到服务器
    void disConnectFromServer();
    void detectStatus(int);
public:

private slots:
    void connectCheck();
    void receiveServerData();
    void displaySocketError(QAbstractSocket::SocketError err);
    void netStateChanged(QAbstractSocket::SocketState sta);
    void hostFound();

private:
    void loadIpAndPort();
private:
    QBindTcpSocket * m_pClientSocket;//客户端

    QTimer * m_pConnectCheckTimer;

public:
    int m_connected;            // 连接状态; 0, 初始化; 1, 未连接; 2, 连接中; 3, 已连接

private:
    QThread * m_pConnectDetectThread;
    TcpConnectDetect * m_pConnectDetect;
    int m_detect;               // 连接测试状态
private:
    QString m_configFileName;
private:
    QString m_localip;
    quint16 m_localport;
    QString m_serverip;
    quint16 m_serverport;
};

#if(0)
class TcpConnectDetect : public QObject
{
    Q_OBJECT
public:
    explicit TcpConnectDetect(QObject *parent = NULL);
    virtual ~TcpConnectDetect();

signals:
    void siConnectSta(int sta);          // 网络连接信号
public:

public slots:
    void detectStart();
    int connectDetect();

private:
    QBindTcpSocket * m_pDetectSocket;

public:
    void setIpAndPort(QString localip, quint16 localport, QString serverip, quint16 serverport);

private:
    QString m_localip;
    quint16 m_localport;
    QString m_serverip;
    quint16 m_serverport;
};
#endif

#endif // TCPCLIENT_H
