using System;
using System.IO;
using System.Net;

namespace Server.Net
{
    public enum ChannelType
    {
        Connect,
        Accept,
    }
    
    public struct Packet
    {
        public const int MinPacketSize = MessageIndex + OpcodeLength;
        public const int ActorIdLength = 3;
        public const int OpcodeLength = 4;
        public const int MessageIndex = ActorIdLength + ActorIdLength;

        public ushort Opcode;
        public MemoryStream MemoryStream;
    }

    public struct RouterPacket
    {
        /// <summary>
        /// 总长度
        /// </summary>
        public const int TotalLength = 4;

        /// <summary>
        /// 路由包长度
        /// </summary>
        public const int RouterPacketLength = 4;

        /// <summary>
        /// 路由包长度索引
        /// </summary>
        public const int RouterPacketLengthIndex = TotalLength;

        /// <summary>
        /// 路由包类型
        /// </summary>
        public const int RouterPacketOpcode = 4;

        public const int RouterPacketOpcodeIndex = TotalLength + RouterPacketLength;

        public const int RouterPacketBodyIndex = TotalLength + RouterPacketLength + RouterPacketOpcode;
        
        /// <summary>
        /// 路由包数据的最小长度
        /// </summary>
        public const int RouterPacketMinLength = TotalLength + RouterPacketLength + RouterPacketOpcode;
        
        /// <summary>
        /// 客户端包长度
        /// </summary>
        public const int ClientPacketLength = 4;

        /// <summary>
        /// 客户端包类型
        /// </summary>
        public const int ClientPacketOpcode = 4;
    }

    public abstract class AChannel : IDisposable
    {
        public long Id;

        public ChannelType ChannelType { get; protected set; }

        private IPEndPoint remoteAddress;

        public IPEndPoint RemoteAddress
        {
            get
            {
                return this.remoteAddress;
            }
            set
            {
                this.remoteAddress = value;
            }
        }

        public bool IsDisposed
        {
            get
            {
                return this.Id == 0;
            }
        }

        public abstract void Dispose();
    }
}