adding multiplayer
This commit is contained in:
237
Runtime/Engine/Code/Steam/SteamNetworkManager.cs
Normal file
237
Runtime/Engine/Code/Steam/SteamNetworkManager.cs
Normal file
@@ -0,0 +1,237 @@
|
||||
#define RR_LOCALHOST_ONLY
|
||||
|
||||
using System;
|
||||
using R3;
|
||||
using RebootKit.Engine.Foundation;
|
||||
using RebootKit.Engine.Main;
|
||||
using Steamworks;
|
||||
using Steamworks.Data;
|
||||
using Logger = RebootKit.Engine.Foundation.Logger;
|
||||
|
||||
namespace RebootKit.Engine.Steam {
|
||||
// class SteamNetworkManager : INetworkManager {
|
||||
// static readonly Logger s_Logger = new Logger(nameof(SteamNetworkManager));
|
||||
//
|
||||
// const int k_DefaultPort = 420;
|
||||
//
|
||||
// SocketManager m_SocketManager;
|
||||
// ConnectionManager m_ConnectionManager;
|
||||
// IDisposable m_TickDisposable;
|
||||
//
|
||||
// SteamId m_HostSteamID;
|
||||
//
|
||||
// public void Initialize(INetworkManagerEventHandler eventHandler) {
|
||||
// m_TickDisposable = Observable.EveryUpdate()
|
||||
// .Subscribe(_ => Tick());
|
||||
//
|
||||
// SteamNetworkingUtils.DebugLevel = NetDebugOutput.Debug;
|
||||
//
|
||||
// SteamNetworkingSockets.OnConnectionStatusChanged += OnConnectionStatusChanged;
|
||||
// SteamNetworkingUtils.OnDebugOutput += OnSteamNetworkDebugOutput;
|
||||
//
|
||||
// #if !RR_LOCALHOST_ONLY
|
||||
// SteamNetworkingUtils.InitRelayNetworkAccess();
|
||||
// #endif
|
||||
// }
|
||||
//
|
||||
// public void Shutdown() {
|
||||
// Disconnect();
|
||||
//
|
||||
// SteamNetworkingUtils.OnDebugOutput -= OnSteamNetworkDebugOutput;
|
||||
// SteamNetworkingSockets.OnConnectionStatusChanged -= OnConnectionStatusChanged;
|
||||
//
|
||||
// m_TickDisposable.Dispose();
|
||||
// }
|
||||
//
|
||||
// public bool IsServer() {
|
||||
// return m_SocketManager != null;
|
||||
// }
|
||||
//
|
||||
// public bool IsClient() {
|
||||
// return m_ConnectionManager != null;
|
||||
// }
|
||||
//
|
||||
// public bool StartServer() {
|
||||
// Disconnect();
|
||||
//
|
||||
// s_Logger.Info("Creating server...");
|
||||
//
|
||||
// try {
|
||||
// m_HostSteamID = SteamNetworkingSockets.Identity.SteamId;
|
||||
//
|
||||
// #if RR_LOCALHOST_ONLY
|
||||
// m_SocketManager = SteamNetworkingSockets.CreateNormalSocket(NetAddress.LocalHost(2137), new ServerCallbacks());
|
||||
// m_ConnectionManager = SteamNetworkingSockets.ConnectNormal(NetAddress.LocalHost(2137), new ClientCallbacks());
|
||||
// #else
|
||||
// m_SocketManager = SteamNetworkingSockets.CreateRelaySocket<SocketManager>(k_DefaultPort);
|
||||
// m_SocketManager.Interface = new ServerCallbacks();
|
||||
// m_ConnectionManager =
|
||||
// SteamNetworkingSockets.ConnectRelay<ConnectionManager>(m_HostSteamID, k_DefaultPort);
|
||||
// m_ConnectionManager.Interface = new ClientCallbacks();
|
||||
// #endif
|
||||
// } catch (Exception e) {
|
||||
// s_Logger.Error($"Failed to create server: {e.Message}");
|
||||
// m_SocketManager = null;
|
||||
// m_ConnectionManager = null;
|
||||
//
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// public void StopServer() {
|
||||
// Disconnect();
|
||||
// }
|
||||
//
|
||||
// public bool Connect(ulong serverID) {
|
||||
// if (IsServer()) {
|
||||
// s_Logger.Error("Cannot connect to a server while running as a server.");
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// Disconnect();
|
||||
//
|
||||
// s_Logger.Info("Connecting to server with steam ID: " + serverID);
|
||||
// try {
|
||||
// #if RR_LOCALHOST_ONLY
|
||||
// m_ConnectionManager =
|
||||
// SteamNetworkingSockets.ConnectNormal(NetAddress.LocalHost(2137), new ClientCallbacks());
|
||||
// #else
|
||||
// m_ConnectionManager = SteamNetworkingSockets.ConnectRelay<ConnectionManager>(serverID, k_DefaultPort);
|
||||
// m_ConnectionManager.Interface = new ClientCallbacks();
|
||||
// #endif
|
||||
//
|
||||
// m_HostSteamID = serverID;
|
||||
// } catch (Exception e) {
|
||||
// s_Logger.Error($"Failed to connect to server with ID {serverID}: {e.Message}");
|
||||
// m_ConnectionManager = null;
|
||||
//
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// public void Disconnect() {
|
||||
// if (m_ConnectionManager != null) {
|
||||
// s_Logger.Info("Disconnecting from the server...");
|
||||
// m_ConnectionManager.Close();
|
||||
// m_ConnectionManager = null;
|
||||
// }
|
||||
//
|
||||
// if (m_SocketManager != null) {
|
||||
// s_Logger.Info("Shutting down the server...");
|
||||
// m_SocketManager.Close();
|
||||
// m_SocketManager = null;
|
||||
// }
|
||||
// }
|
||||
// public void Send(ulong clientID, NetworkMessage message, SendMode mode) {
|
||||
// if (!IsServer()) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// foreach (Connection connection in m_SocketManager.Connected) {
|
||||
// connection.SendMessage(new byte[] {
|
||||
// 0xDE,
|
||||
// 0xAD,
|
||||
// 0xBE,
|
||||
// 0xEF
|
||||
// }, SendType.Reliable);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public void Broadcast(NetworkMessage message, SendMode mode) {
|
||||
// if (!IsServer()) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// foreach (Connection connection in m_SocketManager.Connected) {
|
||||
// connection.SendMessage(new byte[] {
|
||||
// 0xDE,
|
||||
// 0xAD,
|
||||
// 0xBE,
|
||||
// 0xEF
|
||||
// }, SendType.Reliable);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// void OnSteamNetworkDebugOutput(NetDebugOutput level, string message) {
|
||||
// LogLevel logLevel = level switch {
|
||||
// NetDebugOutput.Debug => LogLevel.Debug,
|
||||
// NetDebugOutput.Msg => LogLevel.Info,
|
||||
// NetDebugOutput.Warning => LogLevel.Warning,
|
||||
// NetDebugOutput.Error => LogLevel.Error,
|
||||
// _ => LogLevel.Info
|
||||
// };
|
||||
//
|
||||
// s_Logger.Log(logLevel, message);
|
||||
// }
|
||||
//
|
||||
// void Tick() {
|
||||
// m_SocketManager?.Receive();
|
||||
// m_ConnectionManager?.Receive();
|
||||
// }
|
||||
//
|
||||
// void OnConnectionStatusChanged(Connection connection, ConnectionInfo info) {
|
||||
// s_Logger.Info($"OnConnectionStatusChanged: {connection.Id} - {info.Identity} - Status: {info.State}");
|
||||
// }
|
||||
//
|
||||
// class ServerCallbacks : ISocketManager {
|
||||
// public void OnConnecting(Connection connection, ConnectionInfo data) {
|
||||
// s_Logger.Info($"OnConnecting: {connection.Id} - {data.Identity}");
|
||||
//
|
||||
// connection.Accept();
|
||||
// }
|
||||
//
|
||||
// public void OnConnected(Connection connection, ConnectionInfo data) {
|
||||
// s_Logger.Info($"OnConnected: {connection.Id} - {data.Identity}");
|
||||
//
|
||||
// connection.SendMessage(new byte[] {
|
||||
// 0xBE,
|
||||
// 0xFE,
|
||||
// 0x00,
|
||||
// 0x00
|
||||
// }, 0, 4, 0);
|
||||
// }
|
||||
//
|
||||
// public void OnDisconnected(Connection connection, ConnectionInfo data) {
|
||||
// s_Logger.Info($"OnDisconnected: {connection.Id} - {data.Identity}");
|
||||
// }
|
||||
//
|
||||
// public void OnMessage(Connection connection,
|
||||
// NetIdentity identity,
|
||||
// IntPtr data,
|
||||
// int size,
|
||||
// long messageNum,
|
||||
// long recvTime,
|
||||
// int channel) {
|
||||
// byte[] buffer = new byte[size];
|
||||
// System.Runtime.InteropServices.Marshal.Copy(data, buffer, 0, size);
|
||||
//
|
||||
// s_Logger.Info($"OnMessage: {connection.Id} - {identity} - Size: {size} - Channel: {channel}");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// class ClientCallbacks : IConnectionManager {
|
||||
// public void OnConnected(ConnectionInfo info) {
|
||||
// s_Logger.Info("ConnectionOnConnected");
|
||||
// }
|
||||
//
|
||||
// public void OnConnecting(ConnectionInfo info) {
|
||||
// s_Logger.Info("ConnectionOnConnecting");
|
||||
// }
|
||||
//
|
||||
// public void OnDisconnected(ConnectionInfo info) {
|
||||
// s_Logger.Info("ConnectionOnDisconnected");
|
||||
// }
|
||||
//
|
||||
// public void OnMessage(IntPtr data, int size, long messageNum, long recvTime, int channel) {
|
||||
// byte[] buffer = new byte[size];
|
||||
// System.Runtime.InteropServices.Marshal.Copy(data, buffer, 0, size);
|
||||
//
|
||||
// s_Logger.Info($"OnMessage: Size: {size} - Channel: {channel}");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user