コード例 #1
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="port">端口</param>
        /// <param name="remote">远程ip地址</param>
        /// <param name="subThread">是否运行子线程派发消息</param>
        /// <param name="type">封包类型</param>
        /// <param name="es">封包缓存大小</param>
        public UdpSocket(int port, IPEndPoint remote, bool subThread = true, PackType type = PackType.Total, int es = 262144)
        {
            endPoint = remote;
            //Links = new Linker[thread * 1024];
            IPEndPoint ip = new IPEndPoint(IPAddress.Any, port);

            soc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //new UdpClient(_port);//new IPEndPoint(IPAddress.Parse(ip),
            soc.Bind(ip);
            soc.ReceiveTimeout = 1000;

            if (type != PackType.None)
            {
                Packaging     = true;
                envelope      = new TcpEnvelope(es);
                envelope.type = type;
            }
            running = true;
            auto    = subThread;
            if (thread == null)
            {
                thread = new Thread(Run);
                thread.Start();
            }
            queue = new QueueBuffer <SocData>();
        }
コード例 #2
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="bs">缓冲区大小</param>
 /// <param name="type">封包类型</param>
 /// <param name="es">封包缓冲区大小</param>
 public TcpSocket(int bs = 262144, PackType type = PackType.Part, int es = 262144)
 {
     buffer = new byte[bs];
     if (type != PackType.None)
     {
         Packaging     = true;
         envelope      = new TcpEnvelope(es);
         envelope.type = type;
     }
     queue = new Queue <SocData>();
 }
コード例 #3
0
        public TcpLinker(Socket soc, PackType pack = PackType.All, int buffsize = 4096)
        {
            Link          = soc;
            envelope      = new TcpEnvelope();
            envelope.type = pack;
            var obj = soc.RemoteEndPoint.GetType().GetProperty("Address");

            addr = obj.GetValue(soc.RemoteEndPoint, null) as IPAddress;
            //生成id
            var buf = addr.GetAddressBytes();

            unsafe
            {
                fixed(byte *bp = &buf[0])
                ip = *(int *)bp;
            }
            buff = new byte[buffsize];
        }