diff --git a/.gitignore b/.gitignore index 628c535..21a1d72 100644 --- a/.gitignore +++ b/.gitignore @@ -393,3 +393,7 @@ FodyWeavers.xsd *.exe *.out *.app + + +# Build +/build \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 9015411..bf79529 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.5) project(easy-socket VERSION 0.2.0) -option(EASY_SOCKET_BUILD_TESTS "Turn on to build tests." ON) +option(BUILD_EXAMPLES "Turn on to build tests." ON) set(PROJECT_COMPATIBILITY AnyNewerVersion) add_library(${PROJECT_NAME} INTERFACE) @@ -35,12 +35,12 @@ install( configure_package_config_file( ${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" - INSTALL_DESTINATION lib/cmake/${PROJECT_NAME}-${PROJECT_VERSION} + INSTALL_DESTINATION lib/cmake/${PROJECT_NAME} ) install( EXPORT ${PROJECT_NAME}Targets - DESTINATION lib/cmake/${PROJECT_NAME}-${PROJECT_VERSION} + DESTINATION lib/cmake/${PROJECT_NAME} ) install( @@ -48,7 +48,7 @@ install( "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" DESTINATION - lib/cmake/${PROJECT_NAME}-${PROJECT_VERSION} + lib/cmake/${PROJECT_NAME} ) install( @@ -58,8 +58,10 @@ install( set(${PROJECT_NAME}_VERSION ${PROJECT_VERSION} CACHE INTERNAL "") -if(EASY_SOCKET_BUILD_TESTS) +if(BUILD_EXAMPLES) # Add sub directories - add_subdirectory(test/test-server) - add_subdirectory(test/test-client) + add_subdirectory(examples/test-tcp-server) + add_subdirectory(examples/test-tcp-client) + add_subdirectory(examples/test-udp-sender) + add_subdirectory(examples/test-udp-receiver) endif() diff --git a/README.md b/README.md index b651ff0..8ad5e21 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,9 @@ masesk::EasySocket easySocket; //or using namespace masesk; ``` ### Usecase - * EasySocket wraps the code for the server and client, so that a single header can be used to initialize and start a server or a client. +* EasySocket supports both UDP and TCP. [Refer to this StackOverflow answer for difference between the two and when to use each one.](https://stackoverflow.com/questions/1099672/when-is-it-appropriate-to-use-udp-instead-of-tcp#answer-1099734) +#### TCP ```cpp //server example @@ -43,22 +44,76 @@ void handleData(const std::string &data) { int main() { EasySocket socketManager; - socketManager.socketListen("test", 8080, &handleData); + socketManager.socketListenTCP("test", 8080, &handleData); return 0; } ``` ```cpp // client example + +#include +#include +#include +using namespace std; +using namespace masesk; + +int main() { + EasySocket socketManager; + socketManager.socketConnectTCP("test", "127.0.0.1", 8080); + string userInput; + while (true) { + cout << "> "; + getline(cin, userInput); + if (userInput.size() <= 0) { + break; + } + socketManager.socketSendTCP("test", userInput); + } + socketManager.closeConnection("test"); + return 0; +} +``` + +#### UDP + +```cpp +// receiver example +#include +#include +using namespace std; +using namespace masesk; + +void handleData(const std::string &data) { + cout << "Client sent: " + data << endl; +} + +int main() { + EasySocket socketManager; + socketManager.socketListenUDP("test", 9090, handleData); + return 0; +} +``` + +```cpp +// send example #include #include #include using namespace std; using namespace masesk; + int main() { EasySocket socketManager; - socketManager.socketConnect("test", "127.0.0.1", 8080); - socketManager.socketSend("test", "Hello from client!"); + string userInput; + while (true) { + cout << "> "; + getline(cin, userInput); + if (userInput.size() <= 0) { + break; + } + socketManager.socketSendUDP("127.0.0.1", 9090, userInput); + } socketManager.closeConnection("test"); return 0; } @@ -66,35 +121,61 @@ int main() { ### Functions +### TCP + #### Server Functions -* `void socketListen(std::string channelName, int port, std::function callback);` +* `void socketListenTCP(const std::string &channelName, const std::uint16_t &port, std::function callback);` * channelName: string identifier of channel * port: integer value of port used on server side (eg. 8080) * function: pointer of function that will be called to handle the data when the server recieves data from the client +* `void closeConnection(const std::string &channelName)` - close socket port on server + * channelName: string identifier of channel + #### Client Functions -* `void socketConnect(std::string channelName, std::string ip, int port)` - start a new connection with a server with a channel name, ip address of the server, and the port the server would be listening on. +* `void socketConnectTCP(const std::string &channelName, const std::string &ip, const std::uint16_t &port)` - start a new connection with a server with a channel name, ip address of the server, and the port the server would be listening on. * channelName - string identifier of channel * ip - string for where the server resides (eg. 127.0.0.1 for local) * port - integer value of port used on server side (eg. 8080) -* `void socketSend(std::string channelName, std::string data)` - send data to server based on channel name +* `void socketSendTCP(const std::string &channelName, const std::string &data)` - send data to server based on channel name * channelName: string identifier of channel * data: data to be sent through to the server on given channel -* `void closeConnection(std::string channelName)` - close connection with server using channel name +* `void closeConnection(const std::string &channelName)` - close connection with server using channel name + * channelName: string identifier of channel + + +### UDP + +#### Receiver Functions + +* `void socketListenUDP(const std::string &channelName, const std::uint16_t &port, std::function callback);` * channelName: string identifier of channel + * port: integer value of port used on server side (eg. 9090) + * function: pointer of function that will be called to handle the data when the server recieves data from the client +* `void closeConnection(const std::string &channelName)` - close UDP socket port + * channelName: string identifier of channel + +#### Sender Functions +* `void socketSendUDP(const std::string &ip, const std::uint16_t &port, const std::string &data)` - send data to server based on channel name + * ip - string for where the server resides (eg. 127.0.0.1 for local) + * port - integer value of port used on server side (eg. 9090) + * data: data to be sent through to the server on given channel ## Example -Check `test/test-server` and `test/test-client` for a working client and server example running locally. +Check `test/test-tcp-server` / `test/test-tcp-client` / `test/test-udp-receiver` / `test/test-udp-sender` for a working client and server example running locally. ### Build Tests on Windows 1. Open `easy-socket.sln` in Visual Studio 2017 -2. Right click on `test-client`, select `properites`, and change `Windows SDK Version` to your installed 10.x -3. Right click on `test-server`, select `properites`, and change `Windows SDK Version` to your installed 10.x -4. Right click on ```Solution 'easy-socket' ``` and select `Rebuild entire solution`. -5. Select desired `Configuration` (Debug/Release) and `Platform` (x64/Win32) from the top-bar dropdowns next to the start button. -6. Executables will be available at `[x64 or Win32]/[Debug or Release]` +2. Right click on `test-tcp-client`, select `properites`, and change `Windows SDK Version` to your installed 10.x +3. Right click on `test-tcp-server`, select `properites`, and change `Windows SDK Version` to your installed 10.x +4. Right click on `test-udp-receiver`, select `properites`, and change `Windows SDK Version` to your installed 10.x +5. Right click on `test-udp-sender`, select `properites`, and change `Windows SDK Version` to your installed 10.x +6. Right click on ```Solution 'easy-socket' ``` and select `Rebuild entire solution`. +7. Select desired `Configuration` (Debug/Release) and `Platform` (x64/Win32) from the top-bar dropdowns next to the start button. +8. Right click on `Solution easy-socket` and select two projects to start: either `test-tcp-server` and `test-tcp-client` OR `test-udp-receiver` and `test-udp-sender` +9. Executables will be available at `[x64 or Win32]/[Debug or Release]` ### Build Tests on Linux @@ -116,4 +197,6 @@ cmake .. make ``` -Executables will be available at `build/tests/test-client/client` and `build/tests/test-client/server` +Executables will be available at `build/tests/test-tcp-client/tcp_client` and `build/tests/test-tcp-client/tcp_server` and `build/tests/test-udp-receiver/udp_receiver` and `build/tests/test-udp-sender/udp_sender` + +Disable building tests: `cmake -D BUILD_TESTS=OFF ..` diff --git a/easy-socket.sln b/easy-socket.sln index 0d3dca9..fa5941e 100644 --- a/easy-socket.sln +++ b/easy-socket.sln @@ -5,9 +5,13 @@ VisualStudioVersion = 15.0.28010.2036 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "easy-socket", "easy-socket.vcxproj", "{EF42A18B-14CB-46AD-A34C-30B35EF0586C}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-client", "test\test-client\test-client.vcxproj", "{4FFDB01A-3314-4B28-841A-1FE8CE3516CE}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-tcp-client", "examples\test-tcp-client\test-client.vcxproj", "{4FFDB01A-3314-4B28-841A-1FE8CE3516CE}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-server", "test\test-server\test-server.vcxproj", "{1CAD60FA-43FF-4595-84B3-B72ED7F3330F}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-tcp-server", "examples\test-tcp-server\test-server.vcxproj", "{1CAD60FA-43FF-4595-84B3-B72ED7F3330F}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-udp-receiver", "examples\test-udp-receiver\test-udp-receiver.vcxproj", "{EE13023D-664D-4CEA-988D-35BBA91F49C4}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-udp-sender", "examples\test-udp-sender\test-udp-sender.vcxproj", "{49EC5DE8-B8C7-4081-A79C-A13E9224C175}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -41,6 +45,22 @@ Global {1CAD60FA-43FF-4595-84B3-B72ED7F3330F}.Release|x64.Build.0 = Release|x64 {1CAD60FA-43FF-4595-84B3-B72ED7F3330F}.Release|x86.ActiveCfg = Release|Win32 {1CAD60FA-43FF-4595-84B3-B72ED7F3330F}.Release|x86.Build.0 = Release|Win32 + {EE13023D-664D-4CEA-988D-35BBA91F49C4}.Debug|x64.ActiveCfg = Debug|x64 + {EE13023D-664D-4CEA-988D-35BBA91F49C4}.Debug|x64.Build.0 = Debug|x64 + {EE13023D-664D-4CEA-988D-35BBA91F49C4}.Debug|x86.ActiveCfg = Debug|Win32 + {EE13023D-664D-4CEA-988D-35BBA91F49C4}.Debug|x86.Build.0 = Debug|Win32 + {EE13023D-664D-4CEA-988D-35BBA91F49C4}.Release|x64.ActiveCfg = Release|x64 + {EE13023D-664D-4CEA-988D-35BBA91F49C4}.Release|x64.Build.0 = Release|x64 + {EE13023D-664D-4CEA-988D-35BBA91F49C4}.Release|x86.ActiveCfg = Release|Win32 + {EE13023D-664D-4CEA-988D-35BBA91F49C4}.Release|x86.Build.0 = Release|Win32 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175}.Debug|x64.ActiveCfg = Debug|x64 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175}.Debug|x64.Build.0 = Debug|x64 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175}.Debug|x86.ActiveCfg = Debug|Win32 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175}.Debug|x86.Build.0 = Debug|Win32 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175}.Release|x64.ActiveCfg = Release|x64 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175}.Release|x64.Build.0 = Release|x64 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175}.Release|x86.ActiveCfg = Release|Win32 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/examples/test-tcp-client/CMakeLists.txt b/examples/test-tcp-client/CMakeLists.txt new file mode 100644 index 0000000..76edf4a --- /dev/null +++ b/examples/test-tcp-client/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable( tcp_client + tcp_client.cpp +) + +target_link_libraries(tcp_client easy-socket) diff --git a/test/test-client/Client.cpp b/examples/test-tcp-client/tcp_client.cpp similarity index 75% rename from test/test-client/Client.cpp rename to examples/test-tcp-client/tcp_client.cpp index 57d9771..2382551 100644 --- a/test/test-client/Client.cpp +++ b/examples/test-tcp-client/tcp_client.cpp @@ -6,7 +6,7 @@ using namespace masesk; int main() { EasySocket socketManager; - socketManager.socketConnect("test", "127.0.0.1", 8080); + socketManager.socketConnectTCP("test", "127.0.0.1", 8080); string userInput; while (true) { cout << "> "; @@ -14,7 +14,7 @@ int main() { if (userInput.size() <= 0) { break; } - socketManager.socketSend("test", userInput); + socketManager.socketSendTCP("test", userInput); } socketManager.closeConnection("test"); return 0; diff --git a/test/test-client/test-client.vcxproj b/examples/test-tcp-client/test-client.vcxproj similarity index 98% rename from test/test-client/test-client.vcxproj rename to examples/test-tcp-client/test-client.vcxproj index d23b746..6ece84d 100644 --- a/test/test-client/test-client.vcxproj +++ b/examples/test-tcp-client/test-client.vcxproj @@ -23,6 +23,7 @@ {4FFDB01A-3314-4B28-841A-1FE8CE3516CE} testclient 10.0.17134.0 + test-tcp-client @@ -127,7 +128,7 @@ - + diff --git a/test/test-client/test-client.vcxproj.filters b/examples/test-tcp-client/test-client.vcxproj.filters similarity index 95% rename from test/test-client/test-client.vcxproj.filters rename to examples/test-tcp-client/test-client.vcxproj.filters index 9b8a0e2..64b391e 100644 --- a/test/test-client/test-client.vcxproj.filters +++ b/examples/test-tcp-client/test-client.vcxproj.filters @@ -15,7 +15,7 @@ - + Source Files diff --git a/examples/test-tcp-server/CMakeLists.txt b/examples/test-tcp-server/CMakeLists.txt new file mode 100644 index 0000000..c597257 --- /dev/null +++ b/examples/test-tcp-server/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable( tcp_server + tcp_server.cpp +) + +target_link_libraries(tcp_server easy-socket) diff --git a/examples/test-tcp-server/RecieverUDP.cpp b/examples/test-tcp-server/RecieverUDP.cpp new file mode 100644 index 0000000..e69de29 diff --git a/test/test-server/Server.cpp b/examples/test-tcp-server/tcp_server.cpp similarity index 75% rename from test/test-server/Server.cpp rename to examples/test-tcp-server/tcp_server.cpp index 6db749e..6a53179 100644 --- a/test/test-server/Server.cpp +++ b/examples/test-tcp-server/tcp_server.cpp @@ -9,6 +9,6 @@ void handleData(const std::string &data) { int main() { EasySocket socketManager; - socketManager.socketListen("test", 8080, &handleData); + socketManager.socketListenTCP("test", 8080, &handleData); return 0; } \ No newline at end of file diff --git a/test/test-server/test-server.vcxproj b/examples/test-tcp-server/test-server.vcxproj similarity index 98% rename from test/test-server/test-server.vcxproj rename to examples/test-tcp-server/test-server.vcxproj index 8f74a8d..9217638 100644 --- a/test/test-server/test-server.vcxproj +++ b/examples/test-tcp-server/test-server.vcxproj @@ -23,6 +23,7 @@ {1CAD60FA-43FF-4595-84B3-B72ED7F3330F} testserver 10.0.17134.0 + test-tcp-server @@ -127,7 +128,7 @@ - + diff --git a/test/test-server/test-server.vcxproj.filters b/examples/test-tcp-server/test-server.vcxproj.filters similarity index 95% rename from test/test-server/test-server.vcxproj.filters rename to examples/test-tcp-server/test-server.vcxproj.filters index f0325e0..06bbbe6 100644 --- a/test/test-server/test-server.vcxproj.filters +++ b/examples/test-tcp-server/test-server.vcxproj.filters @@ -15,7 +15,7 @@ - + Source Files diff --git a/examples/test-udp-receiver/CMakeLists.txt b/examples/test-udp-receiver/CMakeLists.txt new file mode 100644 index 0000000..19112d7 --- /dev/null +++ b/examples/test-udp-receiver/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable( udp_receiver + udp_receiver.cpp +) + +target_link_libraries(udp_receiver easy-socket) diff --git a/examples/test-udp-receiver/test-udp-receiver.vcxproj b/examples/test-udp-receiver/test-udp-receiver.vcxproj new file mode 100644 index 0000000..b6d5fe0 --- /dev/null +++ b/examples/test-udp-receiver/test-udp-receiver.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {EE13023D-664D-4CEA-988D-35BBA91F49C4} + testudpreceiver + 10.0.17134.0 + + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + + + Level3 + Disabled + true + true + $(SolutionDir)\include;%(AdditionalIncludeDirectories) + + + ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + Disabled + true + true + $(SolutionDir)\include;%(AdditionalIncludeDirectories) + + + ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + MaxSpeed + true + true + true + true + $(SolutionDir)\include;%(AdditionalIncludeDirectories) + + + true + true + ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + MaxSpeed + true + true + true + true + $(SolutionDir)\include;%(AdditionalIncludeDirectories) + + + true + true + ws2_32.lib;%(AdditionalDependencies) + + + + + + + + + \ No newline at end of file diff --git a/examples/test-udp-receiver/test-udp-receiver.vcxproj.filters b/examples/test-udp-receiver/test-udp-receiver.vcxproj.filters new file mode 100644 index 0000000..a642548 --- /dev/null +++ b/examples/test-udp-receiver/test-udp-receiver.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/examples/test-udp-receiver/udp_receiver.cpp b/examples/test-udp-receiver/udp_receiver.cpp new file mode 100644 index 0000000..bc4d6af --- /dev/null +++ b/examples/test-udp-receiver/udp_receiver.cpp @@ -0,0 +1,14 @@ +#include +#include +using namespace std; +using namespace masesk; + +void handleData(const std::string &data) { + cout << "Client sent: " + data << endl; +} + +int main() { + EasySocket socketManager; + socketManager.socketListenUDP("test_udp", 9090, handleData); + return 0; +} \ No newline at end of file diff --git a/examples/test-udp-sender/CMakeLists.txt b/examples/test-udp-sender/CMakeLists.txt new file mode 100644 index 0000000..f33ae11 --- /dev/null +++ b/examples/test-udp-sender/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable( udp_sender + udp_sender.cpp +) + +target_link_libraries(udp_sender easy-socket) diff --git a/examples/test-udp-sender/test-udp-sender.vcxproj b/examples/test-udp-sender/test-udp-sender.vcxproj new file mode 100644 index 0000000..24f7df2 --- /dev/null +++ b/examples/test-udp-sender/test-udp-sender.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {49EC5DE8-B8C7-4081-A79C-A13E9224C175} + testudpsender + 10.0.17134.0 + + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + + + Level3 + Disabled + true + true + $(SolutionDir)\include;%(AdditionalIncludeDirectories) + + + ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + Disabled + true + true + $(SolutionDir)\include;%(AdditionalIncludeDirectories) + + + ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + MaxSpeed + true + true + true + true + $(SolutionDir)\include;%(AdditionalIncludeDirectories) + + + true + true + ws2_32.lib;%(AdditionalDependencies) + + + + + Level3 + MaxSpeed + true + true + true + true + $(SolutionDir)\include;%(AdditionalIncludeDirectories) + + + true + true + ws2_32.lib;%(AdditionalDependencies) + + + + + + + + + \ No newline at end of file diff --git a/examples/test-udp-sender/test-udp-sender.vcxproj.filters b/examples/test-udp-sender/test-udp-sender.vcxproj.filters new file mode 100644 index 0000000..e076d82 --- /dev/null +++ b/examples/test-udp-sender/test-udp-sender.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/examples/test-udp-sender/udp_sender.cpp b/examples/test-udp-sender/udp_sender.cpp new file mode 100644 index 0000000..9d7ba46 --- /dev/null +++ b/examples/test-udp-sender/udp_sender.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +using namespace std; +using namespace masesk; + +int main() { + EasySocket socketManager; + string userInput; + socketManager.socketInitSendUDP("test_udp", "127.0.0.1", 9090); + while (true) { + cout << "> "; + getline(cin, userInput); + if (userInput.size() <= 0) { + break; + } + try { + socketManager.socketSendUDP("test_udp", userInput); + }catch (socket_error_exception &e) { + cout << e.what() << endl; + } + + } + socketManager.closeConnection("test"); + return 0; +} \ No newline at end of file diff --git a/include/masesk/EasySocket.hpp b/include/masesk/EasySocket.hpp index e610934..a1b5a48 100644 --- a/include/masesk/EasySocket.hpp +++ b/include/masesk/EasySocket.hpp @@ -21,35 +21,43 @@ const int SOCKET_ERROR = -1; #include #include - -namespace masesk { +namespace masesk +{ const int BUFF_SIZE = 4096; struct socket_error_exception : public std::exception { - const char * what() const throw () + const char *what() const throw() { return "Can't start socket!"; } }; struct invalid_socket_exception : public std::exception { - const char * what() const throw () + const char *what() const throw() { return "Can't create a socket!"; } }; struct data_size_exception : public std::exception { - const char * what() const throw () + const char *what() const throw() { return "Data size is above the maximum allowed by the buffer"; } }; - class EasySocket { + struct sock_save { + SOCKET sock; + sockaddr_in addr; + }; + class EasySocket + { public: - void socketListen(const std::string &channelName, int port, std::function callback) { + template + void socketListenTCP(const std::string &channelName, const std::uint16_t &port, std::function callback) + { - if (sockInit() != 0) { + if (sockInit() != 0) + { throw masesk::socket_error_exception(); } SOCKET listening = socket(AF_INET, SOCK_STREAM, 0); @@ -65,7 +73,7 @@ namespace masesk { #else hint.sin_addr.s_addr = INADDR_ANY; #endif - bind(listening, (sockaddr*)&hint, sizeof(hint)); + bind(listening, (sockaddr *)&hint, sizeof(hint)); listen(listening, SOMAXCONN); sockaddr_in client; #ifdef _WIN32 @@ -73,22 +81,23 @@ namespace masesk { #else unsigned int clientSize = sizeof(client); #endif - SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize); - server_sockets[channelName] = clientSocket; - char host[NI_MAXHOST]; - char service[NI_MAXSERV]; + SOCKET clientSocket = accept(listening, (sockaddr *)&client, &clientSize); + sock_save tcp_save; + tcp_save.sock = clientSocket; + server_sockets[channelName] = tcp_save; + char host[NI_MAXHOST]; + char service[NI_MAXSERV]; memset(host, 0, NI_MAXHOST); memset(service, 0, NI_MAXSERV); - if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0) + if (getnameinfo((sockaddr *)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0) { - std::cout << host << " connected on port " << service << std::endl; + //std::cout << host << " connected on port " << service << std::endl; } else { inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST); - std::cout << host << " connected on port " << - ntohs(client.sin_port) << std::endl; + //std::cout << host << " connected on port " << ntohs(client.sin_port) << std::endl; } sockClose(listening); char buff[BUFF_SIZE]; @@ -101,30 +110,34 @@ namespace masesk { { throw socket_error_exception(); } - if (bytesReceived > BUFF_SIZE) { + if (bytesReceived > BUFF_SIZE) + { throw masesk::data_size_exception(); } - if (bytesReceived > 0) { + if (bytesReceived > 0) + { callback(std::string(buff, 0, bytesReceived)); } - else { + else + { break; } - - } sockClose(clientSocket); sockQuit(); } - void socketSend(const std::string &channelName, const std::string &data) { - if (data.size() > BUFF_SIZE) { + void socketSendTCP(const std::string &channelName, const std::string &data) + { + if (data.size() > BUFF_SIZE) + { throw masesk::data_size_exception(); } - if (client_sockets.find(channelName) != client_sockets.end()) { - SOCKET sock = client_sockets.at(channelName); - int sendResult = send(sock, data.c_str(), data.size() + 1, 0); + if (client_sockets.find(channelName) != client_sockets.end()) + { + SOCKET sock = client_sockets.at(channelName).sock; + int sendResult = send(sock, data.c_str(), (int)data.size() + 1, 0); if (sendResult == SOCKET_ERROR) { throw masesk::socket_error_exception(); @@ -132,8 +145,10 @@ namespace masesk { } } - void socketConnect(const std::string &channelName, const std::string &ip, std::uint16_t port) { - if (sockInit() != 0) { + void socketConnectTCP(const std::string &channelName, const std::string &ip, const std::uint16_t &port) + { + if (sockInit() != 0) + { throw masesk::socket_error_exception(); return; } @@ -147,28 +162,110 @@ namespace masesk { hint.sin_family = AF_INET; hint.sin_port = htons(port); inet_pton(AF_INET, ip.c_str(), &hint.sin_addr); - int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint)); + int connResult = connect(sock, (sockaddr *)&hint, sizeof(hint)); if (connResult == SOCKET_ERROR) { sockClose(sock); sockQuit(); throw socket_error_exception(); } - client_sockets[channelName] = sock; - + client_sockets[channelName].sock = sock; } - void closeConnection(const std::string &channelName) { - if (client_sockets.find(channelName) != client_sockets.end()) { - SOCKET s = client_sockets.at(channelName); + void closeConnection(const std::string &channelName) + { + if (client_sockets.find(channelName) != client_sockets.end()) + { + SOCKET s = client_sockets.at(channelName).sock; sockClose(s); sockQuit(); } + } + + + void socketInitSendUDP(const std::string &channelName, const std::string &ip, const std::uint16_t &port) { + if (client_sockets.find(channelName) == client_sockets.end()) { + if (sockInit() != 0) + { + throw masesk::socket_error_exception(); + return; + } + SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); + sockaddr_in servaddr; + memset(&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(port); + inet_pton(AF_INET, ip.c_str(), &(servaddr.sin_addr)); + sock_save udp_sock; + udp_sock.sock = sock; + udp_sock.addr = servaddr; + client_sockets[channelName] = udp_sock; + } } - private: - std::unordered_map client_sockets; - std::unordered_map server_sockets; + void socketSendUDP(const std::string &channelName, const std::string &data) + { + if (client_sockets.find(channelName) != client_sockets.end()) + { + sock_save udp_sock = client_sockets.at(channelName); + SOCKET sock = udp_sock.sock; + sockaddr_in servaddr = udp_sock.addr; + int result = sendto(sock, data.c_str(), (int)data.length(), + 0, (const struct sockaddr *)&servaddr, + sizeof(servaddr)); + if (result == SOCKET_ERROR) + { + throw masesk::socket_error_exception(); + } + } + } + + template + void socketListenUDP(const std::string &channelName, const std::uint16_t &port, std::function callback) + { + if (sockInit() != 0) + { + throw masesk::socket_error_exception(); + return; + } + sockaddr_in servaddr; + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(port); +#ifdef _WIN32 + int serverSize = sizeof(servaddr); +#else + unsigned int serverSize = sizeof(servaddr); +#endif + inet_pton(AF_INET, "127.0.0.1", &(servaddr.sin_addr)); + SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); + bind(sock, (sockaddr *)&servaddr, serverSize); + + server_sockets[channelName].sock = sock; + char buff[BUFF_SIZE]; + sockaddr_in clientAddr; + servaddr.sin_port = htons(port); +#ifdef _WIN32 + int clientSize = sizeof(servaddr); +#else + unsigned int clientSize = sizeof(servaddr); +#endif + + while (1) + { + memset(buff, 0, BUFF_SIZE); + int bytesRecieved = recvfrom(sock, (char *)buff, BUFF_SIZE, + 0, (struct sockaddr *)&clientAddr, + &clientSize); + if (bytesRecieved > 0) + { + callback(std::string(buff, 0, bytesRecieved)); + } + } + } + + private: + std::unordered_map client_sockets; + std::unordered_map server_sockets; int sockInit(void) { #ifdef _WIN32 @@ -194,15 +291,20 @@ namespace masesk { #ifdef _WIN32 status = shutdown(sock, SD_BOTH); - if (status == 0) { status = closesocket(sock); } + if (status == 0) + { + status = closesocket(sock); + } #else status = shutdown(sock, SHUT_RDWR); - if (status == 0) { status = close(sock); } + if (status == 0) + { + status = close(sock); + } #endif return status; - } }; -} -#endif \ No newline at end of file +} // namespace masesk +#endif diff --git a/test/test-client/CMakeLists.txt b/test/test-client/CMakeLists.txt deleted file mode 100644 index 8a58e5d..0000000 --- a/test/test-client/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -add_executable( client - Client.cpp -) - -target_link_libraries(client easy-socket) diff --git a/test/test-server/CMakeLists.txt b/test/test-server/CMakeLists.txt deleted file mode 100644 index d76c4ae..0000000 --- a/test/test-server/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -add_executable( server - Server.cpp -) - -target_link_libraries(server easy-socket)