Ejemplo n.º 1
0
	void ConnectorImpl::OnConnection()
	{
		Log("behaviac: sending initial settings.\n");

		this->SendInitialSettings();

		Socket::SendWorkspaceSettings();

		this->SendInitialProperties();

		{
			ScopedInt_t scopedInt(&gs_threadFlag);
			Log("behaviac: sending packets before connecting.\n");

			this->SendExistingPackets();
		}

		behaviac::Socket::SendText("[connected]precached message done");

		//when '[connected]' is handled in the designer, it will send back all the breakpoints if any and '[breakcpp]' and '[start]'
		//here we block until all those messages have been received, otherwise, if we don't block here to wait for all those messages
		//the breakpoints checking might be wrong.
		bool bLoop = true;
		while (bLoop && !m_isDisconnected)
		{
			//sending packets if any
			if (m_packetsCount > 0)
			{
				SendAllPackets();
			}

			const char* kStartMsg = "[start]";
			bool bFound = this->ReceivePackets(kStartMsg);

			if (bFound)
			{
				bLoop = false;
			}
			else
			{
				behaviac::Thread::Sleep(1);
			}
		}

		Log("behaviac: OnConnection done.\n");
		//this->m_bHandleMessage = false;
	}
Ejemplo n.º 2
0
    void ConnectorInterface::ThreadFunc()
    {
#if BEHAVIAC_COMPILER_MSVC
        //printf("ThreadFunc gs_threadFlag = %d\n", (int)gs_threadFlag.value());
        BEHAVIAC_ASSERT(gs_threadFlag.value() == 0);
#endif
        {
            ScopedInt_t scopedInt(&gs_threadFlag);
            Log("behaviac: Socket Thread Starting\n");
#if BEHAVIAC_COMPILER_MSVC
            BEHAVIAC_ASSERT(t_packetBufferIndex != TLS_OUT_OF_INDEXES);
#else
            //printf("ThreadFunc t_packetBufferIndex = %d\n", t_packetBufferIndex);
            //BEHAVIAC_ASSERT(t_packetBufferIndex != (unsigned int)-1);
#endif//
        }
        namespace Socket = behaviac::Socket;
        const bool blockingSocket = true;
        behaviac::Socket::Handle	serverSocket = 0;
        {
            ScopedInt_t scopedInt(&gs_threadFlag);
            serverSocket = Socket::Create(blockingSocket);

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

            char bufferTemp[64];
            string_sprintf(bufferTemp, "behaviac: Listening at port %d...\n", m_port);
            Log(bufferTemp);

            // max connections: 1, don't allow multiple clients?
            if (!Socket::Listen(serverSocket, m_port, 1))
            {
                Log("behaviac: Couldn't configure server socket.\n");
                Socket::Close(serverSocket);
                return;
            }
        }
#if BEHAVIAC_COMPILER_MSVC
        BEHAVIAC_ASSERT(gs_threadFlag.value() == 0);
#endif

        this->ReserveThreadPacketBuffer();

        while (!m_terminating)
        {
#if BEHAVIAC_COMPILER_MSVC

            //wait for connecting
            while (!m_terminating)
            {
                //Log("Socket::TestConnection.\n");
                if (Socket::TestConnection(serverSocket))
                {
                    break;
                }

                behaviac::Thread::Sleep(100);
            }

#endif

            if (!m_terminating)
            {
                BEHAVIAC_ASSERT(gs_threadFlag.value() == 0);
                Log("behaviac: accepting...\n");
                {
                    ScopedInt_t scopedInt(&gs_threadFlag);
                    m_writeSocket = Socket::Accept(serverSocket, kSocketBufferSize);

                    if (!m_writeSocket)
                    {
                        Log("behaviac: Couldn't create write socket.\n");
                        Socket::Close(serverSocket);
                        return;
                    }

                    Log("behaviac: connection accepted\n");
                }

                BEHAVIAC_ASSERT(gs_threadFlag.value() == 0);

                {
                    ScopedInt_t scopedInt(&gs_threadFlag);

                    AtomicInc(m_isConnected);
                    behaviac::Thread::Sleep(1);

                    OnConnection();

                    AtomicInc(m_isConnectedFinished);
                    behaviac::Thread::Sleep(1);

                    //this->OnConnectionFinished();

                    Log("behaviac: after Connected.\n");
                }

                BEHAVIAC_ASSERT(gs_threadFlag.value() == 0);

                while (!m_terminating && this->m_writeSocket)
                {
                    behaviac::Thread::Sleep(1);
                    SendAllPackets();

                    ReceivePackets();
                }

                BEHAVIAC_ASSERT(gs_threadFlag.value() == 0);

                // One last time, to send any outstanding packets out there.
                if (this->m_writeSocket)
                {
                    SendAllPackets();

					Socket::Close(m_writeSocket);
				}

                this->Clear();

                Log("behaviac: disconnected. \n");
            }
        }//while (!m_terminating)

        Socket::Close(serverSocket);

        this->Clear();

        BEHAVIAC_ASSERT(gs_threadFlag.value() == 0);

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