Skip to content

Commit

Permalink
moar
Browse files Browse the repository at this point in the history
  • Loading branch information
gafferongames committed Dec 25, 2023
1 parent 8a8aab3 commit 747613f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 54 deletions.
55 changes: 1 addition & 54 deletions yojimbo.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "yojimbo_channel.h"
#include "yojimbo_reliable_ordered_channel.h"
#include "yojimbo_unreliable_unordered_channel.h"
#include "yojimbo_connection.h"

/** @file */

Expand Down Expand Up @@ -77,60 +78,6 @@ void ShutdownYojimbo();

namespace yojimbo
{
/// Connection error level.

enum ConnectionErrorLevel
{
CONNECTION_ERROR_NONE = 0, ///< No error. All is well.
CONNECTION_ERROR_CHANNEL, ///< A channel is in an error state.
CONNECTION_ERROR_ALLOCATOR, ///< The allocator is an error state.
CONNECTION_ERROR_MESSAGE_FACTORY, ///< The message factory is in an error state.
CONNECTION_ERROR_READ_PACKET_FAILED, ///< Failed to read packet. Received an invalid packet?
};

/**
Sends and receives messages across a set of user defined channels.
*/

class Connection
{
public:

Connection( Allocator & allocator, MessageFactory & messageFactory, const ConnectionConfig & connectionConfig, double time );

~Connection();

void Reset();

bool CanSendMessage( int channelIndex ) const;

bool HasMessagesToSend( int channelIndex ) const;

void SendMessage( int channelIndex, Message * message, void *context = 0);

Message * ReceiveMessage( int channelIndex );

void ReleaseMessage( Message * message );

bool GeneratePacket( void * context, uint16_t packetSequence, uint8_t * packetData, int maxPacketBytes, int & packetBytes );

bool ProcessPacket( void * context, uint16_t packetSequence, const uint8_t * packetData, int packetBytes );

void ProcessAcks( const uint16_t * acks, int numAcks );

void AdvanceTime( double time );

ConnectionErrorLevel GetErrorLevel() { return m_errorLevel; }

private:

Allocator * m_allocator; ///< Allocator passed in to the connection constructor.
MessageFactory * m_messageFactory; ///< Message factory for creating and destroying messages.
ConnectionConfig m_connectionConfig; ///< Connection configuration.
Channel * m_channel[MaxChannels]; ///< Array of connection channels. Array size corresponds to m_connectionConfig.numChannels
ConnectionErrorLevel m_errorLevel; ///< The connection error level.
};

/**
Simulates packet loss, latency, jitter and duplicate packets.
This is useful during development, so your game is tested and played under real world conditions, instead of ideal LAN conditions.
Expand Down
87 changes: 87 additions & 0 deletions yojimbo_connection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Yojimbo Client/Server Network Library.
Copyright © 2016 - 2024, Mas Bandwidth LLC.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
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
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
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,
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.
*/

#ifndef YOJIMBO_CONNECTION_H
#define YOJIMBO_CONNECTION_H

#include "yojimbo_config.h"

namespace yojimbo
{
/// Connection error level.

enum ConnectionErrorLevel
{
CONNECTION_ERROR_NONE = 0, ///< No error. All is well.
CONNECTION_ERROR_CHANNEL, ///< A channel is in an error state.
CONNECTION_ERROR_ALLOCATOR, ///< The allocator is an error state.
CONNECTION_ERROR_MESSAGE_FACTORY, ///< The message factory is in an error state.
CONNECTION_ERROR_READ_PACKET_FAILED, ///< Failed to read packet. Received an invalid packet?
};

/**
Sends and receives messages across a set of user defined channels.
*/

class Connection
{
public:

Connection( Allocator & allocator, MessageFactory & messageFactory, const ConnectionConfig & connectionConfig, double time );

~Connection();

void Reset();

bool CanSendMessage( int channelIndex ) const;

bool HasMessagesToSend( int channelIndex ) const;

void SendMessage( int channelIndex, Message * message, void *context = 0);

Message * ReceiveMessage( int channelIndex );

void ReleaseMessage( Message * message );

bool GeneratePacket( void * context, uint16_t packetSequence, uint8_t * packetData, int maxPacketBytes, int & packetBytes );

bool ProcessPacket( void * context, uint16_t packetSequence, const uint8_t * packetData, int packetBytes );

void ProcessAcks( const uint16_t * acks, int numAcks );

void AdvanceTime( double time );

ConnectionErrorLevel GetErrorLevel() { return m_errorLevel; }

private:

Allocator * m_allocator; ///< Allocator passed in to the connection constructor.
MessageFactory * m_messageFactory; ///< Message factory for creating and destroying messages.
ConnectionConfig m_connectionConfig; ///< Connection configuration.
Channel * m_channel[MaxChannels]; ///< Array of connection channels. Array size corresponds to m_connectionConfig.numChannels
ConnectionErrorLevel m_errorLevel; ///< The connection error level.
};
}

#endif // #ifndef YOJIMBO_CONNECTION_H

0 comments on commit 747613f

Please sign in to comment.