Example #1
0
        /// <summary>
        /// 处理客户端
        /// </summary>
        /// <param name="socket">客户端套接字</param>
        private void HandleClient(Socket socket)
        {
            IPacketHandler handler = new PacketHandler(socket);

            socket.ReceiveTimeout = timeout;

            try
            {
                byte[] rcvMsg = handler.ReceiveNormalPacket();

                string error;
                string ipAddress   = ((IPEndPoint)socket.RemoteEndPoint).Address.ToString();
                bool   openSuccess = HandleOpenPacket(rcvMsg, ipAddress, out error);

                if (openSuccess)
                {
                    TdsClient client = new TdsClient(handler, this, connectionString);
                    byte[]    sndMsg = BuildSuccessOpenPacket(handler.Key, handler.IV);
                    handler.SendNormalPacket(sndMsg);
                    ThreadPool.QueueUserWorkItem(client.Exchange);
                    socket.ReceiveTimeout = 0;
                }
                else
                {
                    byte[] sndMsg = BuildFailOpenPacket(error);
                    handler.SendNormalPacket(sndMsg);
                    handler.Close();
                }
            }

            catch (SocketException se)
            {
                Console.WriteLine("SocketException:{0}", se.Message);
                handler.Close();
            }

            catch (IOException ioe)
            {
                Console.WriteLine("IOException:{0}", ioe.Message);
                handler.Close();
            }

            catch (PacketException pe)
            {
                Console.WriteLine("PacketException:{0}", pe.Message);
                handler.Close();
            }

            catch (BEncodingException bee)
            {
                Console.WriteLine("BEncoding Exception:{0}", bee.Message);
                handler.Close();
            }
        }