51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2538|回复: 0
打印 上一主题 下一主题

游戏编程系统4----网络

[复制链接]
  • TA的每日心情
    慵懒
    2015-1-8 08:46
  • 签到天数: 2 天

    连续签到: 1 天

    [LV.1]测试小兵

    跳转到指定楼层
    1#
    发表于 2007-11-23 14:50:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    做游戏,服务端不用IOCP的是笨蛋,客户端用了IOCP的是笨蛋,除非你告诉我你的客户端不在98上面跑...

    关于IOCP网上介绍的太多了,也有实现,都很简单.没有完整可以支撑带运作的.
    再来一段韩国代码吧.

    ---------
    从没说过爱着谁
    为谁而憔悴
    从来没有想过对不对
    我的眼中装满疲惫
    面对自己总觉得好累

    我也需要人来陪
    不让我心碎
    让我爱到深处不后悔
    其实我并不像他们说的
    那样多刺难以安慰

    爱人的心应该没有罪
    为何在夜里却一再流泪
    每天抱着寂寞入睡
    生活过得没有滋味
    ----
    别问我是谁
    请与我相恋
    我的真心没人能够体会
    像我这样的人不多
    为何还要让我难过
    ----
    别问我是谁
    请和我面对
    看看我的眼角流下的泪
    我和你并没有不同
    但我的心更容易破碎
    --------



    /********************************************************************
    created: 2006/03/07
    created: 7:3:2006   1:52
    filename:  d:\myc\GameServer\NetWork\iocpnet.h
    file path: d:\myc\GameServer\NetWork
    file base: iocpnet
    file ext: h
    author:  kuchao

    purpose: GameServer NetWork define
    *********************************************************************/
    #pragma once
    #include <WinSock2.h>
    //#include <atlbase.h>
    #include "protocol.h"

    struct OVERLAPPED_BASE : public WSAOVERLAPPED
    {
    OVERLAPPED_BASE()
    {
      Internal = 0;
      InternalHigh = 0;
      Offset = 0;
      OffsetHigh = 0;
      hEvent = 0;
      m_iClient = 0;
      m_sEvent = IOCP_EVENT_NONE;
    }
    USHORT     m_sEvent;     //IOCPs Event
    //IOCP_EVENT_NONE        
    //IOCP_EVENT_READ
    //IOCP_EVENT_WRITE
    WORD     m_wID;      //Current ID (optional uses)
    //when writing, m_wID is the array number of writing queue
    UINT     m_iClient;     //Client array ID
    UINT     m_iWritten;    //written size (used in  write process)
    };

    /************************************************************************************

    struct SXClientBuffer

    */
    #define XBUFFER_EMPTY  (0)
    //for client read & write
    struct SXClientBuffer
    {
    SXClientBuffer() {
      m_dwSize=0;
      m_szBuffer = NULL;
      m_dwTotalBufferSize = 0;
      m_dwSize_Sent = 0;
      m_bIOPending = FALSE;
    }
    ~SXClientBuffer() {
      delete [] m_szBuffer;
    }

    void DeleteBuffer() {
      if (m_szBuffer != NULL)
       delete [] m_szBuffer;
      m_szBuffer = NULL;
      m_dwSize = 0;
    }

    void CreateBuffer(int buffersize) {
      if (m_szBuffer == NULL) {
       m_szBuffer = new BYTE[buffersize];
       m_dwTotalBufferSize = buffersize;
       m_bIOPending = FALSE;
      }
    }

    OVERLAPPED_BASE m_Overlapped;       //overlapped
    DWORD   m_dwTotalBufferSize;
    DWORD   m_dwSize;        //current size of buffer filled
    DWORD   m_dwSize_Sent;
    BOOL   m_bIOPending;
    BYTE*   m_szBuffer;
    };


    /****************************************************************************************

    struct SIOCPPacketHeader
    union _header
    wID[0] : Msg Number (verifications)
    wID[1] : Msg Count;

    dwID   : Unique ID of The message

    total of 256 messages for uses


    */
    //#define PACKET_HEADER  (0)
    //#define PACKET_COUNT  (1)
    //union _header
    //{
    // WORD wID[2];
    // DWORD   dwID;
    //};
    /****************************************************************************************

    class CSClient

    */
    #define CLIENT_COOL_TIME 5000   //socket cool time (not acceptable in cool time)   
    class CXClient
    {
    public:
    CXClient();        //construction
    ~CXClient();

    bool Init(int buffersize,int WritebufferSize); //init buffers
    void Close(int closenumber);         //close socket

    BOOL  Read(WORD wSize = MAX_ONETIME_RECEIVE_BUFFER);     //set read in queue

    SOCKET GetSocket() {return m_Socket;}
    UINT   GetClientNumber() {return m_iClient;}

    void   SetClientNumber(UINT number) {m_iClient = number;}
    void   SetSocket(SOCKET socket) {m_Socket = socket;}
    void   SetConnectFlag(BOOL bConn) {m_bConnected = bConn;}
    BOOL   IsConnected() {return m_bConnected;}

    //write and read operation****************************************************************
    int   Write(BYTE* szBuffer,DWORD dwSize,BOOL bExtra = FALSE); //write in queue
    void     WriteSocket();            //write into IOCPs
    void     WriteFinish(int sent);          //write finished
    CRITICAL_SECTION  m_csSWriteflag;
    void EnterWriteCS()  {EnterCriticalSection(&m_csSWriteflag);}
    void LeaveWriteCS()  {LeaveCriticalSection(&m_csSWriteflag);}

    SXClientBuffer*      m_XReceiveBuffer;  //receive buffer
    SXClientBuffer*      m_XSendBuffer;   //send buffer
    public:

    WORD   m_wClientType;

    DWORD   m_dwCoolTime;
    DWORD   m_dwLastReceviedCount; //latency receive count
    DWORD   m_dwLastReceivedTime; //latency
    TCHAR   m_szPeerName[25];

    DWORD   GetInitCnt() {return m_dwInitCnt;}


    int    m_iFakeClient;   //real sock num
    bool   m_bFakeConnect;   //real sock connected

    protected:
    SOCKET   m_Socket;    //socket
    UINT   m_iClient;    //Current Client array number
    BOOL   m_bConnected;
    BOOL   m_bCencelIO;
    long   m_lPort;
    DWORD   m_dwInitCnt;

    };


    struct _SocInfo
    {
    BOOL bListen;
    int  nIndex;
    TCHAR szTitle[25];
    TCHAR szLocalIP[25];
    TCHAR szIP[25];
    int  nPort;
    int  nRecvSockBuffer;
    int  nSendSockBuffer;
    int  nReadQueueBuffer;
    int  nSendQueueBuffer;
    };

    #define FILE_CONNECT L"connect.txt"
    class CIOCPNet;
    class CMultiToken
    {
    public:
    CMultiToken(){}
    ~CMultiToken();

    BOOL Connect(CIOCPNet* pNet);
    void KeepAlive();
    int  DisConnect(int nClient);
    void SendReady(int iClient,int nServer,BOOL bSend = FALSE);
    BOOL Analize_ConnectData();
    virtual void OnConnected(int /*iClient*/, int /*nServer*/) {}
    virtual void OnDisConnected(int /*iClient*/, int /*nServer*/) {}
    void Connected(int iClient, int nServer);
    void DisConnected(int iClient, int nServer);

    int GetServerNum() {return m_nServer;}
    int GetTotalServerCount() {return m_nSocInfoCount;}


    //CXCLIENT 鉴辑
    /*
    Listen SocInfo 1锅何磐 俊 蝶扼 1何磐 矫累茄促
    Listen SocInfo 饶何磐 Connect 鉴辑俊 蝶扼 1何磐 矫累茄促
    Listen SocInfo 0 篮 蜡历侩烙栏肺 BRAVO_MAX_MULTI_SERVER 何磐 矫累茄促
    */
    _SocInfo  m_SocInfo[SOCKET_MAX_LISTEN];

    int   m_nConnectSocketCount;
    int   m_nListenSocketCount;
    int   m_nSocInfoCount;

    int   m_nFirstlistencount;
    int   GetFirstConnet();

    int   m_nZoneservercount;
    int   m_nGatewaycount;
    int   m_nStartGateway;

    int   m_nExceptLiveserver;

    TCHAR m_chCaptionName[20];

    BOOL  m_arrSockAlive[SOCKET_MAX_LISTEN];
    void Stop() {
      DWORD dwExit = 5;
      TerminateThread(m_hThread,dwExit);}
    protected:

    CIOCPNet* m_pIOCPNet;         
    HANDLE m_hThread;      
    DWORD  m_dwThreadID;      

    int  m_iConnCount;        //
    int  m_nServer;               //郴 辑滚锅龋
    };


    class CIOCPToken
    {
    public:
    CIOCPToken() {}
    ~CIOCPToken() {}
    void Connected(int iClient);
    void DisConnected(int iClient);

    protected:

    virtual void OnConnected(int /*iClient*/)  {}
    virtual void OnDisconnected(int /*iClient*/) {}
    };


    /*

    CIOCPNet

    Windows MMORPG server engine using Windows IOCPs

    */
    class CIOCPNet
    {
    public:
    //const & dist
    CIOCPNet();
    ~CIOCPNet();

    bool Init(CIOCPToken* pToken, CMultiToken* pMultiToken = NULL);
    bool StartUp();
    BOOL    Process();
    BOOL Accept();
    BOOL Accept(SOCKET listen,int nNum,int nRecvSockBuffer,int nSendSockBuffer, int nReadQueueBuffer,int nSendQueueBuffe);
    void CloseClient(int iClient);
    int     Connect(TCHAR* szPeerName, long port,int nClient ,TCHAR* szLocalIP ,int SocReadBuffer,int SocWriteBuffer
      ,int ReadQueueBuffer,int SendQueueBuffer);
    DWORD   GetMsgSize(_header Header,UINT iClient);
    void GetStatus(SYSTEM_INFO* info,int* iThreads,int* iClients)
    {
      *info  = m_SysInfo;
      *iThreads   = m_iThreads;
      *iClients = m_iClients;
    }
    BOOL   InitListen(long port,WORD listen,TCHAR* LocalIP=NULL);

    int    GetClientCount() {return m_iClients;}
    void   GetLocalName(TCHAR* addr = NULL);
    // gr 6_28
    CMultiToken* GetMultiToken() {return m_pMultiToken;}   // 楷搬窍绊磊 窍绰 辑滚狼 CMultiToken狼 器牢磐蔼阑 啊咙
    BOOL SockOptionSet(SOCKET soc,int nNum,int nRecvSockBuffer,int nSendSockBuffer);

    protected:
    int   m_iClients;     //client counts

    SYSTEM_INFO m_SysInfo;

    int   m_iThreads;     // 醚 _xThread狼 静贰靛荐甫 啊咙
    DWORD  m_dwCurrentTime;

    CIOCPToken*  m_pToken;        // 楷搬窍绊磊 窍绰 辑滚狼 CIOCPToken狼 器牢磐蔼阑 啊咙
    CMultiToken* m_pMultiToken;   // 楷搬窍绊磊 窍绰 辑滚狼 CMultiToken狼 器牢磐蔼阑 啊咙

    HWND   m_hParents;        // 促捞舅肺弊 HWND
    HANDLE m_hThreads[IOCP_THREADS_PER_CPU];   // _xThread狼 静贰靛甫 cpu 俺荐 * 2甫 父惦
    HANDLE m_hThreadAccept;
    HANDLE m_hIOCP;        // iocp勤甸
    long   m_nPort;      
    SOCKET m_ListenSoc[SOCKET_MAX_LISTEN];  // 瘤陛篮 0捞搁 努扼捞攫飘侩 府郊家南  1捞搁 辑滚侩 府郊家南

    void   AnalyizeMsg(UINT iClient,DWORD dwSize);
    BOOL   m_bPause;
    BOOL   m_bOperate;
    // :fstream  m_IOCPclosenumber;

    int  GetEmptyClient();

    public:
    void Stop();
    void EnterCriticalListen(){EnterCriticalSection(&m_ListenFlag);}
    void LeaveCriticalListen(){LeaveCriticalSection(&m_ListenFlag);}
    void EnterCriticalSListen(){EnterCriticalSection(&m_ListenFlag_Server);}
    void LeaveCriticalSListen(){LeaveCriticalSection(&m_ListenFlag_Server);}
    CRITICAL_SECTION m_ListenFlag;
    CRITICAL_SECTION m_ListenFlag_Server;
    };
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-22 12:51 , Processed in 0.074811 second(s), 27 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表