FS#86 - Rewrite the sockets API
Updated the API documentation of the whole network module The system headers are no longer included by the sfml-network public headers git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1475 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
parent
a09ee0f9e3
commit
9280771665
66 changed files with 3976 additions and 2992 deletions
|
@ -22,17 +22,35 @@ public :
|
|||
////////////////////////////////////////////////////////////
|
||||
/// Constructor
|
||||
///
|
||||
/// \param Socket : Socket that holds the connection with the server
|
||||
/// \param host Remote host to which send the recording data
|
||||
/// \param port Port of the remote host
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
NetworkRecorder(sf::SocketTCP socket) :
|
||||
mySocket(socket)
|
||||
NetworkRecorder(const sf::IpAddress& host, unsigned short port) :
|
||||
myHost(host),
|
||||
myPort(port)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private :
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// /see SoundRecorder::OnStart
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
virtual bool OnStart()
|
||||
{
|
||||
if (mySocket.Connect(myHost, myPort) == sf::Socket::Done)
|
||||
{
|
||||
std::cout << "Connected to server " << myHost << std::endl;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// /see SoundRecorder::ProcessSamples
|
||||
///
|
||||
|
@ -48,10 +66,27 @@ private :
|
|||
return mySocket.Send(packet) == sf::Socket::Done;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// /see SoundRecorder::OnStop
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
virtual void OnStop()
|
||||
{
|
||||
// Send a "end-of-stream" packet
|
||||
sf::Packet packet;
|
||||
packet << endOfStream;
|
||||
mySocket.Send(packet);
|
||||
|
||||
// Close the socket
|
||||
mySocket.Disconnect();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Member data
|
||||
////////////////////////////////////////////////////////////
|
||||
sf::SocketTCP mySocket; ///< Socket used to communicate with the server
|
||||
sf::IpAddress myHost; ///< Address of the remote host
|
||||
unsigned short myPort; ///< Remote port
|
||||
sf::TcpSocket mySocket; ///< Socket used to communicate with the server
|
||||
};
|
||||
|
||||
|
||||
|
@ -70,41 +105,25 @@ void DoClient(unsigned short port)
|
|||
}
|
||||
|
||||
// Ask for server address
|
||||
sf::IpAddress serverAddress;
|
||||
sf::IpAddress server;
|
||||
do
|
||||
{
|
||||
std::cout << "Type address or name of the server to connect to : ";
|
||||
std::cin >> serverAddress;
|
||||
std::cin >> server;
|
||||
}
|
||||
while (serverAddress == sf::IpAddress::None);
|
||||
while (server == sf::IpAddress::None);
|
||||
|
||||
// Create a TCP socket for communicating with server
|
||||
sf::SocketTCP socket;
|
||||
|
||||
// Connect to the specified server
|
||||
if (socket.Connect(port, serverAddress) != sf::Socket::Done)
|
||||
return;
|
||||
std::cout << "Connected to server " << serverAddress << std::endl;
|
||||
// Create an instance of our custom recorder
|
||||
NetworkRecorder recorder(server, port);
|
||||
|
||||
// Wait for user input...
|
||||
std::cin.ignore(10000, '\n');
|
||||
std::cout << "Press enter to start recording audio";
|
||||
std::cin.ignore(10000, '\n');
|
||||
|
||||
// Create a instance of our custom recorder
|
||||
NetworkRecorder recorder(socket);
|
||||
|
||||
// Start capturing audio data
|
||||
recorder.Start(44100);
|
||||
std::cout << "Recording... press enter to stop";
|
||||
std::cin.ignore(10000, '\n');
|
||||
recorder.Stop();
|
||||
|
||||
// Send a "end-of-stream" packet
|
||||
sf::Packet packet;
|
||||
packet << endOfStream;
|
||||
socket.Send(packet);
|
||||
|
||||
// Close the socket when we're done
|
||||
socket.Close();
|
||||
}
|
||||
|
|
|
@ -32,17 +32,6 @@ public :
|
|||
Initialize(1, 44100);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Destructor
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
~NetworkAudioStream()
|
||||
{
|
||||
// Close the sockets
|
||||
myClient.Close();
|
||||
myListener.Close();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Run the server, stream audio data from the client
|
||||
///
|
||||
|
@ -52,14 +41,14 @@ public :
|
|||
if (!myHasFinished)
|
||||
{
|
||||
// Listen to the given port for incoming connections
|
||||
if (!myListener.Listen(port))
|
||||
if (myListener.Listen(port) != sf::Socket::Done)
|
||||
return;
|
||||
std::cout << "Server is listening to port " << port << ", waiting for connections... " << std::endl;
|
||||
|
||||
// Wait for a connection
|
||||
sf::IpAddress clientAddress;
|
||||
myListener.Accept(myClient, &clientAddress);
|
||||
std::cout << "Client connected : " << clientAddress << std::endl;
|
||||
if (myListener.Accept(myClient) != sf::Socket::Done)
|
||||
return;
|
||||
std::cout << "Client connected: " << myClient.GetRemoteAddress() << std::endl;
|
||||
|
||||
// Start playback
|
||||
Play();
|
||||
|
@ -149,7 +138,7 @@ private :
|
|||
else if (id == endOfStream)
|
||||
{
|
||||
// End of stream reached : we stop receiving audio data
|
||||
std::cout << "Audio data has been 100% received !" << std::endl;
|
||||
std::cout << "Audio data has been 100% received!" << std::endl;
|
||||
myHasFinished = true;
|
||||
}
|
||||
else
|
||||
|
@ -164,8 +153,8 @@ private :
|
|||
////////////////////////////////////////////////////////////
|
||||
// Member data
|
||||
////////////////////////////////////////////////////////////
|
||||
sf::SocketTCP myListener;
|
||||
sf::SocketTCP myClient;
|
||||
sf::TcpListener myListener;
|
||||
sf::TcpSocket myClient;
|
||||
sf::Mutex myMutex;
|
||||
std::vector<sf::Int16> mySamples;
|
||||
std::vector<sf::Int16> myTempBuffer;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue