//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
#pragma once
#include "macros.h"
#include "XboxNetworkMeshDiagnosticsTraceLevel.h"
#include "MeshThread.h"
#include "MeshPacketManager.h"
#include "MeshConnection.h"
#include "MeshEvents.h"
namespace Microsoft {
namespace Xbox {
namespace Samples {
namespace NetworkMesh {
ref class MeshPacketManager;
#define DEFAULT_HEARTBEAT_PERIOD_MILLISECONDS 2048
public ref class MeshManager sealed
{
public:
///
/// Pass in the template for the title's SDA and kick off the WSA initialization
///
///
MeshManager(
uint8 localConsoleId,
Platform::String^ secureDeviceAssociationTemplateName,
Platform::String^ localConsoleName,
bool dropOutOfOrderPackets
);
Platform::String^ GetLocalConsoleDisplayName();
Platform::String^ GetLocalConsoleName();
void SetLocalConsoleName(Platform::String^ consoleName);
///
/// Return the template being used by local mesh manager
///
Windows::Xbox::Networking::SecureDeviceAssociationTemplate^ GetSecureDeviceAssociationTemplate();
///
/// Pass in the SDA you want to connect to.
///
///
void ConnectToAddress(Windows::Xbox::Networking::SecureDeviceAddress^ secureDeviceAddress, Platform::String^ debugName );
///
///
Windows::Foundation::Collections::IVectorView^ GetConnections();
///
///
Windows::Foundation::Collections::IVectorView^ GetConnectionsByType(ConnectionStatus type);
///
///
MeshConnection^ GetConnectionFromAssociation(Windows::Xbox::Networking::SecureDeviceAssociation^ association);
///
///
MeshConnection^ GetConnectionFromSecureDeviceAddress(Windows::Xbox::Networking::SecureDeviceAddress^ address);
MeshConnection^ GetConnectionFromConsoleId(uint8 consoleId);
///
/// This will destroy the association and remove the connection from the list of all connections.
///
void DestroyConnection( MeshConnection^ connection);
///
/// Given a secure device address, this will destroy the association
/// and remove the connection from the list of all connections.
///
void DisconectFromAddress( Windows::Xbox::Networking::SecureDeviceAddress^ address);
///
/// Deletes all connections and destroys all associations.
///
void DestroyAndDisconnectAll();
///
/// Cleanup winsock
///
void Shutdown();
///
///
Microsoft::Xbox::Samples::NetworkMesh::MeshPacketManager^ GetMeshPacketManager();
///
/// Gets the Heartbeat period (in milliseconds)
///
UINT GetHeartbeatPeriod();
///
/// sets the Heartbeat period (in milliseconds)
///
void SetHeartbeatPeriod(UINT periodInMilliseconds);
///
/// // Warn the game that this connection is being disconnected. The game would have to then explicitly
/// call DisconnectFromAddress or DisconnectFromConnection to remove him from the connection list, if needed.
/// Otherwise, we will retry connecting to him.
///
event Windows::Foundation::EventHandler^ OnDisconnected;
///
/// This event is triggered when a proper handshake has been established between 2 connections.
///
event Windows::Foundation::EventHandler^ OnPostHandshake;
///
/// This event is triggered upon receiving heartbeats from remote connection.
///
event Windows::Foundation::EventHandler^ OnHeartbeat;
event Windows::Foundation::EventHandler^ OnDebugMessage;
internal:
void OnAssociationChange(
Windows::Xbox::Networking::SecureDeviceAssociationStateChangedEventArgs^ args,
Windows::Xbox::Networking::SecureDeviceAssociation^ association );
///
/// Return the template being used by local mesh manager
///
void RefreshConnections();
private:
Concurrency::critical_section m_connectionsLock;
Microsoft::Xbox::Samples::NetworkMesh::MeshPacketManager^ m_meshPacketManager;
Platform::String^ m_localConsoleName;
MeshThread^ m_autoConnectThread;
Windows::Xbox::Networking::SecureDeviceAssociationTemplate^ m_associationTemplate;
std::vector m_connections;
bool m_dropOutOfOrderPackets;
MeshThread^ m_heartbeatThread;
void Initialize(uint8 localConsoleId);
void RegisterMeshPacketEventHandlers();
MeshConnection^ AddConnection(
Windows::Xbox::Networking::SecureDeviceAddress^ secureDeviceAddress,
Windows::Xbox::Networking::SecureDeviceAssociation^ secureDeviceAssociation,
bool inComingAssociation,
ConnectionStatus connectionStatus
);
bool AreSecureDeviceAddressesEqual(
Windows::Xbox::Networking::SecureDeviceAddress^ secureDeviceAddress1,
Windows::Xbox::Networking::SecureDeviceAddress^ secureDeviceAddress2
);
///
/// Deletes the connection from the list of all connections.
///
void DeleteConnection(MeshConnection^ connection);
void DestroyAllTemplateAssociations();
bool DoesConnectionExistInList(Windows::Foundation::Collections::IVectorView^ list, MeshConnection^ connection);
///////////////////////
// Events
Windows::Foundation::EventRegistrationToken m_associationIncomingToken;
Windows::Foundation::EventRegistrationToken m_onHelloReceivedToken;
Windows::Foundation::EventRegistrationToken m_onHeartbeatReceivedToken;
Windows::Foundation::EventRegistrationToken m_onDebugMessageReceivedToken;
void OnHeartbeatReceived( Microsoft::Xbox::Samples::NetworkMesh::MeshHeartbeatReceivedEvent^ args );
void OnHelloReceived( Microsoft::Xbox::Samples::NetworkMesh::MeshHelloReceivedEvent^ args );
void OnDebugMessageReceived( Microsoft::Xbox::Samples::NetworkMesh::DebugMessageEventArgs^ args );
void OnAutoConnectWorkerThreadDoWork ( Microsoft::Xbox::Samples::NetworkMesh::ProcessThreadsEventArgs^ args );
void OnAssociationIncoming(
Windows::Xbox::Networking::SecureDeviceAssociationTemplate^ associationTemplate,
Windows::Xbox::Networking::SecureDeviceAssociationIncomingEventArgs^ args
);
void OnHeartbeatWorkerThreadDoWork( Microsoft::Xbox::Samples::NetworkMesh::ProcessThreadsEventArgs^ args );
///////////////////////
// Logging
void LogComment( Platform::String^ strText );
void LogCommentFormat( LPCWSTR strMsg, ... );
};
}}}}