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

📄 ctpnet.h

📁 用UDP写的可靠传输程序源代码,非常有借鉴意义,适合互连网通讯
💻 H
📖 第 1 页 / 共 2 页
字号:

    // Returns amount of entries in created sessions information storege
    inline unsigned int GetSessionsCount() {return m_Sessions.size();};

    // Returns amount of entries in large messages storage
    inline unsigned int GetLrgMessagesCount() {return m_LargeCommands.size();};

    // Returns current amount of deliverer threads
    inline unsigned int GetDelThreadsCount() {return m_pDeliverTrds.size();};

    // Returns current amount of busy deliverer threads
    inline unsigned int GetBusyDelThreadsCount() {return m_uBusy;};

    // Returns current amount of deliveries
    inline unsigned int GetDelCount() {return m_Deliveries.size();};

protected:
    // Free buffers of all sent packets
    void FreeSntCommands();

    // Free information about received packets
    void FreeSessions();

    // Free buffers of large packets
    void FreeLargeCommands();

    // Free all planned deliveries
    void FreeDeliveries();

    // Actually send packet pointed with buf to recipient to. Returns true if
    // succeeded and false otherwise
    bool SendPacket(char* buf, unsigned long to);

    // Creates sending and receiving sockets. Returns true if succeeded and
    // false otherwise
    bool CreateSockets();

    // Check the options validity in the given header
    void CheckupOptions(Header& header);

public:
    // Socket for data receiving
    SOCKET m_RecvSocket;

    // Receiving buffer
    char* m_pBuffer;

    // Equals true if server and delivery threads needs to be finished
    bool m_bKill;

    // Maximal amount of delivery threads
    unsigned short m_uMaxDeliverers;

    //Output stream for log building
    ostream* m_pLog;

    // Deliveries (received messages and error information) storage
    DeliveriesList m_Deliveries;

    // Threads handles
    vector<CWinThread*> m_pServerTrds; // Server threads
    CWinThread* m_pDelManTrd; // Delivery manager thread
    vector<CWinThread*> m_pDeliverTrds; // Deliverers threads

    // Critical section for server threads access
    CCriticalSection m_csServerTrds;

    // Critical section for deliverers threads access
    CCriticalSection m_csDeliverTrds;

    // Critical section for deliveries access
    CCriticalSection m_csDeliveries;

    // Critical section for sent packets storage access
    CCriticalSection m_csSntCommands;

    // Critical section for sent recipients access
    CCriticalSection m_csSessions;

    // Critical section for sent large commands storage access
    CCriticalSection m_csLargeCommands;

    // Critical section for network access
    CCriticalSection m_csNetwork;

    // Critical section for log access
    CCriticalSection m_csLog;

    // Stores amount of busy deliverers threads
    unsigned short m_uBusy;

    // Determines is a<b or not, taking overruning in the account (0xffffffff
    // is less than zero)
    inline static bool Less(unsigned __int32 a,unsigned __int32 b) {if (max(a,b)-min(a,b)>0x7fffffff) return !(a<b); else return a<b;}

    // Bit mask which is to be set for confirmations
    static const unsigned __int16 m_iConfirm;

