Пример #1
0
        public void Close()
        {
            m_terminating.AtomicInc();
            m_isConnectedFinished.AtomicDec();

            m_isDisconnected.AtomicInc();

            if (s_tracerThread != null)
            {
                if (s_tracerThread.IsAlive)
                {
                    while (IsConnected() && s_tracerThread.IsAlive)
                    {
                        System.Threading.Thread.Sleep(1);
                    }
                }

                lock (this)
                {
                    m_packetBuffers = null;
                }

                if (s_tracerThread.IsAlive)
                {
                    s_tracerThread.Abort();
                }

                s_tracerThread = null;
            }

            if (m_packetCollection != null)
            {
                m_packetCollection.Close();
                m_packetCollection = null;
            }

            m_packetPool        = null;
            t_packetBufferIndex = -1;
            SocketBase.ShutdownSockets();

            LogManager.Close();

            m_isInited.AtomicDec();
        }
Пример #2
0
        /**
         * return true if 'msgCheck' is received
         */

        protected bool ReceivePackets(string msgCheck)
        {
            if (this.m_writeSocket != null && this.m_writeSocket.Connected)
            {
                int    kBufferLen = 2048;
                byte[] buffer     = new byte[kBufferLen];

                bool found = false;
                int  reads = SocketBase.Read(ref m_writeSocket, buffer, kBufferLen);

                while (reads > 0 && this.m_writeSocket != null && this.m_writeSocket.Connected)
                {
                    //BEHAVIAC_LOG(MEMDIC_LOG_INFO, buffer);

                    lock (this)
                    {
                        ms_texts += GetStringFromBuffer(buffer, 0, reads, true);
                    }

                    if (!string.IsNullOrEmpty(msgCheck) && ms_texts.IndexOf(msgCheck) != -1)
                    {
                        found = true;
                    }

                    reads = SocketBase.Read(ref m_writeSocket, buffer, kBufferLen);
                }

                if (this.m_bHandleMessage && this.m_writeSocket != null && this.m_writeSocket.Connected)
                {
                    string msgs = "";

                    if (this.ReadText(ref msgs))
                    {
                        this.OnRecieveMessages(msgs);

                        return(true);
                    }
                }

                return(found);
            }

            return(false);
        }
Пример #3
0
        protected void SendAllPackets()
        {
            for (int i = 0; i < m_maxTracedThreads; ++i)
            {
                if (m_packetBuffers[i] != null && !m_packetBuffers[i].m_free)
                {
#if USING_BEHAVIAC_SEQUENTIAL
                    if (!m_packetBuffers[i].CollectPackets(m_packetCollection))
                    {
                        break;
                    }
#else
                    m_packetBuffers[i].SendPackets(m_writeSocket);
#endif
                }
            }

#if USING_BEHAVIAC_SEQUENTIAL
            // TODO: Deal with Socket.Write failures.
            // (right now packet is lost).
            m_packetCollection.Sort();

            int      endIndex = 0;
            Packet[] packets  = m_packetCollection.GetPackets(ref endIndex);

            for (int i = 0; i < endIndex; ++i)
            {
                Packet p = packets[i];

                int bytesWritten = (0);
                SocketBase.Write(m_writeSocket, p.GetData(), ref bytesWritten);
                if (!m_writeSocket.Connected || bytesWritten <= 0)
                {
                    break;
                }
            }

            m_packetCollection.Reset();
#endif
            this.m_packetsCount = 0;
        }
Пример #4
0
        private void SendPackets(Socket h)
        {
            Packet packet = m_packetQueue.Peek();

            while (packet != null)
            {
                int  bytesWritten = (0);
                bool success      = SocketBase.Write(h, packet.GetData(), ref bytesWritten);

                // Failed to send data. Most probably sending too much, break and
                // hope for the best next time
                if (!success)
                {
                    Debug.Check(false);
                    behaviac.Debug.LogWarning("A packet is not correctly sent...\n");
                    break;
                }

                m_packetQueue.Dequeue();        // 'Commit' pop if data sent.
                packet = m_packetQueue.Peek();
            }
        }
Пример #5
0
        private void ThreadFunc()
        {
            Log("behaviac: Socket Thread Starting\n");
            try
            {
                this.ReserveThreadPacketBuffer();
                int bufferIndex = t_packetBufferIndex;
                Debug.Check(bufferIndex > 0);

                bool   blockingSocket = true;
                Socket serverSocket   = null;
                try
                {
                    serverSocket = SocketBase.Create(blockingSocket);

                    if (serverSocket == null)
                    {
                        Log("behaviac: Couldn't create server socket.\n");
                        return;
                    }

                    string bufferTemp = string.Format("behaviac: Listening at port {0}...\n", m_port);
                    Log(bufferTemp);

                    // max connections: 1, don't allow multiple clients?
                    if (!SocketBase.Listen(serverSocket, m_port, 1))
                    {
                        Log("behaviac: Couldn't configure server socket.\n");
                        SocketBase.Close(ref serverSocket);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Debug.LogError(ex.Message);
                }

                while (m_terminating.Get() == 0)
                {
                    //wait for connecting
                    while (m_terminating.Get() == 0)
                    {
                        if (SocketBase.TestConnection(serverSocket))
                        {
                            break;
                        }

                        System.Threading.Thread.Sleep(100);
                    }

                    if (m_terminating.Get() == 0)
                    {
                        Log("behaviac: accepting...\n");
                        try
                        {
                            m_writeSocket = SocketBase.Accept(serverSocket, SocketConnection.kSocketBufferSize);

                            if (m_writeSocket == null)
                            {
                                Log("behaviac: Couldn't create write socket.\n");
                                SocketBase.Close(ref serverSocket);
                                return;
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.LogError(ex.Message);
                        }

                        try
                        {
                            m_isConnected.AtomicInc();
                            System.Threading.Thread.Sleep(1);

                            OnConnection();

                            m_isConnectedFinished.AtomicInc();
                            System.Threading.Thread.Sleep(1);

                            //this.OnConnectionFinished();

                            Log("behaviac: Connected. accepted\n");
                        }
                        catch (Exception ex)
                        {
                            Debug.LogError(ex.Message);
                        }

                        try
                        {
                            while (m_terminating.Get() == 0 && this.m_writeSocket != null)
                            {
                                System.Threading.Thread.Sleep(1);

                                this.SendAllPackets();
                                this.ReceivePackets("");
                            }

                            if (this.m_writeSocket != null && this.m_writeSocket.Connected)
                            {
                                // One last time, to send any outstanding packets out there.
                                this.SendAllPackets();
                            }

                            SocketBase.Close(ref this.m_writeSocket);
                            this.Clear();

                            Log("behaviac: disconnected. \n");
                        }
                        catch (Exception ex)
                        {
                            Debug.LogError(ex.Message);
                        }
                    }
                }//while (!m_terminating)

                SocketBase.Close(ref serverSocket);

                this.Clear();
            }
            catch (Exception ex)
            {
                Debug.LogError(ex.Message);
            }

            Log("behaviac: ThreadFunc exited. \n");
        }