Beispiel #1
0
        public void Start(int port)
        {
            if (m_HasInit)
            {
                return;
            }
            string mLocalIP = "";

            string      mHostName = Dns.GetHostName();
            IPHostEntry localHost = Dns.GetHostEntry(mHostName);

            for (int i = 0; i < localHost.AddressList.Length; ++i)
            {
                if (localHost.AddressList[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    //MonoBehaviour.print(localHost.AddressList[i].ToString());
                    mLocalIP = localHost.AddressList[i].ToString();
                    break;
                }
            }

            if ("".Equals(mLocalIP))
            {
                MonoBehaviour.print("网络检测异常。请检查网络设置或接入网络");
                return;
            }
            BaseInit();
            m_MsgOrder = new Queue <int>();

            //服务器IP地址
            IPAddress ip = IPAddress.Parse(mLocalIP);

            m_ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            m_ServerSocket.Bind(new IPEndPoint(ip, port)); //绑定IP地址:端口
            m_ServerSocket.Listen(10);                     //设定最多10个排队连接请求
            MonoBehaviour.print("游戏网络启动监听" + m_ServerSocket.LocalEndPoint.ToString());

            m_ServerSocketList = new System.Collections.ArrayList();
            m_listenSocketList = new System.Collections.ArrayList();
            m_DeleteSocketList = new System.Collections.ArrayList();

            m_ConnectPool  = new ClientConnect[m_MaxClientConnect];
            m_EmptyConnect = new Queue <int>();
            for (int i = 0; i < m_MaxClientConnect; ++i)
            {
                m_ConnectPool[i] = new ClientConnect();
                m_EmptyConnect.Enqueue(i);
            }
            //通过Clientsoket发送数据
            m_ReciveThread = new Thread(ReceiveMessage);
            m_ReciveThread.Start();
            m_LinstenThread = new Thread(ListenClientConnect);
            m_LinstenThread.Start();
        }
Beispiel #2
0
 public void SendToAll(ref MsgPack msg)
 {
     try
     {
         Lock();
         for (int i = 0; i < m_MaxClientConnect; ++i)
         {
             ClientConnect curPlayer = m_ConnectPool[i];
             if (null != curPlayer.m_Connect)
             {
                 curPlayer.m_Connect.Send(msg.GetByte(), msg.GetByteLen(), SocketFlags.None);
                 break;
             }
         }
     }
     finally
     {
         UnLock();
     }
 }
Beispiel #3
0
 /// <summary>
 /// 获取一个数据
 /// </summary>
 public void GetMsg(ref ClientMsgUnPack msg)
 {
     if (!m_HasInit)
     {
         return;
     }
     try
     {
         Lock();
         if (0 != m_MsgOrder.Count)
         {
             int           mSlot       = m_MsgOrder.Dequeue();
             ClientConnect curPlayer   = m_ConnectPool[mSlot];
             ushort        mOnePackLen = System.BitConverter.ToUInt16(curPlayer.m_AllData, curPlayer.m_AllDataHead);
             msg = new ClientMsgUnPack(curPlayer.m_AllData, (ushort)curPlayer.m_AllDataHead, (ushort)mOnePackLen, (int)curPlayer.m_UserID);
             curPlayer.m_AllDataHead += mOnePackLen;
         }
     }
     finally
     {
         UnLock();
     }
 }
Beispiel #4
0
        private bool PutDataToBuff(byte[] mClientSendBuff, int mReceiveNumber, Socket client)
        {
            ClientConnect curPlayer = null;
            int           mSlot     = -1;

            for (int i = 0; i < m_MaxClientConnect; ++i)
            {
                if (client == m_ConnectPool[i].m_Connect)
                {
                    curPlayer = m_ConnectPool[i];
                    mSlot     = i;
                    break;
                }
            }
            if (null == curPlayer)
            {
                return(false);
            }
            if (curPlayer.m_AllDataEnd + mReceiveNumber >= LanSocketBase.m_MaxAllBuff)
            {
                byte[] mCurAllData = new byte[curPlayer.m_AllDataEnd - curPlayer.m_AllDataHead];
                System.Buffer.BlockCopy(curPlayer.m_AllData, curPlayer.m_AllDataHead, mCurAllData, 0, curPlayer.m_AllDataEnd - curPlayer.m_AllDataHead);
                System.Buffer.BlockCopy(mCurAllData, 0, curPlayer.m_AllData, 0, curPlayer.m_AllDataEnd - curPlayer.m_AllDataHead);
                curPlayer.m_AllDataEnd -= curPlayer.m_AllDataHead;
                curPlayer.m_AllDataHead = 0;

                if (curPlayer.m_AllDataEnd + mReceiveNumber >= LanSocketBase.m_MaxAllBuff)
                {
                    return(false);
                }
            }
            int mOnePackStartPos = 0;

            while (mReceiveNumber > 0)
            {
                if (0 == curPlayer.m_OnePackIndex)
                {
                    ushort datalen = System.BitConverter.ToUInt16(mClientSendBuff, mOnePackStartPos);
                    if (datalen > LanSocketBase.m_MaxOnePackBuff || datalen < LanSocketBase.m_HeadSize)
                    {
                        return(false);
                    }
                    if (datalen <= mReceiveNumber)
                    {
                        System.Buffer.BlockCopy(mClientSendBuff, mOnePackStartPos, curPlayer.m_AllData, curPlayer.m_AllDataEnd, datalen);
                        curPlayer.m_AllDataEnd += datalen;
                        mOnePackStartPos       += datalen;

                        mReceiveNumber -= datalen;

                        m_MsgOrder.Enqueue(mSlot);
                    }
                    else
                    {
                        System.Buffer.BlockCopy(mClientSendBuff, mOnePackStartPos, curPlayer.m_OnePack, curPlayer.m_OnePackIndex, mReceiveNumber);
                        curPlayer.m_OnePackIndex += mReceiveNumber;
                        mOnePackStartPos         += mReceiveNumber;

                        mReceiveNumber -= mReceiveNumber;
                    }
                }
                else
                {
                    if (curPlayer.m_OnePackIndex < 2)
                    {
                        System.Buffer.BlockCopy(mClientSendBuff, mOnePackStartPos, curPlayer.m_OnePack, curPlayer.m_OnePackIndex, 1);

                        ++curPlayer.m_OnePackIndex;
                        --mReceiveNumber;
                        ++mOnePackStartPos;
                    }
                    ushort datalen = System.BitConverter.ToUInt16(curPlayer.m_OnePack, 0);
                    if (datalen > LanSocketBase.m_MaxOnePackBuff || datalen < LanSocketBase.m_HeadSize)
                    {
                        return(false);
                    }
                    if (curPlayer.m_OnePackIndex + mReceiveNumber >= datalen)
                    {
                        int mNeedNum = datalen - curPlayer.m_OnePackIndex;
                        System.Buffer.BlockCopy(mClientSendBuff, mOnePackStartPos, curPlayer.m_OnePack, curPlayer.m_OnePackIndex, mNeedNum);
                        mOnePackStartPos += mNeedNum;

                        System.Buffer.BlockCopy(curPlayer.m_OnePack, 0, curPlayer.m_AllData, curPlayer.m_AllDataEnd, datalen);
                        curPlayer.m_OnePackIndex = 0;
                        curPlayer.m_AllDataEnd  += datalen;

                        mReceiveNumber -= mNeedNum;

                        m_MsgOrder.Enqueue(mSlot);
                    }
                    else
                    {
                        System.Buffer.BlockCopy(mClientSendBuff, mOnePackStartPos, curPlayer.m_OnePack, curPlayer.m_OnePackIndex, mReceiveNumber);
                        curPlayer.m_OnePackIndex += mReceiveNumber;
                        mOnePackStartPos         += mReceiveNumber;

                        mReceiveNumber -= mReceiveNumber;
                    }
                }
            }

            return(true);
        }