⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netbasic.h

📁 用UDP写的可靠传输,非常有借鉴意义,适合互连网通讯
💻 H
字号:
// IPAddr represents workstations network address
// NetSender - Interface for network sending provider
// NetReceiver - Interface for network receiving provider
// Declaration file
//
// (c) Lev Naumov, CAMEL Laboratory
// E-mail: camellab@mail.ru
// For more information see http://camel.ifmo.ru or
// http://www.codeproject.com/internet/ctp.asp
/////////////////////////////////////////////////////////////////////////////

#include "SmartBuffer.h"

union IPAddr
{
    // Data type for solid representation
    typedef unsigned __int32 IPSolid;

    // Actual data
    struct IPBytes {
        unsigned char b1,b2,b3,b4;
    } Bytes;
    IPSolid Solid;

    // Constructors
    IPAddr() {SetLocalhost();};
    IPAddr(unsigned char b1,unsigned char b2,unsigned char b3,unsigned char b4) {Bytes.b1=b1;Bytes.b2=b2;Bytes.b3=b3;Bytes.b4=b4;};
    IPAddr(IPSolid l) {Solid=l;};

    // Returns true is this ip-address refers to localhost (127.0.0.1)
    inline bool IsLocalhost() {return Bytes.b1==127&&Bytes.b2==0&&Bytes.b3==0&&Bytes.b4==1;};

    // Returns true is this ip-address refers to broadcasting address
    // (255.255.255.255)
    inline bool IsBroadcasting() {return Bytes.b1==255&&Bytes.b2==255&&Bytes.b3==255&&Bytes.b4==255;};

    // Returns via s and return value dotted string representation of ip-address
    inline LPTSTR GetString(LPTSTR s) {sprintf(s,"%d.%d.%d.%d",Bytes.b1,Bytes.b2,Bytes.b3,Bytes.b4); return s;};

    // Set stored ip address to value, represented with string s (in
    // dot-separated format). Returns true if succeeded and false otherwise
    bool FromString(LPTSTR s);

    // Set ip address to localhost (127.0.0.1)
    inline void SetLocalhost() {Bytes.b1=127;Bytes.b2=0;Bytes.b3=0;Bytes.b4=1;};

    // Set ip address to broadcasting address (255.255.255.255)
    inline void SetBroadcast() {Bytes.b1=255;Bytes.b2=255;Bytes.b3=255;Bytes.b4=255;};

    // Operators
    bool operator ==(unsigned long ip) {return Solid==ip;};
    bool operator ==(IPAddr ip) {return Solid==ip.Solid;};
    bool operator !=(unsigned long ip) {return Solid!=ip;};
    bool operator !=(IPAddr ip) {return Solid!=ip.Solid;};
    IPAddr& operator =(const unsigned long ip) {Solid=ip; return *this;};
    IPAddr& operator =(const IPAddr ip) {Solid=ip.Solid; return *this;};
};

class NetSender
{
public:
    // Send smart buffer sb to address to. Parameter command represents command
    // id. Parameter options - sending options. If parameter storeiffail is
    // true then sent command will be stored in sent packets storage even if
    // sending fails and will not overwise. Returns true if succeeded (message
    // has gone) and false otherwise
    virtual bool Send(SmartBuffer& sb, unsigned __int16 command, IPAddr to, unsigned __int8 options=0, bool storeiffail=true)=0;

    // Returns true if sender is working (is not suspended and so on) and false
    // otherwise. Default implementation returns true, because implementation
    // of many protocols cannot be switched off and cannot handle errors at all
    virtual bool IsWorking() {return true;};
};

class NetReceiver
{
public:
    // Is called when have received data pointed by data
    virtual void OnReceive(void* data)=0;

    // Is called when there was an error, described with data pointed by data
    virtual void OnError(void* data)=0;
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -