using JT1078.Gateway.Abstractions.Enums;
using JT1078.Gateway.Abstractions;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace JT1078.Gateway.Sessions
{
    public class JT1078TcpSession : IJT1078Session
    {
        public JT1078TcpSession(Socket client)
        {
            Client = client;
            RemoteEndPoint = client.RemoteEndPoint;
            ActiveTime = DateTime.Now;
            StartTime = DateTime.Now;
            SessionID = Guid.NewGuid().ToString("N");
            ReceiveTimeout = new CancellationTokenSource();
        }
        /// <summary>
        /// 终端手机号
        /// </summary>
        public string TerminalPhoneNo { get; set; }
        public DateTime ActiveTime { get; set; }
        public DateTime StartTime { get; set; }
        public JT1078TransportProtocolType TransportProtocolType { get;} = JT1078TransportProtocolType.tcp;
        public string SessionID { get; }
        public Socket Client { get; set; }
        public CancellationTokenSource ReceiveTimeout { get; set; }
        public EndPoint RemoteEndPoint { get ; set; }
        public void Close()
        {
            try
            {
                Client.Shutdown(SocketShutdown.Both);
            }
            catch { }
            finally
            {
                Client.Close();
            }
        }
    }
}
