상속: IDisposable
        void GotUdpMessage(IAsyncResult ar)
        {
            IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint epSender = (EndPoint)ipeSender;
            int size = UDPSocket.EndReceiveFrom(ar, ref epSender);

            string senderAddress = sender.Address.ToString();
            int senderPort = sender.Port;

            // Get the data back
            byte[] data = new byte[buff.Length];
            buff.CopyTo(data, 0);
            Pipe pipe = new Pipe();
            pipe.Write(data, 0, size);
            filter.ProcessReceivedData(pipe, epSender, size, false);

            // Start Accepting again
            UDPSocket.BeginReceiveFrom(buff, 0, buff.Length, SocketFlags.None, ref epSender, GotUdpMessage, epSender);
        }
        internal CATcpConnection(Socket socket, CAServer server)
        {
            pipe = new Pipe();

            Socket = socket;

            processData = new Thread(new ThreadStart(BackgroundProcess));
            processData.IsBackground = true;
            processData.Start();

            Server = server;
            remoteKey = Socket.RemoteEndPoint.ToString();
            Socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveData, null);

            // Send version
            Socket.Send(new byte[] { 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0 });

            Closed = false;
        }
        public void ProcessReceivedData(Pipe dataPipe, EndPoint remoteEndPoint, int maxPacketSize = 0, bool wait = true)
        {
            string remoteAddress = remoteEndPoint.ToString();
            uint readedBytes = 0;
            UInt16 cmdId, dataType;
            UInt32 payloadSize, dataCount, param1, param2;
            byte[] payload = null;
            byte[] header = null;

            while (maxPacketSize == 0 || maxPacketSize >= (readedBytes + 16))
            {
                if (!wait && dataPipe.AvailableBytes == 0)
                    return;
                header = dataPipe.Read(16);

                //Pipe destroyed
                if (header.Length == 0)
                    return;

                cmdId = header.ToUInt16(0);
                payloadSize = header.ToUInt16(2);
                dataType = header.ToUInt16(4);
                param1 = header.ToUInt32(8);
                param2 = header.ToUInt32(12);

                if (payloadSize == 0xFFFF)
                {
                    payloadSize = dataPipe.Read(4).ToUInt32();
                    dataCount = dataPipe.Read(4).ToUInt32();
                    readedBytes += (payloadSize + 24);
                }
                else
                {
                    dataCount = header.ToUInt16(6);
                    readedBytes += (payloadSize + 16);
                }

                payload = dataPipe.Read(Convert.ToInt32(payloadSize));

                HandleMessage(cmdId, dataType, ref payloadSize, ref dataCount,
                              ref param1, ref param2, ref header, ref payload, ref remoteEndPoint);
            }
        }
        protected void ProcessReceivedData(Pipe pipe)
        {
            try
            {
                Server.Filter.ProcessReceivedData(pipe, Socket.RemoteEndPoint, 0, false);
            }
            catch (NullReferenceException exc)
            {
                while (Server == null)
                    Thread.Sleep(10);

                Server.Filter.ProcessReceivedData(pipe, Socket.RemoteEndPoint, 0, false);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + "\n\r" + e.StackTrace);
            }
        }