-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
491 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
Language: Cpp | ||
IndentWidth: 4 | ||
ColumnLimit: '80' | ||
NamespaceIndentation: All | ||
AccessModifierOffset: -4 | ||
ConstructorInitializerIndentWidth: 4 | ||
ContinuationIndentWidth: 4 | ||
AlignAfterOpenBracket: 'AlwaysBreak' | ||
BinPackArguments: 'false' | ||
BinPackParameters: 'false' | ||
PointerAlignment: Left | ||
ReferenceAlignment: Pointer | ||
SortIncludes: CaseSensitive | ||
SortUsingDeclarations: true | ||
SpaceAfterCStyleCast: false | ||
SpaceAfterLogicalNot: false | ||
SpaceAfterTemplateKeyword: false | ||
SpaceBeforeAssignmentOperators: true | ||
SpaceBeforeCaseColon: false | ||
SpaceBeforeCpp11BracedList: true | ||
SpaceBeforeCtorInitializerColon: true | ||
SpaceBeforeInheritanceColon: true | ||
SpaceBeforeRangeBasedForLoopColon: true | ||
SpaceBeforeSquareBrackets: false | ||
SpacesInAngles: Never | ||
AllowShortBlocksOnASingleLine: Empty | ||
AllowShortCaseLabelsOnASingleLine: false | ||
AllowShortFunctionsOnASingleLine: Empty | ||
AllowShortIfStatementsOnASingleLine: WithoutElse | ||
AlwaysBreakAfterReturnType: None | ||
AlwaysBreakBeforeMultilineStrings: true | ||
AlwaysBreakTemplateDeclarations: Yes | ||
# BreakAfterAttributes: Always | ||
BreakBeforeConceptDeclarations: Always | ||
BreakBeforeBinaryOperators: NonAssignment | ||
CompactNamespaces: false | ||
BreakStringLiterals: true | ||
Cpp11BracedListStyle: false | ||
EmptyLineBeforeAccessModifier: Always | ||
FixNamespaceComments: true | ||
IncludeBlocks: Merge | ||
QualifierAlignment: Left # Left - west const, Right - east const | ||
ReflowComments: true | ||
RequiresClausePosition: OwnLine | ||
SeparateDefinitionBlocks: Always | ||
PackConstructorInitializers: NextLine #NextLineOnly is better | ||
BreakConstructorInitializers: BeforeComma | ||
BreakInheritanceList: BeforeComma | ||
BreakBeforeBraces: Custom | ||
BraceWrapping: | ||
AfterClass: true | ||
AfterControlStatement: true | ||
AfterEnum: true | ||
AfterFunction: true | ||
AfterNamespace: true | ||
AfterObjCDeclaration: true | ||
AfterStruct: true | ||
AfterUnion: true | ||
AfterExternBlock: true | ||
BeforeCatch: true | ||
BeforeElse: true | ||
BeforeLambdaBody: true | ||
BeforeWhile: false | ||
IndentBraces: false | ||
SplitEmptyFunction: true | ||
SplitEmptyRecord: true | ||
SplitEmptyNamespace: true | ||
|
||
# Unsupported in MSVC 17.5.2 | ||
# LanguageStandard: Cpp20 | ||
# SpaceBeforeJsonColon: false | ||
# QualifierOrder: ['inline', 'static', 'constexpr', 'volatile', 'const', 'type', ] | ||
# RequiresExpressionIndentation: OuterScope | ||
# NextLineOnly for PackConstructorInitializers | ||
# BreakAfterAttributes: Always |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
cmake_minimum_required ( VERSION 3.26 ) | ||
|
||
file ( | ||
COPY "${CMAKE_BINARY_DIR}/.clang-format" | ||
DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}" | ||
) | ||
|
||
project ( client-test ) | ||
|
||
add_executable ( ${PROJECT_NAME} Main.cpp ) | ||
|
||
target_link_libraries ( ${PROJECT_NAME} lib-network lib-misc ) | ||
|
||
autoset_target_compile_options ( ${PROJECT_NAME} FALSE ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <SFML/Network.hpp> | ||
#include <format> | ||
#include <iostream> | ||
|
||
import Network; | ||
|
||
int main(int, char*[]) | ||
{ | ||
auto&& client = Client::create("127.0.0.1", 10666); | ||
if (!client) | ||
{ | ||
std::println(std::cerr, "{}", client.error()); | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,116 @@ | ||
module; | ||
|
||
#pragma warning(push, 0) | ||
#include <SFML/Network.hpp> | ||
#pragma warning(pop) | ||
#include <cassert> | ||
#include <concepts> | ||
#include <expected> | ||
#include <format> | ||
#include <print> | ||
#include <stdexcept> | ||
#include <string> | ||
|
||
export module Client; | ||
|
||
import Message; | ||
import Error; | ||
import Memory; | ||
|
||
export class Client | ||
{ | ||
public: | ||
Client(const sf::IpAddress& address, unsigned short port) | ||
: remoteAddress(address), remotePort(port) | ||
{ | ||
} | ||
|
||
public: | ||
static [[nodiscard]] std::expected<Client, ErrorMessage> | ||
create(const sf::IpAddress& address, unsigned short port) | ||
{ | ||
try | ||
{ | ||
auto&& client = Client(address, port); | ||
if (auto&& result = client.bindToAnyPort(); !result) | ||
return std::unexpected(result.error()); | ||
|
||
auto&& id = client.registerToServer(); | ||
if (!id) return std::unexpected(id.error()); | ||
client.myClientId = *id; | ||
|
||
return client; | ||
} | ||
catch (std::exception& e) | ||
{ | ||
return std::unexpected(e.what()); | ||
} | ||
} | ||
|
||
private: | ||
ExpectSuccess bindToAnyPort() | ||
{ | ||
if (socket->bind(sf::Socket::AnyPort) != sf::Socket::Status::Done) | ||
{ | ||
return std::unexpected( | ||
std::format("Cannot bind socket to any port")); | ||
} | ||
|
||
myPort = socket->getLocalPort(); | ||
std::println("Socket bound to port {}", myPort); | ||
|
||
return ReturnFlag::Success; | ||
} | ||
|
||
std::expected<PlayerIdType, ErrorMessage> registerToServer() | ||
{ | ||
if (auto&& result = sendConnectPacket(); !result) | ||
return std::unexpected(result.error()); | ||
|
||
auto&& message = getConnectResponse(); | ||
if (!message) return std::unexpected(message.error()); | ||
return message->playerId; | ||
} | ||
|
||
ExpectSuccess sendConnectPacket() | ||
{ | ||
auto&& packet = Message { .type = MessageType::Connect }.toPacket(); | ||
if (socket->send(packet, remoteAddress, remotePort) | ||
!= sf::Socket::Status::Done) | ||
{ | ||
return std::unexpected(std::format( | ||
"Could not send message to {}:{}", | ||
remoteAddress.toString(), | ||
remotePort)); | ||
} | ||
} | ||
|
||
std::expected<Message, ErrorMessage> getConnectResponse() | ||
{ | ||
auto&& packet = sf::Packet(); | ||
if (socket->receive(packet, remoteAddress, remotePort) | ||
!= sf::Socket::Status::Done) | ||
{ | ||
return std::unexpected( | ||
std::format("Got no response from remote server")); | ||
} | ||
|
||
auto&& message = Message::parseMessage(packet); | ||
if (message.type != MessageType::ConnectConfirmed) | ||
{ | ||
return std::unexpected(std::format( | ||
"Expected ConnectConfirmed from the server, got {}", | ||
static_cast<std::underlying_type_t<MessageType>>( | ||
message.type))); | ||
} | ||
|
||
return message; | ||
} | ||
|
||
private: | ||
sf::IpAddress remoteAddress; | ||
unsigned short remotePort; | ||
mem::Box<sf::UdpSocket> socket; | ||
unsigned short myPort; | ||
PlayerIdType myClientId; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.