Added support for examples to the build system. Cleaned up examples.

Examples can now be automatically built with the project, if enabled. And I've cleaned up MiaoDX's examples to be more compliant with frnetlib's coding style.
This commit is contained in:
Unknown 2017-05-29 18:57:39 +01:00
parent ae61464aee
commit c4fea83a2c
4 changed files with 176 additions and 0 deletions

View File

@ -6,6 +6,7 @@ set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake_mo
#User options #User options
option(USE_SSL "Use SSL" ON) option(USE_SSL "Use SSL" ON)
option(BUILD_EXAMPLES "Build frnetlib examples" ON)
set(FRNETLIB_BUILD_SHARED_LIBS false CACHE BOOL "Build shared library.") set(FRNETLIB_BUILD_SHARED_LIBS false CACHE BOOL "Build shared library.")
if(USE_SSL) if(USE_SSL)
@ -65,6 +66,11 @@ if(USE_SSL)
endif() endif()
endif() endif()
#Build examples if needbe
if(BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
install( install(
TARGETS frnetlib TARGETS frnetlib
RUNTIME DESTINATION bin COMPONENT bin RUNTIME DESTINATION bin COMPONENT bin

View File

@ -0,0 +1,8 @@
add_executable(simple_client tcpsocket_client.cpp)
target_link_libraries(simple_client frnetlib)
add_executable(simple_server tcpsocket_server.cpp)
target_link_libraries(simple_server frnetlib)
install(TARGETS simple_client simple_server
DESTINATION "bin")

View File

@ -0,0 +1,86 @@
#include <frnetlib/TcpSocket.h>
#include <frnetlib/TcpListener.h>
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT "8081"
int send_a_packet(fr::TcpSocket &socket)
{
std::cout << "Going to send something..." << std::endl;
//Send the request
fr::Packet packet;
packet << "Hello there, I am " << (float)1.2 << " years old";
if(socket.send(packet) != fr::Socket::Success)
{
//Failed to send packet
std::cout << "Seems got something wrong when sending" << std::endl;
return -1;
}
//Receive a response
std::cout << "Waiting for a response..." << std::endl;
if(socket.receive(packet) != fr::Socket::Success)
{
std::cout << "Failed to receive server response!" << std::endl;
return -2;
}
//Extract the response, you can surround this in a try/catch block to catch errors
std::string str1, str2;
float age;
packet >> str1 >> age >> str2;
std::cout << "Server sent: " << str1 << ", " << age << ", " << str2 << "\n\n\n" << std::endl;
return 0;
}
int main()
{
//Try and connect to the server, create a new TCP object for the job and then connect
fr::TcpSocket socket; //Use an fr::SSLSocket if SSL
if(socket.connect(SERVER_IP, SERVER_PORT) != fr::Socket::Success)
{
//Failed to connect
std::cout << "Failed to connect to: " << SERVER_IP << ":" << SERVER_PORT << std::endl;
return -1;
}
//For storing user input and send_a_packet response
std::string op_str;
int rtn = 0;
//Keep going until either we, or the server closes the connection
while(true)
{
//Ask the user what to do
std::cout << "Choose what you want to do, `c` for `continue`, `q` for `quit`:" << std::endl;
std::cin >> op_str;
if(op_str.length() > 1)
{
std::cout << "Seems that you inputted more than one character, please retry." << std::endl;
continue;
}
switch(op_str[0])
{
case 'c':
std::cout << "continue" << std::endl;
rtn = send_a_packet(socket);
break;
case 'q':
break;
}
//Exit/error check
if(op_str[0] == 'q')
break;
if(rtn != 0)
break;
}
std::cout << "All done, bye!" << std::endl;
}

View File

@ -0,0 +1,76 @@
#include <frnetlib/TcpSocket.h>
#include <frnetlib/TcpListener.h>
#define PORT "8081"
int main()
{
//Create a new TCP socket to maintain connections, and a new Tcp Listener to accept connections
fr::TcpSocket client; //fr::SSLSocket for SSL
fr::TcpListener listener; //fr::SSLListener for HTTPS
//Bind to a port
if(listener.listen(PORT) != fr::Socket::Success)
{
std::cout << "Failed to bind to port: " << PORT << std::endl;
return -1;
}
std::cout << "Listener is listening on port: " << PORT << "..." << std::endl;
//Start accepting connections
while(true)
{
std::cout << "Waiting for a new connection ..." << std::endl;
//Accept a new connection
if(listener.accept(client) != fr::Socket::Success)
{
std::cout << "Failed to accept client, shutdown" << std::endl;
break;
}
while(true) //Infinite loop to keep the connection active
{
try
{
//Receive an fr::Packet from the client
fr::Packet packet;
if(client.receive(packet) != fr::Socket::Success)
{
std::cout << "Failed to receive request" << std::endl;
client.close_socket();
break;
}
//Extract the data from the packet
std::string str1, str2;
float age;
packet >> str1 >> age >> str2;
//Print out what we got
std::cout << "Client sent:" << str1 << ", " << age << ", " << str2 << std::endl;
//Send back the same packet
if(client.send(packet) != fr::Socket::Success)
{
throw std::string("Failed to send packet to client");
}
}
catch(const std::exception &e)
{
//Print out what happened
std::cout << "Error: " << e.what() << std::endl;
//Close connection
client.close_socket();
break;
}
}
}
return 0;
}