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

Expose GetClientUserData on YojimboServer class. Fix address serialization bug in matcher code. #201

Merged
merged 6 commits into from
Jul 3, 2024
Merged
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
2 changes: 2 additions & 0 deletions include/yojimbo_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ namespace yojimbo

uint64_t GetClientId( int clientIndex ) const;

const uint8_t * GetClientUserData( int clientIndex ) const;

netcode_address_t * GetClientAddress( int clientIndex ) const;

int GetNumConnectedClients() const;
Expand Down
7 changes: 7 additions & 0 deletions include/yojimbo_server_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ namespace yojimbo

virtual uint64_t GetClientId( int clientIndex ) const = 0;

/**
Get the user data of the client.
@param clientIndex the index of the client slot in [0,maxClients-1], where maxClients corresponds to the value passed into the last call to Server::Start.
@returns The user data of the client.
*/
const uint8_t * GetClientUserData( int clientIndex ) const;

/**
Get the address of the client
@param clientIndex the index of the client slot in [0,maxClients-1], where maxClients corresponds to the value passed into the last call to Server::Start.
Expand Down
9 changes: 5 additions & 4 deletions matcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ func writeAddresses(buffer []byte, addresses []net.UDPAddr) int {
buffer[offset+4] = ipv4[3]
buffer[offset+5] = (byte)(port & 0xFF)
buffer[offset+6] = (byte)(port >> 8)
offset += 7
} else {
buffer[offset] = addressIPV6
copy(buffer[offset+1:], addr.IP)
buffer[offset+17] = (byte)(port & 0xFF)
buffer[offset+18] = (byte)(port >> 8)
offset += 19
}
offset += 19
}
return offset
}
Expand Down Expand Up @@ -178,9 +179,9 @@ func (token *connectToken) Write(buffer []byte) error {
return errors.Wrap(err, "failed to encrypt message")
}
binary.LittleEndian.PutUint32(buffer[connectTokenPrivateBytes+61:], (uint32)(token.TimeoutSeconds))
offset := writeAddresses(buffer[1024+61+4:], token.ServerAddresses)
copy(buffer[1024+61+4+offset:], token.ClientToServerKey[:])
copy(buffer[1024+61+4+offset+keyBytes:], token.ServerToClientKey[:])
offset := writeAddresses(buffer[connectTokenPrivateBytes+61+4:], token.ServerAddresses)
copy(buffer[connectTokenPrivateBytes+61+4+offset:], token.ClientToServerKey[:])
copy(buffer[connectTokenPrivateBytes+61+4+offset+keyBytes:], token.ServerToClientKey[:])
return nil
}

Expand Down
11 changes: 8 additions & 3 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ solution "Yojimbo"
configurations { "Debug", "Release" }
includedirs { ".", "include", "sodium", "tlsf", "netcode", "reliable", "serialize" }
if not os.istarget "windows" then
targetdir "bin/"
targetdir "bin/"
end
rtti "Off"
warnings "Extra"
Expand Down Expand Up @@ -64,27 +64,31 @@ project "client"
links { "yojimbo", "sodium-builtin", "tlsf", "netcode", "reliable" }
filter "system:not windows"
links { "yojimbo", "sodium", "tlsf", "netcode", "reliable" }
libdirs { "/opt/homebrew/lib" }

project "server"
files { "server.cpp", "shared.h" }
filter "system:windows"
links { "yojimbo", "sodium-builtin", "tlsf", "netcode", "reliable" }
filter "system:not windows"
links { "yojimbo", "sodium", "tlsf", "netcode", "reliable" }
libdirs { "/opt/homebrew/lib" }

project "loopback"
files { "loopback.cpp", "shared.h" }
filter "system:windows"
links { "yojimbo", "sodium-builtin", "tlsf", "netcode", "reliable" }
filter "system:not windows"
links { "yojimbo", "sodium", "tlsf", "netcode", "reliable" }
libdirs { "/opt/homebrew/lib" }

project "soak"
files { "soak.cpp", "shared.h" }
filter "system:windows"
links { "yojimbo", "sodium-builtin", "tlsf", "netcode", "reliable" }
filter "system:not windows"
links { "yojimbo", "sodium", "tlsf", "netcode", "reliable" }
libdirs { "/opt/homebrew/lib" }

project "test"
files { "test.cpp" }
Expand All @@ -93,6 +97,7 @@ project "test"
links { "yojimbo", "sodium-builtin", "tlsf", "netcode", "reliable" }
filter "system:not windows"
links { "yojimbo", "sodium", "tlsf", "netcode", "reliable" }
libdirs { "/opt/homebrew/lib" }

newaction
{
Expand All @@ -102,7 +107,7 @@ newaction

execute = function ()

files_to_delete =
files_to_delete =
{
"Makefile",
"*.make",
Expand All @@ -120,7 +125,7 @@ newaction
"*.xcworkspace"
}

directories_to_delete =
directories_to_delete =
{
"obj",
"ipch",
Expand Down
33 changes: 19 additions & 14 deletions source/yojimbo_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
Expand All @@ -31,7 +31,7 @@

namespace yojimbo
{
Server::Server( Allocator & allocator, const uint8_t privateKey[], const Address & address, const ClientServerConfig & config, Adapter & adapter, double time )
Server::Server( Allocator & allocator, const uint8_t privateKey[], const Address & address, const ClientServerConfig & config, Adapter & adapter, double time )
: BaseServer( allocator, config, adapter, time )
{
yojimbo_assert( KeyBytes == NETCODE_KEY_BYTES );
Expand All @@ -52,12 +52,12 @@ namespace yojimbo
{
if ( IsRunning() )
Stop();

BaseServer::Start( maxClients );

char addressString[MaxAddressLength];
m_address.ToString( addressString, MaxAddressLength );

struct netcode_server_config_t netcodeConfig;
netcode_default_server_config(&netcodeConfig);
netcodeConfig.protocol_id = m_config.protocolId;
Expand All @@ -68,15 +68,15 @@ namespace yojimbo
netcodeConfig.callback_context = this;
netcodeConfig.connect_disconnect_callback = StaticConnectDisconnectCallbackFunction;
netcodeConfig.send_loopback_packet_callback = StaticSendLoopbackPacketCallbackFunction;

m_server = netcode_server_create(addressString, &netcodeConfig, GetTime());

if ( !m_server )
{
Stop();
return;
}

netcode_server_start( m_server, maxClients );

m_boundAddress.SetPort( netcode_server_get_port( m_server ) );
Expand Down Expand Up @@ -186,6 +186,11 @@ namespace yojimbo
return netcode_server_client_id( m_server, clientIndex );
}

const uint8_t * Server::GetClientUserData( int clientIndex ) const
{
return (const uint8_t*)netcode_server_client_user_data( m_server, clientIndex );
}

netcode_address_t * Server::GetClientAddress( int clientIndex ) const
{
return netcode_server_client_address( m_server, clientIndex );
Expand Down
Loading