Esempio n. 1
0
        /// <summary>
        /// log worker 시작
        /// </summary>
        void Execute()
        {
            while (!m_threadStopEvent.WaitForEvent(0))
            {
                Thread.Sleep(1);

                m_logString = "";


                lock (m_logLock)
                {
                    while (m_logQueue.Count != 0)
                    {
                        string logString = m_logQueue.Peek();
                        m_logQueue.Dequeue();

                        DateTime curTime = DateTime.Now;
                        m_logString += "[" + curTime.ToString("yyyy-MM-dd HH:mm:ss-fff") + "] : " + logString + "\r\n";
                    }
                }

                if (m_logString.Length > 0)
                {
                    AppendToFile(m_fileName);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Make the connection to the server and start receiving
        /// </summary>
        protected override void execute()
        {
            ConnectStatus status = ConnectStatus.SUCCESS;

            try
            {
                lock (m_generalLock)
                {
                    if (IsConnectionAlive)
                    {
                        status = ConnectStatus.FAIL_ALREADY_CONNECTED;
                        new Task(delegate()
                        {
                            OnConnected(this, status);
                        }).Start();
                        return;
                    }

                    CallBackObj       = m_clientOps.CallBackObj;
                    HostName          = m_clientOps.HostName;
                    Port              = m_clientOps.Port;
                    NoDelay           = m_clientOps.NoDelay;
                    ConnectionTimeOut = m_clientOps.ConnectionTimeOut;

                    if (HostName == null || HostName.Length == 0)
                    {
                        HostName = ServerConf.DEFAULT_HOSTNAME;
                    }

                    if (Port == null || Port.Length == 0)
                    {
                        Port = ServerConf.DEFAULT_PORT;
                    }

                    m_client         = new TcpClient();
                    m_client.NoDelay = NoDelay;

                    m_client.Client.BeginConnect(HostName, Convert.ToInt32(Port), new AsyncCallback(IocpTcpClient.onConnected), this);
                    if (m_timeOutEvent.WaitForEvent(ConnectionTimeOut))
                    {
                        if (!m_client.Connected)
                        {
                            status = ConnectStatus.FAIL_SOCKET_ERROR;
                            new Task(delegate()
                            {
                                OnConnected(this, status);
                            }).Start();
                            return;
                        }
                        IsConnectionAlive = true;
                        new Task(delegate()
                        {
                            OnConnected(this, ConnectStatus.SUCCESS);
                        }).Start();
                    }
                    else
                    {
                        try
                        {
                            if (m_client.Client.Connected)
                            {
                                m_client.Client.Shutdown(SocketShutdown.Both);
                                //Client.Client.Disconnect(true);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                        }
                        m_client.Close();
                        status = ConnectStatus.FAIL_TIME_OUT;
                        Task t = new Task(delegate()
                        {
                            OnConnected(this, status);
                        });
                        t.Start();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                Task t = new Task(delegate()
                {
                    OnConnected(this, ConnectStatus.FAIL_SOCKET_ERROR);
                });
                t.Start();
                return;
            }
            startReceive();
        }
        /// <summary>
        /// Make the connection to the server and start receiving
        /// </summary>
        protected override void execute()
        {
            ConnectStatus status = ConnectStatus.SUCCESS;

            try
            {
                lock (m_generalLock)
                {
                    if (IsConnectionAlive())
                    {
                        status = ConnectStatus.FAIL_ALREADY_CONNECTED;
                        throw new CallbackException();
                    }

                    m_callBackObj        = m_clientOps.callBackObj;
                    m_hostName           = m_clientOps.hostName;
                    m_port               = m_clientOps.port;
                    m_noDelay            = m_clientOps.noDelay;
                    m_waitTimeInMilliSec = m_clientOps.waitTimeInMilliSec;

                    if (m_hostName == null || m_hostName.Length == 0)
                    {
                        m_hostName = ServerConf.DEFAULT_HOSTNAME;
                    }

                    if (m_port == null || m_port.Length == 0)
                    {
                        m_port = ServerConf.DEFAULT_PORT;
                    }


                    m_client.NoDelay = m_noDelay;

                    m_client.BeginConnect(m_hostName, Convert.ToInt32(m_port), new AsyncCallback(IocpTcpClient.onConnected), this);
                    if (m_timeOutEvent.WaitForEvent(m_waitTimeInMilliSec))
                    {
                        if (!m_client.Connected)
                        {
                            status = ConnectStatus.FAIL_SOCKET_ERROR;
                            throw new CallbackException();
                        }
                        m_isConnected  = true;
                        m_clientStream = new NetworkStream(m_client.Client);
                        if (m_callBackObj != null)
                        {
                            Thread t = new Thread(delegate()
                            {
                                m_callBackObj.OnConnected(this, ConnectStatus.SUCCESS);
                            });
                            t.Start();
                        }
                    }
                    else
                    {
                        m_client.Dispose();
                        status = ConnectStatus.FAIL_TIME_OUT;
                        throw new CallbackException();
                    }
                }
            }
            catch (CallbackException)
            {
                if (m_callBackObj != null)
                {
                    Thread t = new Thread(delegate()
                    {
                        m_callBackObj.OnConnected(this, status);
                    });
                    t.Start();
                }
                return;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
                if (m_callBackObj != null)
                {
                    Thread t = new Thread(delegate()
                    {
                        m_callBackObj.OnConnected(this, ConnectStatus.FAIL_SOCKET_ERROR);
                    });
                    t.Start();
                }
                return;
            }
            startReceive();
        }