Esempio n. 1
0
        public uint AddUser(AsyncUserToken tUserToken)
        {
            uint id = m_temp_userid;

            mUserDic[id] = tUserToken;

            tUserToken.mUserid = id;
            EndPoint point = tUserToken.ConnectSocket.RemoteEndPoint;

            tUserToken.mRemoteIp = point.ToString().Split(':')[0];
            System.Console.WriteLine("id = {0}, ip = {1}", id, tUserToken.mRemoteIp);

            m_temp_userid++;
            return(id);
        }
Esempio n. 2
0
        internal static void SendSelectHeroFinish(AsyncUserToken token, Dictionary <uint, uint> userhero_Dic)
        {
            NtfSelectHeroFinish selectHeroFinish = new NtfSelectHeroFinish();

            foreach (var item in userhero_Dic)
            {
                PlayerHeroInfo info = new PlayerHeroInfo();
                info.userid = item.Key;
                info.heroid = item.Value;
                selectHeroFinish.select_herolist.Add(info);
            }
            using (MemoryStream stream = new MemoryStream())
            {
                Serializer.Serialize <NtfSelectHeroFinish>(stream, selectHeroFinish);
                Send(token, Protocol.NtfSelectHeroFinish, stream.ToArray());
            }
        }
Esempio n. 3
0
        internal static void SendMatch(AsyncUserToken token, List <uint> playeridlist, uint m_temp_roomid)
        {
            NtfMatch match = new NtfMatch();

            for (int i = 0; i < playeridlist.Count; i++)
            {
                PlayerInfo info = new PlayerInfo();
                info.userid   = playeridlist[i];
                info.nickname = "";
                match.playerinfoList.Add(info);
            }
            match.roomid = m_temp_roomid;
            using (MemoryStream stream = new MemoryStream())
            {
                Serializer.Serialize <NtfMatch>(stream, match);
                Send(token, Protocol.NtfMatch, stream.ToArray());
            }
        }
Esempio n. 4
0
        void ProcessAccept(SocketAsyncEventArgs e)
        {
            Interlocked.Increment(ref m_numConnectedSockets);
            Console.WriteLine("Client connection accepted. There are {0} clients connected to the server",
                              m_numConnectedSockets);

            lock (m_readWritePool)
            {
                AsyncUserToken userToken = m_readWritePool.Pop();
                userToken.ConnectSocket = e.AcceptSocket;
                Console.WriteLine("接入socket = {0}", userToken.ConnectSocket.RemoteEndPoint.ToString());
                bool willRaiseEvent = userToken.ConnectSocket.ReceiveAsync(userToken.receiveArgs);
                if (!willRaiseEvent)
                {
                    ProcessReceive(userToken.receiveArgs);
                }
            }
            StartAccept(e);
        }
