TCPListen.cs 4.1 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using FObjBase;

namespace TCPManager
{
    public class TCPListen
    {
        private List<TCPConn> conns = new List<TCPConn>();
        private Socket sock;
        private IPEndPoint ep = new IPEndPoint(IPAddress.Any, 20006);
        public IPEndPoint LocalEP
        {
            get
            {
                return new IPEndPoint(ep.Address, ep.Port);
            }
            set
            {
                ep.Address = value.Address;
                ep.Port = value.Port;
            }
        }
        /// <summary>
        /// 处理接收的数据。 必须有
        /// </summary>
        public ParsePacketHandler ParsePacket = null;
        /// <summary>
        /// 连接状态改变。 可有可无
        /// </summary>
        public Action<TCPConn> ConnectAction;
        /// <summary>
        /// 长时间没发数据,事件。 可有可无
        /// </summary>
        public Action<TCPConn> OutputIsEmptyWithLongTimeAction;


        public TCPListen(IPEndPoint ep, ParsePacketHandler func)
        {
            LocalEP = ep;
            ParsePacket = func;
            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sock.Blocking = false;
            PollModule.Current.Poll_Config(PollModule.POLL_CONFIG.ADD, new PollModule.PollHandler(OnPoll), TimeSpan.FromMilliseconds(1));
        }
        /// <summary>
        /// 使能,开始监听
        /// </summary>
        public bool Enable
        {
            set
            {
                if (value)
                    StartListen();
                else
                    StopListen();
            }
        }
        private void StartListen()
        {
            sock.Bind(LocalEP);
            sock.Listen(10);
        }
        private void StopListen()
        {
            sock.Close();
        }
        //DateTime dtlast = DateTime.Now;
        //int pollcnt = 0;
        private void OnPoll()
        {
            //pollcnt++;

            //DateTime dt1 = DateTime.Now;
            //if ((dt1 - dtlast).TotalSeconds >= 1)
            //{
            //    Console.WriteLine("TCPListen OnPoll " + ((dt1 - dtlast).TotalMilliseconds/pollcnt).ToString());
            //    dtlast = dt1;
            //    pollcnt = 0;
            //}
            //查新连接----------------------------------------------------------
            try
            {
                List<Socket> s = new List<Socket>();
                s.Add(sock);
                Socket.Select(s, null, null, 0);
                if (s.Count > 0)
                {
                    Socket socket = sock.Accept();

                    TCPConn sconn = new TCPConn(socket);
                    sconn.ParsePacket = ParsePacket;
                    if(ConnectAction!=null)
                        sconn.ConnectEvent += ConnectAction;
                    if(OutputIsEmptyWithLongTimeAction!=null)
                        sconn.OutputIsEmptyWithLongTimeEvent += OutputIsEmptyWithLongTimeAction;
                    sconn.Enable = true;
                    sconn.IsConnected = true;
                    conns.Add(sconn);
                }
            }
            catch (SocketException e)
            {
            }
            //处理当前连接----------------------------------------------------------
            SConns_Poll();
        }
        private void SConns_Poll()
        {
            int cnt = conns.Count();
            int i = 0;
            while (i < cnt)
            {
                TCPConn conn = conns.ElementAt(i);
                if (conn != null)
                {
                    if (conn.OnPoll() < 0)
                    {
                        conn.Enable = false;
                        conns.RemoveAt(i);
                        cnt--;
                        continue;
                    }
                    i++;
                }
                else
                {
                    cnt--;
                    conns.RemoveAt(i);
                }
            }
        }
    }
}