//Disconnects a specific connection
void ConnectionManager::Disconnect(u16 connectionHandle)
{
	Connection* connection = GetConnectionFromHandle(connectionHandle);
	if (connection != NULL)
	{
		connection->Disconnect();
	}
}
Esempio n. 2
0
void Chat::HandleDisconnect(StringHash eventType, VariantMap& eventData)
{
    Network* network = GetSubsystem<Network>();
    Connection* serverConnection = network->GetServerConnection();
    // If we were connected to server, disconnect
    if (serverConnection)
        serverConnection->Disconnect();
    // Or if we were running a server, stop it
    else if (network->IsServerRunning())
        network->StopServer();
    
    UpdateButtons();
}
Esempio n. 3
0
TEST(lagi_signal, connection_outlives_slot) {
	int x = 0;
	Connection c;

	EXPECT_EQ(0, x);
	{
		Signal<> s;
		c = s.Connect([&] { ++x; });
		s();
		EXPECT_EQ(1, x);
	}
	c.Disconnect();
}
Esempio n. 4
0
void SceneReplication::HandleDisconnect(StringHash eventType, VariantMap& eventData)
{
    Network* network = GetSubsystem<Network>();
    Connection* serverConnection = network->GetServerConnection();
    // If we were connected to server, disconnect. Or if we were running a server, stop it. In both cases clear the
    // scene of all replicated content, but let the local nodes & components (the static world + camera) stay
    if (serverConnection)
    {
        serverConnection->Disconnect();
        scene_->Clear(true, false);
        clientObjectID_ = 0;
    }
    // Or if we were running a server, stop it
    else if (network->IsServerRunning())
    {
        network->StopServer();
        scene_->Clear(true, false);
    }
    
    UpdateButtons();
}
Esempio n. 5
0
//--------------------------------------------------------------------------
void Client::Clear()
{
	VE_AUTO_LOCK_MUTEX(m_kMutex);
	m_kConnectionMap.Clear();
	m_kConnectOnList.BeginIterator();
	while(!m_kConnectOnList.IsEnd())
	{
		Connection* pkConnect = m_kConnectOnList.GetIterNode()->m_tContent;
		m_kConnectOnList.Next();
		VE_ASSERT(pkConnect);
		pkConnect->Disconnect();
	}
	VE_ASSERT(m_kConnectOnList.Empty());
	m_kConnectOffList.BeginIterator();
	while(!m_kConnectOffList.IsEnd())
	{
		Connection* pkConnect = m_kConnectOffList.GetIterNode()->m_tContent;
		m_kConnectOffList.Next();
		VE_ASSERT(pkConnect);
		VE_SAFE_DELETE(pkConnect);
	}
	VE_ASSERT(m_kConnectOffList.Empty());
}
Esempio n. 6
0
  void ConnectionManager::HandleEdgeClose(const QString &reason)
  {
    Edge *edge = qobject_cast<Edge *>(sender());
    qDebug() << "Edge closed: " << edge->ToString() << reason;
    if(!_con_tab.RemoveEdge(edge)) {
      qWarning() << "Edge closed but no Edge found in CT:" << edge->ToString();
    }

    Connection *con = _con_tab.GetConnection(edge);
    if(con != 0) {
      con = _con_tab.GetConnection(con->GetRemoteId());
      if(con != 0) {
        con->Disconnect();
      }
    }

    if(!_closed) {
      return;
    }

    if(_con_tab.GetEdges().count() == 0) {
      emit Disconnected();
    }
  }
