public bool Init(int maxTracedThreads, ushort port, bool bBlocking)
        {
            this.Clear();

            m_port = ushort.MaxValue;

            m_packetPool       = new CustomObjectPool(4096);
            m_packetCollection = new PacketCollection();
            m_packetBuffers    = new PacketBuffer[maxTracedThreads];
            m_maxTracedThreads = maxTracedThreads;
            m_packetCollection.Init(SocketConnection.kGlobalQueueSize);

            if (!SocketBase.InitSockets())
            {
                this.Log("behaviac: Failed to initialize sockets.\n");
                return(false);
            }

            {
                behaviac.Debug.Log("behaviac: ConnectorInterface.Init Enter\n");
                string portMsg = string.Format("behaviac: listing at port {0}\n", port);
                behaviac.Debug.Log(portMsg);

                this.ReserveThreadPacketBuffer();
                this.SetConnectPort(port);

                {
                    this.CreateAndStartThread();
                }

                if (bBlocking)
                {
                    Debug.LogWarning("behaviac: SetupConnection is blocked, please Choose 'Connect' in the Designer to continue");

                    while (!this.IsConnected() || !this.IsConnectedFinished())
                    {
                        // Wait for connection
                        System.Threading.Thread.Sleep(100);
                    }

                    System.Threading.Thread.Sleep(1);

                    Debug.Check(this.IsConnected() && this.IsConnectedFinished());
                }

                behaviac.Debug.Log("behaviac: ConnectorInterface.Init Connected\n");

                //wait for the OnConnection ends
                System.Threading.Thread.Sleep(200);

                behaviac.Debug.Log("behaviac: ConnectorInterface.Init successful\n");
            }

            m_isInited.AtomicInc();

            return(m_packetBuffers != null);
        }
Beispiel #2
0
        public bool CollectPackets(PacketCollection coll)
        {
            if (m_packetQueue.Count == 0)
            {
                return(true);
            }

            lock (m_packetQueue)
            {
                Packet packet = m_packetQueue.Peek();

                while (packet == null)
                {
                    m_packetQueue.Dequeue();

                    if (m_packetQueue.Count == 0)
                    {
                        break;
                    }

                    packet = m_packetQueue.Peek();
                }

                while (packet != null)
                {
                    if (coll.Add(packet))
                    {
                        m_packetQueue.Dequeue();

                        if (m_packetQueue.Count == 0)
                        {
                            break;
                        }

                        packet = m_packetQueue.Peek();
                    }
                    else
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Beispiel #3
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();
        }