Example #1
0
        public void ReceiveCb(IAsyncResult ar)
        {
            TCP conn = (TCP)ar.AsyncState;

            if (conn.isUse == false)
            {
                return;
            }
            try
            {
                int count = conn.socket.EndReceive(ar);
                if (count <= 0)
                {
                    Console.WriteLine("Received [" + conn.GetAddress() + "] Disconnection");
                    NMC.instance.CloseTCP(conn);
                    return;
                }
                conn.buffercount += count;
                ProcessData(conn);
                if (conn.isUse && conn.socket != null)
                {
                    conn.socket.BeginReceive(conn.readbuffer, conn.buffercount, conn.BuffRmain(), SocketFlags.None, ReceiveCb, conn);
                }
            }
            catch (Exception e)
            {
                NMC.instance.CloseTCP(conn);
                Console.WriteLine("【Abnormal】  ReceiveCb " + e.Message);
            }
        }
Example #2
0
 private void AcceptCb(IAsyncResult ar)
 {
     try
     {
         Socket sock  = listenfd.EndAccept(ar);
         int    index = NewIndex();
         tcps[index] = new TCP();
         if (index < 0)
         {
             sock.Close();
             Console.WriteLine("【Warning】The Maximum number of connections has been reached");
         }
         else
         {
             TCP conn = tcps[index];
             conn.Init(sock);
             string adr = conn.GetAddress();
             Console.WriteLine("Client connection [" + adr + "] ConnectionPool ConnectionID= " + index);
             conn.socket.BeginReceive(conn.readbuffer, conn.buffercount, conn.BuffRmain(), SocketFlags.None, ReceiveCb, conn);
             listenfd.BeginAccept(AcceptCb, null);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine("AcceptCb Defeated" + e.Message);
     }
 }
Example #3
0
        public void hearBeat()
        {
            long timeNow = Sys.GetTimeStamp();

            for (int i = 0; i < tcps.Length; i++)
            {
                TCP conn = tcps[i];
                if (conn == null)
                {
                    continue;
                }
                if (!conn.isUse)
                {
                    continue;
                }
                if (conn.lastTickTime < timeNow - hearBeatTime)
                {
                    Console.WriteLine("[Heartbeat Cause DisConnection :]" + conn.GetAddress());
                    lock (conn)
                        CloseTCP(conn);
                    Console.WriteLine("DisConnetcion");
                }
            }
        }
Example #4
0
        private void HandleMsg(TCP conn, ProtocolBase protoBase)
        {
            ProtocolBytes bytes = (ProtocolBytes)protoBase;
            // Console.WriteLine("收到" + bytes.ProtocolName().ToString());
            string name  = bytes.Protocol.ToString();
            int    index = name.LastIndexOf("_");

            name = name.Substring(index + 1);
            object[] OB = bytes.GetDecode();
            string   s  = "";

            for (int i = 0; i < bytes.GetDecode().Length; i++)
            {
                s += bytes.GetDecode()[i].ToString();
            }
            //if (s != "HearBeat")
            Console.WriteLine(name + " " + s);
            string methodname = name;

            if (ProtocolSort.FriendProtocol.Contains(bytes.Protocol))
            {
                MethodInfo mm  = HandFriendEventMP.GetType().GetMethod(methodname);
                object[]   obj = new object[] { conn, OB };
                mm.Invoke(HandFriendEventMP, obj);
            }
            else if (bytes.Protocol == Fursion_Protocol.LockStep)
            {
                try
                {
                    MethodInfo mm  = handFPSMsg.GetType().GetMethod(methodname);
                    object[]   obj = new object[] { protoBase };
                    mm.Invoke(handFPSMsg, obj);
                }
                catch (Exception e)
                { Console.WriteLine(e.Message + " 221"); }
            }
            else if (ProtocolSort.MSGProtocol.Contains(bytes.Protocol))
            {
                try
                {
                    MethodInfo mm = handleConnMP.GetType().GetMethod(methodname);
                    if (mm == null)
                    {
                        string str = "[Warning]HandleMsg Don't Have method precessing method in HandleConnMethodPool" + bytes.Protocol;
                        Console.WriteLine(str + methodname);
                    }
                    object[] obj = new object[] { protoBase };
                    mm.Invoke(handleConnMP, obj);
                }
                catch (Exception e)
                { Console.WriteLine(e.Message + "  239"); }
            }
            else if (conn.Player == null || ProtocolSort.ConnectProtocol.Contains(bytes.Protocol) || name == "JSONTEST")
            {
                try
                {
                    MethodInfo mm = handleConnMP.GetType().GetMethod(methodname);
                    if (mm == null)
                    {
                        string str = "【warning】 handlemsg没有处理连接方法";
                        Console.WriteLine(str + methodname);
                        if (conn.Player == null)
                        {
                            ProtocolBytes RestConnect = new ProtocolBytes();
                            RestConnect.SetProtocol(Fursion_Protocol.Reconnect);
                            conn.Send(RestConnect);
                        }
                    }
                    object[] obj = new object[] { conn, protoBase };
                    try
                    {
                        mm.Invoke(handleConnMP, obj);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message + "256");
                    }
                }
                catch (Exception e)
                { Console.WriteLine(e.Message + "  261"); }
            }
            else if (ProtocolSort.TeamProtocol.Contains(bytes.Protocol))
            {
                try
                {
                    MethodInfo mm = HandTeameEventMP.GetType().GetMethod(methodname);
                    if (mm == null)
                    {
                        string str = "【warning】 Don't Method precessing in HandTeameEventMP";
                        Console.WriteLine(str + methodname);
                    }
                    object[] obj = new object[] { conn, protoBase };
                    try
                    {
                        mm.Invoke(HandTeameEventMP, obj);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message + "256");
                    }
                }
                catch (Exception e)
                { Console.WriteLine(e.Message + "  261"); }
            }
            else if (ProtocolSort.LockStepProtocol.Contains(bytes.Protocol))
            {
            }
            else
            {
                try
                {
                    MethodInfo mm = handlePlayerEvent.GetType().GetMethod(methodname);
                    if (mm == null)
                    {
                        if (conn.Player == null)
                        {
                            ProtocolBytes RestConnect = new ProtocolBytes();
                            RestConnect.SetProtocol(Fursion_Protocol.Reconnect);
                            conn.Send(RestConnect);
                        }
                        string str = "[警告]handlemsg没有处理玩家方法";
                        Console.WriteLine(str + methodname);
                    }
                    object[] obj = new object[] { conn, protoBase };
                    Console.WriteLine("[处理玩家消息]" + conn.GetAddress() + name);
                    mm.Invoke(handlePlayerEvent, obj);
                }
                catch (Exception e)
                { Console.WriteLine(e.Message + "  247"); }
            }
        }