// VisionNW.h: interface for the CVisionNW class.
//
//////////////////////////////////////////////////////////////////////

#pragma once

#include <array>
#include <string>

#include "MySocket.h"

#ifdef _DLLCLASS
#undef _DLLCLASS
#endif

#define _DLLCLASS __declspec(dllexport)

constexpr auto MAXSOCKBUF = 4096;
constexpr bool SERVER     = true;
constexpr bool CLIENT     = false;

class _DLLCLASS CVisionNW
{
public:
    // ctor
    CVisionNW();
    virtual ~CVisionNW();

    // methods
    void ResetReceiveBuffer();
    void OnClose(int nErrorCode);
    void OnReceive(int nErrorCode);
    void OnSend(int nErrorCode);
    void OnConnect(int nErrorCode);
    void OnAccept(int nErrorCode);

    // attributes
    bool m_bTraceData{};

protected:
    bool         Send(const std::string& strMsg);
    bool         IsConnectedToServer(void);
    bool         IsServerActive(void);
    bool         IsClientConnected(void);
    void         Disconnect(void);
    bool         Connect(int nPort, const std::string& strServerName, bool bServer = true);
    virtual void ReceiveComm(const char* cpcBuffer, int nSize);
    void         ProcessMessage(void);

    char m_RcvBuff[MAXSOCKBUF];
    bool                         m_bDataComplete{};
    int                          m_nBufferPtr{};

private:
    std::string m_strServerName;
    bool        m_bServerActive{};
    bool        m_bConnectedToServer{};
    bool        m_bClientConnected{};

    CMySocket m_sListenSocket;
    CMySocket m_sConnectSocket;
    bool      m_bConnectionType{SERVER};
    int       m_nPort{};
};