Esempio n. 7
0
int main(int argc, char** argv)
{
	if (!Initialize())
	{
		cout << "Failed to initialize socket layer\n";
		system("pause");
		return -1;
	}
	if (!Bind(PORT))
	{
		cout << "Failed to bind listening socket\n";
		system("pause");
		Cleanup();
		return -1;
	}
	Connection SendingSocket;

	char choice;
	char IP[16];
	cout << "Host or Join (h/j)\n";
	cin >> choice;
	if (choice == 'h')
	{
		if (!ReceivingSocket.Accept())
		{
			cout << "Error while connecting to partner\n";
			system("pause");
			Cleanup();
			return -1;
		}
		if(!SendingSocket.Accept())
		{
			cout << "Error while connecting to partner\n";
			system("pause");
			Cleanup();
			return -1;
		}
	}
	else
	{
		cout << "IP: ";
		cin >> IP;
		if (!SendingSocket.Connect(IP, PORT))
		{
			cout << "Error while connecting to partner\n";
			cout << StringError() << endl;
			system("pause");
			Cleanup();
			return -1;
		}
		if(!ReceivingSocket.Connect(IP, PORT))
		{
			cout << "Error while connecting to partner\n";
			cout << StringError() << endl;
			system("pause");
			Cleanup();
			return -1;
		}
	}

	HRESULT result;
	IAudioCaptureClient* capture_client = NULL;
	IAudioClient* audio_client_capture = NULL;
	IMMDevice* capture_device = NULL;
	IMMDeviceEnumerator* enumerator = NULL;
	IPropertyStore* propstore_capture = NULL;
	PROPVARIANT pv_capture;
	WAVEFORMATEX* wf_capture = NULL;
	UINT32 packSize;
	UINT32 availableFrames;
	UINT32 timeIntervalForBuffer = 1000000;
	UINT32 timeIntervalInMilliseconds = timeIntervalForBuffer / 10000;
	BYTE* pData = NULL;
	DWORD flags;
	float index = 0;

	result = CoInitialize(0);
	if (FAILED(result))
	{
		printf("Failed to initialize\n");
		goto Exit;
	}
	result = CoCreateInstance(
		__uuidof(MMDeviceEnumerator), NULL,
		CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
		(void**)&enumerator);
	if (FAILED(result))
	{
		printf("Failed to create device enumerator\n");
		goto Exit;
	}
	result = enumerator->GetDefaultAudioEndpoint(eCapture, eCommunications, &capture_device);
	if (FAILED(result))
	{
		printf("Failed to get capture endpoint handle\n");
		goto Exit;
	}
	PropVariantInit(&pv_capture);
	result = capture_device->OpenPropertyStore(STGM_READ, &propstore_capture);
	if (FAILED(result))
	{
		printf("Failed to read device properties\n");
		goto Exit;
	}
	propstore_capture->GetValue(PKEY_Device_FriendlyName, &pv_capture);
	printf("Opening capture device: %S\n", pv_capture.pwszVal);
	PropVariantClear(&pv_capture);
	result = capture_device->Activate(
		__uuidof(IAudioClient), CLSCTX_ALL, NULL, (void**)&audio_client_capture);
	if (FAILED(result))
	{
		printf("Failed to activate capture device\n");
		goto Exit;
	}
	result = audio_client_capture->GetMixFormat(&wf_capture);
	if (FAILED(result))
	{
		printf("Failed to get mix format\n");
		goto Exit;
	}
	result = audio_client_capture->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, timeIntervalForBuffer, 0, wf_capture, NULL);
	if (FAILED(result))
	{
		printf("Failed to initialize audio client\n");
		goto Exit;
	}
	printf("Sample rate: %u Hz\n", wf_capture->nSamplesPerSec);
	printf("Sample size: %u bits\n", wf_capture->wBitsPerSample);
	printf("Size of audio frame: %u bytes\n", wf_capture->nBlockAlign);
	printf("Number of channels: %u\n", wf_capture->nChannels);
	result = audio_client_capture->Start();
	if (FAILED(result))
	{
		printf("Failed to start recording\n");
		goto Exit;
	}
	result = audio_client_capture->GetService(__uuidof(IAudioCaptureClient), (void**)&capture_client);
	if (FAILED(result))
	{
		printf("Failed to get capture service\n");
		goto Exit;
	}
	
	// Get render endpoint interface
	result = enumerator->GetDefaultAudioEndpoint(eRender, eCommunications, &render_device);
	if (FAILED(result))
	{
		printf("Failed to get render endpoint handle\n");
		goto Exit;
	}
	PropVariantInit(&pv_render);
	result = render_device->OpenPropertyStore(STGM_READ, &propstore_render);
	if (FAILED(result))
	{
		printf("Failed to read device properties\n");
		goto Exit;
	}
	propstore_render->GetValue(PKEY_Device_FriendlyName, &pv_render);
	printf("Opening render device: %S\n", pv_render.pwszVal);
	PropVariantClear(&pv_render);
	result = render_device->Activate(
		__uuidof(IAudioClient), CLSCTX_ALL, NULL, (void**)&audio_client_render);
	if (FAILED(result))
	{
		printf("Failed to activate render device\n");
		goto Exit;
	}
	result = audio_client_render->GetMixFormat(&wf_render);
	if (FAILED(result))
	{
		printf("Failed to get mix format\n");
		goto Exit;
	}
	result = audio_client_render->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, timeIntervalForBuffer, 0, wf_render, NULL);
	if (FAILED(result))
	{
		printf("Failed to initialize audio client\n");
		goto Exit;
	}
	printf("Sample rate: %u Hz\n", wf_render->nSamplesPerSec);
	printf("Sample size: %u bits\n", wf_render->wBitsPerSample);
	printf("Size of audio frame: %u bytes\n", wf_render->nBlockAlign);
	printf("Number of channels: %u\n", wf_render->nChannels);
	result = audio_client_render->Start();
	if (FAILED(result))
	{
		printf("Failed to start recording\n");
		goto Exit;
	}
	result = audio_client_render->GetService(__uuidof(IAudioRenderClient), (void**)&render_client);
	if (FAILED(result))
	{
		printf("Failed to get render service\n");
		goto Exit;
	}

	int partner_format_received_size;
	SendingSocket.Send((const char*)wf_capture, sizeof(*wf_capture));
	ReceivingSocket.Recv((char*)&partner_format, sizeof(partner_format), partner_format_received_size);
	if (last_error != _NO_ERROR)
	{
		printf("Connection failed\n");
		goto Exit;
	}
	if (partner_format.nSamplesPerSec != wf_render->nSamplesPerSec)
	{
		/*cout << "Partned format:\n";
		cout << partner_format.wBitsPerSample << endl;
		cout << partner_format.nSamplesPerSec << endl;
		cout << "My format:\n";
		cout << wf_render->wBitsPerSample << endl;
		cout << wf_render->nSamplesPerSec << endl;*/
		printf("Partner capture format unsupported\n");
		//goto Exit;
	}
	LPTHREAD_START_ROUTINE StartRoutine = (LPTHREAD_START_ROUTINE)RenderAudio;
	DWORD threadID;
	HANDLE hThread = CreateThread(NULL, 0, StartRoutine, NULL, 0, &threadID);
	while (true)
	{
		Sleep(timeIntervalInMilliseconds);
		result = capture_client->GetNextPacketSize(&packSize);
		if (FAILED(result))
		{
			printf("Failed to get next pack size\n");
			break;
		}
		while (packSize)
		{
			result = capture_client->GetBuffer(&pData, &availableFrames, &flags, NULL, NULL);
			if (FAILED(result))
			{
				printf("Failed to get buffer\n");
				goto Exit;
			}
			if (!SendingSocket.Send((const char*)pData, availableFrames * wf_capture->nBlockAlign))
			{
				if (last_error == SOCKET_CLOSED)
				{
					printf("Disconnect\n");
				}
				else
				{
					printf("Connection broke\n");
				}
				goto Exit;
			}
			result = capture_client->ReleaseBuffer(packSize);
			result = capture_client->GetNextPacketSize(&packSize);
			if (FAILED(result))
			{
				printf("Failed to release buffer\n");
				goto Exit;
			}
		}
		result = capture_client->ReleaseBuffer(packSize);
		if (GetAsyncKeyState(VK_ESCAPE))
		{
			break;
		}
	}
	
