Ejemplo n.º 1
0
        }                                                                             /*}}}*/

        protected override void when_data_arrival(ClientTcp clientRec, int idxThread) /*{{{
                                                                                       */
        {
            string dataBuffer = clientRec.dataBuffer;
            string curLine    = "";

            while (dataBuffer.Contains(CRLF))
            {
                string[] parts = dataBuffer.Split(arrCRLF, 2, StringSplitOptions.None);
                curLine              = parts[0];
                dataBuffer           = parts[1];
                clientRec.dataBuffer = parts[1];
                Interlocked.Increment(ref this._statCountClientLine);

                if (OnLineArrival != null)  // Call Event if Define
                {
                    if (this.frmInvoke == null)
                    {
                        this.OnLineArrival(curLine, clientRec, idxThread);
                    }
                    else
                    {
                        this.frmInvoke.Invoke(OnLineArrival, new object[] { curLine, clientRec, idxThread });
                    }
                }
            }
        }       /*}}}*/
Ejemplo n.º 2
0
        }                                                                        /*}}}*/

        protected override void when_client_connect_success(ClientTcp clientRec) /*{{{
                                                                                  */
        {
            if (OnClientConnectedSuccess == null)
            {
                return;
            }
            if (this.frmInvoke == null)
            {
                this.OnClientConnectedSuccess(clientRec);
            }
            else
            {
                this.frmInvoke.Invoke(OnClientConnectedSuccess, new object[] { clientRec });
            }
        }                                                  /*}}}*/
Ejemplo n.º 3
0
        }                                                                   /*}}}*/

        protected override void when_client_disconnect(ClientTcp clientRec) /*{{{
                                                                             */
        {
            if (OnClientDisconnected == null)
            {
                return;
            }
            if (this.frmInvoke == null)
            {
                this.OnClientDisconnected(clientRec);
            }
            else
            {
                this.frmInvoke.Invoke(OnClientDisconnected, new object[] { clientRec });
            }
        }                                                                             /*}}}*/
Ejemplo n.º 4
0
        }       /*}}}*/

        // Private Thread(s)
        private void thread_listen()    /*{{{
                                         */
        {
            TcpListener listener = new TcpListener(this.ipAddress, this.portNum);

            try
            {
                listener.Start();
                isStart = true;
                this.trickListenFinish.Set();

                // Cycle Listen for Client Connection
                ulong NumClient = 0;

                while (!this.isTerminate)
                {
                    TcpClient handleClient = listener.AcceptTcpClient();
                    if (handleClient != null)
                    {
                        NumClient++;
                        ClientTcp clientRec = new ClientTcp(NumClient, handleClient);
                        this.when_client_connect_success(clientRec);
                        lock (this.clientRecQ.SyncRoot)
                        {
                            this.clientRecQ.Enqueue(clientRec);
                        }
                        Interlocked.Increment(ref this._statCountClientConnect);
                    }
                    else
                    {
                        Interlocked.Increment(ref this._statCountClientErrorAccept);
                        this.when_client_connect_fail();
                    }
                }
            }
            catch
            {
                isStart = false;
                this.trickListenFinish.Set();
                return;
            }
        }                             /*}}}*/
Ejemplo n.º 5
0
        }                                                                            /*}}}*/

        protected virtual void when_data_arrival(ClientTcp clientRec, int idxThread) /*{{{
                                                                                      */
        // Byte data any length was Read-In
        {
            clientRec.dataBuffer = ""; // Clear DataBuffer if doesnot do anything
        }                              /*}}}*/
Ejemplo n.º 6
0
        }                                                                  /*}}}*/

        protected virtual void when_client_disconnect(ClientTcp clientRec) /*{{{
                                                                            */
        // Client Disconnected - Normal & AbNormal Case
        {
        }                                                                            /*}}}*/
Ejemplo n.º 7
0
        }                                                                       /*}}}*/

        protected virtual void when_client_connect_success(ClientTcp clientRec) /*{{{
                                                                                 */
        // Client is Connected Success
        {
        }                                                 /*}}}*/
Ejemplo n.º 8
0
        }                             /*}}}*/

        private void thread_pooling() /*{{{
                                       */
        {
            string[] parts        = Thread.CurrentThread.Name.Split(new string[] { "_" }, StringSplitOptions.None);
            int      ThisThreadID = -1;

            if (parts.Length == 3)
            {
                int.TryParse(parts[2], out ThisThreadID);
            }
            this.when_thread_pooling_init(ThisThreadID);

            ClientTcp clientRec = null;

            while (!this.isTerminate)
            {
                clientRec = null;
                lock (this.clientRecQ.SyncRoot)
                {
                    if (this.clientRecQ.Count > 0)
                    {
                        clientRec = (ClientTcp)this.clientRecQ.Dequeue();
                    }
                }
                if (clientRec == null)
                {
                    // Nothing to Process
                    Thread.Sleep(1);
                    continue;
                }
                else
                {
                    switch (clientRec.read())
                    {
                    case -2:        // Disconnecting or Close
                    case -1:        // Error Exception
                        //Console.WriteLine("Client #" + clientRec.ID.ToString() + " Disconnected or Closed");
                        break;

                    case 0:         // No-More data, then Process IT now
                        //Console.WriteLine(Thread.CurrentThread.Name + " Read 0 data");
                        break;

                    default:        // Have data but not sure how many
                        //Console.WriteLine(Thread.CurrentThread.Name + " Read >0 data");
                        if (clientRec.dataBuffer.Length > 0)
                        {
                            this.when_data_arrival(clientRec, ThisThreadID);      // Raise EVENT if have DATA
                        }
                        break;
                    }

                    // Push client for Next Cycle
                    if (clientRec.isAlive)
                    {
                        lock (this.clientRecQ.SyncRoot)
                        {
                            this.clientRecQ.Enqueue(clientRec);
                        }
                    }
                    else
                    {
                        Interlocked.Increment(ref this._statCountClientDisconnect);
                        this.when_client_disconnect(clientRec);
                    }

                    Thread.Sleep(1);
                }
            }
        }                                 /*}}}*/