Esempio n. 5
0
        public void ProcessReceive(SocketAsyncEventArgs e)
        {
            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                Interlocked.Add(ref m_totalBytesRead, e.BytesTransferred);
                Console.WriteLine("The server has read a total of {0} bytes", m_totalBytesRead);

                AsyncUserToken userToken = e.UserToken as AsyncUserToken;
                userToken.Receive();

                bool willRaiseEvent = userToken.ConnectSocket.ReceiveAsync(e);
                if (!willRaiseEvent)
                {
                    ProcessReceive(e);
                }
            }
            else
            {
                CloseClientSocket(e);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// This method is called whenever a receive or send operation is completed on a socket
        /// </summary>
        /// <param name="e">SocketAsyncEventArg associated with the completed receive operation</param>
        void IO_Completed(object sender, SocketAsyncEventArgs e)
        {
            AsyncUserToken userToken = e.UserToken as AsyncUserToken;

            try
            {
                switch (e.LastOperation)
                {
                case SocketAsyncOperation.Receive:
                    ProcessReceive(e);
                    break;

                default:
                    throw new ArgumentException("The last operation completed on the socket was not a receive or send");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Esempio n. 7
0
File: Room.cs Progetto: weijunhu/vs
        /// <summary>
        /// 初始化房间
        /// </summary>
        /// <param name="useridlist"></param>
        /// <param name="tRoomid"></param>
        public void Init(List <uint> useridlist, uint tRoomid)
        {
            m_useridList = useridlist;
            mRoomid      = tRoomid;

            userhero_Dic.Clear();
            messageQueue.Clear();
            mConnectDic.Clear();
            start      = false;
            frameIndex = 1;
            Console.WriteLine("匹配成功,创建房间");
            for (int i = 0; i < m_useridList.Count; i++)
            {
                userhero_Dic[m_useridList[i]] = 0;
                mConnectDic[m_useridList[i]]  = null;

                AsyncUserToken token = mServer.m_UserManager.GetUser(m_useridList[i]);
                if (token != null)
                {
                    SendMsgManager.SendMatch(token, m_useridList, mRoomid);
                }
            }
        }
Esempio n. 8
0
        void CloseClientSocket(SocketAsyncEventArgs e)
        {
            AsyncUserToken userToken = e.UserToken as AsyncUserToken;

            uint userid = userToken.mUserid;

            lock (m_UserManager)
            {
                m_UserManager.RemoveUser(userid);
            }

            if (userToken.ConnectSocket != null)
            {
                try { userToken.ConnectSocket.Shutdown(SocketShutdown.Both); }
                catch (Exception ee) { Console.WriteLine(ee.ToString()); }
                finally
                {
                    try
                    {
                        userToken.ConnectSocket.Close();
                        userToken.ConnectSocket = null;
                        Interlocked.Decrement(ref m_numConnectedSockets);
                        Console.WriteLine("A client has been disconnected from the server. There are {0} clients connected to the server", m_numConnectedSockets);
                        m_maxNumberAcceptedClients.Release();
                        lock (m_readWritePool)
                        {
                            m_readWritePool.Push(userToken);
                        }
                    }
                    catch (Exception eee)
                    {
                        Console.WriteLine("断开异常");
                        Console.WriteLine(eee.ToString());
                    }
                }
            }
        }
Esempio n. 9
0
        internal static void MessageHandle(AsyncUserToken asyncUserToken, byte tType, byte[] tData)
        {
            using (MemoryStream stream = new MemoryStream(tData))
            {
                Console.WriteLine("收到消息类型 = {0}", (Protocol)tType);
                switch ((Protocol)tType)
                {
                case Protocol.ReqLogin:
                    ReqLogin login = Serializer.Deserialize <ReqLogin>(stream);
                    MessageHandle(asyncUserToken, login);
                    break;

                case Protocol.ReqMatch:
                    ReqMatch match = Serializer.Deserialize <ReqMatch>(stream);
                    MessageHandle(asyncUserToken, match);
                    break;

                case Protocol.ReqSelectHero:
                    ReqSelectHero selectHero = Serializer.Deserialize <ReqSelectHero>(stream);
                    MessageHandle(asyncUserToken, selectHero);
                    break;
                }
            }
        }
Esempio n. 10
0
 internal static void MessageHandle(AsyncUserToken asyncUserToken, ReqSelectHero selectHero)
 {
     asyncUserToken.mServer.m_RoomManager.ReqSelectHero(selectHero);
 }
Esempio n. 11
0
 internal static void MessageHandle(AsyncUserToken asyncUserToken, ReqMatch match)
 {
     asyncUserToken.mServer.m_RoomManager.ReqMatch(match);
 }
Esempio n. 12
0
 internal static void MessageHandle(AsyncUserToken asyncUserToken, ReqLogin login)
 {
     asyncUserToken.mServer.m_UserManager.AddUser(asyncUserToken);
     SendMsgManager.SendLogin(asyncUserToken, login.account, asyncUserToken.mUserid);
 }