Exit:
	audio_client_capture->Stop();
	//TerminateThread(hThread, 0);
	audio_client_render->Stop();

	SendingSocket.Disconnect();
	ReceivingSocket.Disconnect();
	SAFE_RELEASE(capture_device);
	SAFE_RELEASE(render_device);
	SAFE_RELEASE(enumerator);
	SAFE_RELEASE(propstore_capture);
	SAFE_RELEASE(propstore_render);
	SAFE_RELEASE(capture_client);
	SAFE_RELEASE(render_client);
	SAFE_RELEASE(audio_client_capture);
	SAFE_RELEASE(audio_client_render);
	system("pause");
	return 0;
}
Esempio n. 8
0
unsigned Listener::Process()
{
	//Profile profile("Listener::Process");
    ////////////////////////////////////////
    //  handle inactive state (with UdpManager)
    if (!IsActive() && mTcpManager)
    {
		//	check all connections to see if they are idle
		std::set<Connection *>::iterator iterator;
		for (iterator = mConnections.begin(); iterator != mConnections.end(); iterator++)
		{
			Connection * connection = *iterator;
			if (connection->IsConnected())
				connection->Disconnect();
		}
		//	close the UdpManager if all the connections are closed
		if (!mConnectionCount)
		{
			mTcpManager->Release();
			mTcpManager = 0;
			OnShutdown();
		}
    }
    ////////////////////////////////////////
    //  handle active state (without UdpManager)
    else if (IsActive() && !mTcpManager)
    {
		mParams = GetConnectionParams();
        mActiveMax = GetActiveRequestMax();
        mTcpManager = new TcpManager(mParams);
		mTcpManager->SetHandler(this);
		if (mTcpManager->BindAsServer()){
            OnStartup();
        }else{
            OnFailedStartup();
            return 0;
        }
    }

    ////////////////////////////////////////
    //  process the TcpManager
    if (mTcpManager)
    {
		//Profile subProfile("TcpManager::GiveTime()");
        mTcpManager->GiveTime();
    }

	//	check all closed connections to see if they are idle
	std::list<Connection *>::iterator closedIterator = mClosedConnections.begin();
	while (closedIterator != mClosedConnections.end())
	{
		//Profile profile("Listener::Process (cleanup connection)");
		std::list<Connection *>::iterator current = closedIterator++;
		Connection * connection = *current;
		if (!connection->GetActiveRequests() && 
			!connection->GetQueuedRequests())
		{
			mClosedConnections.erase(current);
			mConnections.erase(connection);
			mConnectionCount--;
			OnConnectionDestroyed(connection);
			delete connection;
		}
	}

    ////////////////////////////////////////
    //  process request queue
    while (!mQueuedRequests.empty() && (!mActiveMax || mActiveCount < mActiveMax))
    {
		//Profile profile("Listener::Process (activate queued request)");
        QueueNode & node = mQueuedRequests.front();
        if (!IsActive())
        {
            //  If not active, discard queued request
            if (node.connection)
            {
                // normal request, internal requests have no connection
                node.connection->NotifyDiscardRequest(node.request);
            }
            DestroyRequest(node.request);
        }
        else
        {
            //  Move request to active list
            if (node.connection)
            {
                // normal request, internal requests have no connection
                node.connection->NotifyBeginRequest(node.request);
            }
            mActiveRequests.push_back(node);
			mActiveCount++;
        }
        mQueuedRequests.pop_front();
    }

    ////////////////////////////////////////
    //  Process active requests
    unsigned requestsProcessed = 0;
    std::list<QueueNode>::iterator iterator = mActiveRequests.begin();
    while (iterator != mActiveRequests.end())
    {
		//Profile profile("Listener::Process (process request)");
        std::list<QueueNode>::iterator current = iterator++;
        RequestBase * request = current->request;
        Connection * connection = current->connection;
    
        if (request->Process())
        {
            if (connection)
            {
                // normal request, internal requests have no connection
                connection->NotifyEndRequest(request);
            }
            DestroyRequest(request);
            mActiveRequests.erase(current);
			mActiveCount--;
        }
		else if (mSleepingRequests.find(request) != mSleepingRequests.end())
		{
            mActiveRequests.erase(current);
		}
	    requestsProcessed++;
    }
    return requestsProcessed;
}