ITANetAudioServer.cpp 1.8 KB
Newer Older
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
1 2 3
#include "ITANetAudioServer.h"
#include "ITANetAudioProtocol.h"

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <ITANetAudioStreamingServer.h>

// ITA includes
#include <ITADataSource.h>
#include <ITAException.h>
#include <ITAStreamInfo.h>

// Vista includes
#include <VistaInterProcComm/Concurrency/VistaThreadLoop.h>
#include <VistaInterProcComm/Connections/VistaConnectionIP.h>
#include <VistaInterProcComm/IPNet/VistaTCPServer.h>
#include <VistaInterProcComm/IPNet/VistaTCPSocket.h>
#include <VistaBase/VistaTimeUtils.h>
#include <VistaInterProcComm/IPNet/VistaIPAddress.h>

// STL
#include <cmath>
#include <cassert>

Anne's avatar
Anne committed
23 24
CITANetAudioServer::CITANetAudioServer()
	: m_pServer( NULL )
25
	, m_pSocket( NULL )
26
	, m_pConnection( NULL )
27 28 29 30 31 32
	, m_iServerPort( -1 )
{
};

CITANetAudioServer::~CITANetAudioServer()
{
33
	m_pSocket = NULL;
34 35
	delete m_pConnection;
	delete m_pServer;
36 37 38 39 40 41 42 43 44 45 46 47
}

std::string CITANetAudioServer::GetServerAddress() const
{
	return m_sServerAddress;
}

int CITANetAudioServer::GetNetworkPort() const
{
	return m_iServerPort;
}

Anne's avatar
Anne committed
48
bool CITANetAudioServer::Start(const std::string& sAddress, int iPort)
49 50 51 52 53 54 55 56 57 58
{
	if( m_pServer )
		ITA_EXCEPT1( MODAL_EXCEPTION, "This net sample server is already started" );

	m_pServer = new VistaTCPServer( sAddress, iPort, 1 );
	m_sServerAddress = sAddress;
	m_iServerPort = iPort;

	// blocking wait for connection
	m_pSocket = m_pServer->GetNextClient();
59 60 61 62 63 64 65
	if( !m_pSocket )
		return false;
	if( m_pSocket->GetIsConnected() )
		m_pConnection = new VistaConnectionIP( m_pSocket );

	if( !m_pConnection )
		return false;
66

67
	return true;
Anne's avatar
Anne committed
68
}
69

70
VistaConnectionIP* CITANetAudioServer::GetConnection() const
Anne's avatar
Anne committed
71
{
72
	return m_pConnection;
73 74
}

Anne's avatar
Anne committed
75

76
void CITANetAudioServer::Stop()
77
{
78 79 80
	delete m_pConnection;
	m_pConnection = NULL;

81
	m_pSocket = NULL;
82

83 84 85 86 87 88
	delete m_pServer;
	m_pServer = NULL;
}

bool CITANetAudioServer::IsConnected() const
{
89 90
	return m_pConnection ? true : false;
}