TCPCConn.cs 4.31 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
namespace FObjBase
{
    //客户端长时间不通讯,必须发送 Send_HeartBeat
    public class TCPCConn : TCPConn
    {
        enum STATE 
        {
            Connected,
            Not_Connect,
            Connecting,
            Err_Connect    
        }

        private STATE state;
        private DateTime conn_delay_time;

        private IPEndPoint ep = new IPEndPoint(IPAddress.Any, 20006);
潘栩锋's avatar
潘栩锋 committed
25 26 27
        /// <summary>
        /// 远端地址
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
28 29 30 31 32 33 34 35 36 37 38 39 40
        public IPEndPoint RemoteEP
        {
            get
            {
                return new IPEndPoint(ep.Address, ep.Port);
            }
            set
            {
                ep = new IPEndPoint(value.Address, value.Port);
            }
        }
        PollModule.PollHandler onpoll_func;

潘栩锋's avatar
潘栩锋 committed
41 42 43 44
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ipep"></param>
潘栩锋's avatar
潘栩锋 committed
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 139 140 141 142 143
        public TCPCConn(IPEndPoint ipep)
        {
            RemoteEP = ipep;
            onpoll_func = new PollModule.PollHandler(delegate()
            {
                OnPoll();
            });

            ReStart();
        }


        void ReStart() 
        {
            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sock.Blocking = false;
            state = STATE.Not_Connect;
            PollModule.Current.Poll_Config(
                PollModule.POLL_CONFIG.ADD, onpoll_func, TimeSpan.FromMilliseconds(1));
        }
        protected override void Stop()
        {
            base.Stop();
            PollModule.Current.Poll_Config(
                PollModule.POLL_CONFIG.REMOVE, onpoll_func);
            IsConnected = false;
        }
        //DateTime dtlast = DateTime.Now;
        //int pollcnt = 0;
        public override int OnPoll()
        {

            if (!Enable)
                return 0;

            //pollcnt++;

            //DateTime dt1 = DateTime.Now;
            //if ((dt1 - dtlast).TotalSeconds >= 1)
            //{
            //    Console.WriteLine("TCPCConn OnPoll " + ((dt1 - dtlast).TotalMilliseconds / pollcnt).ToString());
            //    dtlast = dt1;
            //    pollcnt = 0;
            //}
            switch (state)
            {
                case STATE.Not_Connect:
                    try
                    {
                        sock.Connect(RemoteEP);
                    }
                    catch (SocketException e)
                    {
                        if (e.SocketErrorCode != SocketError.WouldBlock)
                        {
                            Debug.Print(this.GetType().ToString() + " " + DateTime.Now.ToLongTimeString() + " " + "OnPoll() e.SocketErrorCode=" + e.SocketErrorCode.ToString());
                            conn_delay_time = DateTime.Now;
                            state = STATE.Err_Connect;
                            break;
                        }
                    }
                    conn_delay_time = DateTime.Now;
                    state = STATE.Connecting;
                    break;
                case STATE.Connecting:
                    if (sock.Connected)
                    {
                        state = STATE.Connected;
                        first_poll = true;
                        Init();
                        //连接成功
                        IsConnected = true;
                    }
                    else if (sock.Poll(0, SelectMode.SelectError))
                    {
                        state = STATE.Not_Connect;
                    }
                    break;
                case STATE.Connected:
                    {
                        if (base.OnPoll() < 0)
                        {
                            Enable = true;
                            ReStart();
                            conn_delay_time = DateTime.Now;
                            state = STATE.Err_Connect;
                        }
                        break;
                    }
                case STATE.Err_Connect:
                    if ((DateTime.Now - conn_delay_time) > TimeSpan.FromMilliseconds(1000))
                        state = STATE.Not_Connect;
                    break;
            }
            return 0;
        }

    }
}