net interface

#ifndef _INETPORT_H_
#define _INETPORT_H_

#include <WINSOCK2.H>
#pragma comment(lib,"ws2_32")

typedef enum
{
    PORT_TYPE_USB = 0,
    PORT_TYPE_UDP = 1,
    PORT_TYPE_TCP = 2
} PORT_TYPE;

//串口的端口状态定义
enum {
    PORT_STATUS_NORMAL, PORT_STATUS_REMOVAL, PORT_STATUS_CLOSE
};

////////////////////////////////////////////////////////
//通信端口的接口类,上层完全通过此接口来访问串行通信模块
class INetPort
{
public:
    //向串口/网口写入一串数据,输入数据缓冲区地址和长度
    virtual BOOL Write(PBYTE pOutBuffer, DWORD dwLength, DWORD dwTimeOut) = 0;

    //从串口/网口读出指定长度的数据,给出缓冲区首址和希望接收长度
    virtual BOOL Read(PBYTE pInBuffer, DWORD dwBufferLen, DWORD &rdwRealLen, DWORD dwTimeOut) = 0;

    //关闭串口/网口
    virtual BOOL Close() = 0;

    //配置串口/网口,供Open操作获取到必须的设置项
    virtual BOOL SetConfig(const CString &sAddr, USHORT uPort) = 0;

    //打开串口/网口
    virtual BOOL Open() = 0;

    //返回类型
    virtual PORT_TYPE GetPortType() = 0;

    //清空通信设备的发送和接收缓冲区,并使所有阻塞操作返回
    virtual BOOL Purge()
    {
        return TRUE;
    }

    //监测端口的连接状态
    virtual ULONG RegisterNotification()
    {
        return PORT_STATUS_NORMAL;
    }

    //获得最后一次的错误码
    DWORD GetLastError()
    {
        return m_dwErrorCode;
    }

    // 构造函数
    INetPort ()
    {
        m_dwErrorCode = ERROR_SUCCESS;
    }

    //析构函数
    virtual ~INetPort ()
    {}

protected:
    DWORD m_dwErrorCode; //最近一个错误的错误码
};

#include "HOST_Util.h"
class CWsaEvent : private NonCopyable
{
public:
    CWsaEvent ()
    {
        m_hEvent = WSACreateEvent();
    }
    ~CWsaEvent ()
    {
        if (WSA_INVALID_EVENT != m_hEvent)
        {
            WSACloseEvent(m_hEvent);
        }
    }//lint !e1740
    BOOL SetEvent()
    {
        _ASSERTE(m_hEvent != NULL);
        return ::WSASetEvent(m_hEvent);
    }
    BOOL ResetEvent()
    {
        _ASSERTE(m_hEvent != NULL);
        return ::WSAResetEvent(m_hEvent);
    }
    WSAEVENT m_hEvent;
};

#endif

你可能感兴趣的:(net interface)