protected:
    // Fills id field of header, refered by head for data, which will be sent
    // to recipient addr. If recipient's address is for broadcasting, then
    // option Broadcast is to be set set beforehand. This function will also
    // set StartSession option, if needed
    void GetNextID(Header& head, IPAddr addr);

    // Returns timeout for the session with workstation addr or for
    // broadcasting, if parameter bcast equals true
    unsigned int GetTimeout(IPAddr addr, bool bcast);

    // Sets timeout to the value of parameter timeout for the session with
    // workstation addr or for broadcasting, if parameter bcast equals true.
    // Value will be set only if current value is zero
    void SetTimeout(IPAddr addr, bool bcast, unsigned int timeout);

    // Type definitions

    // Structures for sent packets
    struct SntCommandInfo {
        // Constructors
        SntCommandInfo():sbBody(*(new SmartBuffer())) {CI=NULL; uCount=0;} // Only for STL compliance
        SntCommandInfo(SmartBuffer& sb, DWORD time, unsigned long to):sbBody(sb) {ipTo=to; uCount=sb.GetPacketsCount(); CI=new CommandInfo[uCount]; for (unsigned int i=0; i<uCount; i++) {CI[i].dwTime=time; CI[i].dwLTime=time;}}

        // Confirms receiving of i-th packet. Returns true if this object can
        // be excluded from sent commands list and false otherwise
        bool Confirm(unsigned int i);

        // Free memory, controlled by this sent command information storage
        inline void Free() {delete[] CI; if (sbBody.GetAutoDel()) delete &sbBody;};

        // Representation of recipients IP address
        unsigned long ipTo;
        // Reference to smart buffer
        SmartBuffer& sbBody;
        // Amount of messages in this command
        unsigned __int32 uCount;

        // Structure for single command info
        struct CommandInfo {
            // Constructor
            CommandInfo() {uResend=1; dwTime=0; dwLTime=0; bConfirmed=false;};
        
            // Increment period between sendings
            void IncResend() {if (uResend<16384) uResend<<=1;}
            // Dead timeout has elapsed
            bool IsDeadTimeout() {return uResend>=256;}

            // Period between sendings
            unsigned int uResend;
            // Creation time
            DWORD dwTime;
            // Last sending (or resending) time
            DWORD dwLTime;
            // Was confirmed or not
            bool bConfirmed;
        };

        // Array of commands' information
        CommandInfo* CI;
    };
    typedef list<SntCommandInfo> SntCommandInfoList;

    // Structures for session description
    struct SessionInfo {
        // Constructor
        SessionInfo() {id=rand()*rand(); timeout=0; received.clear(); minwasset=false;}

        // Type for received messages list
        typedef list<unsigned __int32> RcvList;

        unsigned __int32 id; // Next id
        bool minwasset; // Was minimal id already set or it was not 
        unsigned int timeout; // Timeout
        RcvList received; // Ids of received packets
    };
    typedef map<IPAddr::IPSolid,SessionInfo> SessionsInfo;

    // Structures for storing parts of large packets
    struct LargeCommandInfo {
        // Constructors. Parameters:
        // +) command - command;
        // +) size - amount of data to be stored;
        // +) from - ip address of host, that sends this data;
        // +) id - first packet's id;
        // +) amount - amount of packets left in the message
        LargeCommandInfo(unsigned __int16 command, unsigned __int64 size, unsigned long from, unsigned __int32 id, unsigned __int32 amount) {pRD=new CCTPReceivedData(command,size,from,NULL); this->id=id; uCount=amount; received=new bool[uCount]; for (unsigned int i=0; i<uCount; i++) received[i]=false;};
        LargeCommandInfo() {id=0; uCount=0; received=NULL; pRD=NULL;}; // Only for STL compliance

        // Mark i-th part of commang as received one. Returns true if all parts
        // were received and false otherwise
        inline bool GotPart(unsigned int i);
    
        // Free memory, controlled by this large command information storage
        // (message's body is not destroyed)
        inline void Free() {delete[] received;};

        unsigned __int32 id; // First packet's id
        unsigned __int32 uCount; // Amount of packets for command
        bool* received; // Array of flags, which shows received or not
        CCTPReceivedData* pRD; // Points to received data
    };
    typedef list<LargeCommandInfo> LargeCommandInfoList;

    // Structures for special receivers
    struct SpecialReceiver {
        // Constructor
        SpecialReceiver(unsigned __int16 command, NetReceiver* receiver, DeliveryType type) {this->command=command; this->receiver=receiver; this->type=type;};
        SpecialReceiver() {command=0;receiver=NULL;type=(DeliveryType)NULL;}; // For STL compliance

        unsigned __int16 command; // Command
        NetReceiver* receiver; // Receiver
        DeliveryType type; // Type of delivery to be sent to the receiver
    };
    typedef list<SpecialReceiver> SpecialReceiversList;

    // Storages and other data structures

    // Sessions information storage
    SessionsInfo m_Sessions;

    // Sent commands storage
    SntCommandInfoList m_SntCommands;

    // Large packets storage
    LargeCommandInfoList m_LargeCommands;

    // Points to receiver which will get messages and error information by
    // default (if no special receiver will be present)
    NetReceiver* m_DefReceiver;

    // Special receivers
    SpecialReceiversList m_Receivers;

    // Socket for data sending
    SOCKET m_SendSocket;

    // Local address, used for data receiving
    SOCKADDR_IN m_Local;

    // Port on which to work;
    unsigned short m_uPort;

    // Size of the data in packet
    unsigned __int16 m_uPacketDataSize;

    // Time settings
    Times m_Times;

    // Determines if this workstation is offline or not
    bool m_bSuspended;

    // Returns reference to corresponding session information. Broadcasting
    // session will be returned, if parameter bcast equals true
    SessionInfo& GetSessionInfo(IPAddr addr, bool bcast);
};

// Main server threads function
unsigned int CTPServerFunction(void* pNet);

// Deliveries managers threads function
unsigned int CTPDelManFunction(void* pNet);

// Deliverers threads function
unsigned int CTPDeliverFunction(void* pNet);

⌨️ 快捷键说明

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