Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V0.3 #14

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

V0.3 #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,7 @@ FodyWeavers.xsd
*.exe
*.out
*.app


# Build
/build
16 changes: 9 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -35,20 +35,20 @@ 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(
FILES
"${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(
Expand All @@ -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()
113 changes: 98 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -43,58 +44,138 @@ 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 <iostream>
#include <masesk/EasySocket.hpp>
#include <string>
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 <iostream>
#include <masesk/EasySocket.hpp>
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 <iostream>
#include <masesk/EasySocket.hpp>
#include <string>
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;
}
```

### Functions

### TCP

#### Server Functions

* `void socketListen(std::string channelName, int port, std::function<void (std::string data)> callback);`
* `void socketListenTCP(const std::string &channelName, const std::uint16_t &port, std::function<void (const std::string &data)> 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<void (const std::string & data)> 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

Expand All @@ -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 ..`
24 changes: 22 additions & 2 deletions easy-socket.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions examples/test-tcp-client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_executable( tcp_client
tcp_client.cpp
)

target_link_libraries(tcp_client easy-socket)
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ 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 << "> ";
getline(cin, userInput);
if (userInput.size() <= 0) {
break;
}
socketManager.socketSend("test", userInput);
socketManager.socketSendTCP("test", userInput);
}
socketManager.closeConnection("test");
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<ProjectGuid>{4FFDB01A-3314-4B28-841A-1FE8CE3516CE}</ProjectGuid>
<RootNamespace>testclient</RootNamespace>
<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
<ProjectName>test-tcp-client</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
Expand Down Expand Up @@ -127,7 +128,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Client.cpp" />
<ClCompile Include="tcp_client.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Client.cpp">
<ClCompile Include="tcp_client.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions examples/test-tcp-server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_executable( tcp_server
tcp_server.cpp
)

target_link_libraries(tcp_server easy-socket)
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ void handleData(const std::string &data) {

int main() {
EasySocket socketManager;
socketManager.socketListen("test", 8080, &handleData);
socketManager.socketListenTCP<void, std::string>("test", 8080, &handleData);
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<ProjectGuid>{1CAD60FA-43FF-4595-84B3-B72ED7F3330F}</ProjectGuid>
<RootNamespace>testserver</RootNamespace>
<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
<ProjectName>test-tcp-server</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
Expand Down Expand Up @@ -127,7 +128,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Server.cpp" />
<ClCompile Include="tcp_server.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Server.cpp">
<ClCompile Include="tcp_server.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions examples/test-udp-receiver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_executable( udp_receiver
udp_receiver.cpp
)

target_link_libraries(udp_receiver easy-socket)
Loading