Exemplo n.º 1
0
 public ClientData()
 {
     tcpThread       = null;
     udpThread       = null;
     ackThread       = null;
     _ackThreadClass = null;
     _udpThreadClass = null;
     _tcpThreadClass = null;
     avdPortNum      = 0;
     clientID        = null;
     clientIP        = null;
 }
Exemplo n.º 2
0
        public void ListenerThread()
        {
            try
            {
                IPHostEntry ipHost = Dns.Resolve("192.168.0.8");
                IPAddress   ipAddr = ipHost.AddressList[0];
                tcpClient = new TcpListener(ipAddr, tcpPortNum);
                udpSock   = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                tcpClient.Start();
                isRunning = true;

                // db = new DBConnect(this);
                con      = new ConnectHandler(this);
                checkACK = new CheckACKThread(this, avdPortNum);

                Dict_Client = new Dictionary <int, ClientData>();
                // 클라이언트의 연결 요청 확인
                logMSG("info", "TCP 클라이언트 대기중... CloudPhoneTestServer.ListenerThread");

                while (isRunning)
                {
                    clientData            = new ClientData();
                    clientData.AVDPortNum = avdPortNum;

                    while (!tcpClient.Pending())
                    {
                        if (isRunning == false)
                        {
                            tcpClient.Server.Close(0);
                            tcpClient.Stop();
                            tcpClient = null;
                        }
                        Thread.Sleep(100);
                    }

                    //logMSG("info", "클라이언트의 요청 발견... CloudPhoneTestServer.ListenerThread");

                    // logMSG("info", "TCP 클라이언트 통신용 쓰레드 생성 CloudPhoneTestServer new ConnectionHandler");
                    con.threadListener = this.tcpClient;

                    tcpThread = new Thread(new ThreadStart(con.clientHandler));
                    tcpThread.Start();
                    logMSG("info", "tcpThread 시작");

                    Thread.Sleep(2000);

                    // TCP로 메시지를 받았으면 바로 BackGroundThread 실행(UDP)
                    //logMSG("info", "UDP Thread 실행 준비");

                    setClientInfo(con.ip, con.clientID, clientData);

                    // DB 에 클라이언트 ID가 있는 지 체크하고 등록한다.
                    //db.CheckClientID(clientData.ClientID);
                    Dict_ClientID.Add(clientData.ClientID, avdPortNum);

                    ACKThread = new Thread(new ThreadStart(checkACK.SendAckToClient));


                    clientData._TcpThreadClass = con;
                    clientData.TCPThread       = tcpThread;
                    clientData._AckThreadClass = checkACK;
                    clientData.ACKThread       = ACKThread;
                    Dict_Client.Add(avdPortNum, clientData);
                    ACKThread.Start();
                    logMSG("info", "ACKThread 시작");
                    StartAVD(avdPortNum);

                    IPHostEntry _ipHost = Dns.Resolve(clientData.ClientIP);
                    IPAddress   _ipAddr = _ipHost.AddressList[0];
                    IPEndPoint  ipep    = new IPEndPoint(_ipAddr, udpPortNum);

                    checkBG   = new CheckBackGroundThread(this, udpSock, ipep);
                    udpThread = new Thread(new ThreadStart(checkBG.udpCheckbg));
                    //logMSG("info", "UDP 클라이언트 통신용 쓰레드 생성... CheckBackGroundThread 실행");
                    udpThread.Start();
                    logMSG("info", "udpThread 시작");

                    // ClientData Set
                    Dict_Client[avdPortNum]._UdpThreadClass = checkBG;
                    Dict_Client[avdPortNum].UDPThread       = udpThread;

                    Thread.Sleep(300);
                }
            }
            catch (Exception e)
            {
                logMSG("error", "ListenerThread : " + e.Message);
                isRunning   = false;
                isConnected = false;
            }
            finally // TCP, UDP 종료
            {
                if (tcpClient != null)
                {
                    try
                    {
                        // tcpClient.Server.Shutdown(SocketShutdown.Both);
                    }
                    catch (Exception e)
                    {
                        logMSG("error", "tcpClient Shutdown Err : " + e.Message);
                    }
                    finally
                    {
                        tcpClient.Server.Close(0);
                        tcpClient.Stop();

                        tcpClient = null;
                    }
                }

                if (udpSock != null)
                {
                    // udpSock.Shutdown(SocketShutdown.Both);
                    udpSock.Close();
                    udpSock = null;
                }
            }
        }