Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using HawkNetworking;
- using Steamworks;
- using Steamworks.Data;
- using System;
- public class SteamP2PNetworkManager : HawkNetworkManager
- {
- private static int ConnectionID = 0;
- private Steamworks.Data.Lobby lobby;
- private bool bIsConnected = false;
- private bool bIsConnecting = false;
- private bool bIsServer = false;
- // Converted it to use a lookup the same as HawkDeliveryMethod
- private P2PSend[] deliveryMethods = new P2PSend[]
- {
- P2PSend.Unreliable,
- P2PSend.Reliable
- };
- protected override void OnEnable()
- {
- base.OnEnable();
- SteamMatchmaking.OnLobbyCreated += OnLobbyCreated;
- SteamMatchmaking.OnLobbyEntered += OnLobbyEntered;
- SteamMatchmaking.OnLobbyMemberJoined += OnPlayerJoined;
- SteamMatchmaking.OnLobbyMemberLeave += OnPlayerLeft;
- SteamNetworking.OnP2PSessionRequest += OnP2PSessionRequest;
- SteamNetworking.OnP2PConnectionFailed += OnP2PConnectionFailed;
- SteamMatchmaking.OnChatMessage += OnLobbyChatMessage;
- }
- protected override void OnDisable()
- {
- base.OnDisable();
- SteamMatchmaking.OnLobbyCreated -= OnLobbyCreated;
- SteamMatchmaking.OnLobbyEntered -= OnLobbyEntered;
- SteamMatchmaking.OnLobbyMemberJoined -= OnPlayerJoined;
- SteamMatchmaking.OnLobbyMemberLeave -= OnPlayerLeft;
- SteamNetworking.OnP2PSessionRequest -= OnP2PSessionRequest;
- SteamNetworking.OnP2PConnectionFailed -= OnP2PConnectionFailed;
- SteamMatchmaking.OnChatMessage -= OnLobbyChatMessage;
- }
- public override void HostServer(int maxPlayers)
- {
- if (!SteamClient.IsLoggedOn || IsConnected() || IsConnecting()) return;
- bIsConnecting = true;
- SteamMatchmaking.CreateLobbyAsync(maxPlayers);
- }
- public override void JoinServer()
- {
- }
- public void JoinLobby(Lobby lobbyid)
- {
- if (!SteamClient.IsLoggedOn || IsConnected() || IsConnecting()) return;
- bIsConnecting = true;
- SteamMatchmaking.JoinLobbyAsync(lobbyid.Id);
- }
- public override void Disconnect()
- {
- if (!IsConnected()) return;
- lobby.Leave();
- bIsConnected = false;
- bIsConnecting = false;
- bIsServer = false;
- OnDisconnect();
- }
- public override void DisconnectPlayer(HawkConnection connection)
- {
- SteamNetworking.CloseP2PSessionWithUser(((SteamConnection)connection).steamId);
- }
- protected override void DoSend(HawkDeliveryMethod deliveryMethod, HawkConnection receiver, byte[] data)
- {
- SteamConnection connection = (SteamConnection)receiver;
- SteamNetworking.SendP2PPacket(connection.steamId, data, data.Length, 0, deliveryMethods[(int)deliveryMethod]);
- }
- protected override void FixedUpdate()
- {
- if (!SteamClient.IsLoggedOn) return;
- base.FixedUpdate();
- while (SteamNetworking.IsP2PPacketAvailable())
- {
- P2Packet? packet = SteamNetworking.ReadP2PPacket();
- if (packet.HasValue)
- {
- // Ensure this is a packet with data and not a p2pacceptmessage
- if (packet.Value.Data.Length > 0)
- {
- SteamConnection connection = (SteamConnection)connections.Find(x => ((SteamConnection)x).steamId == packet.Value.SteamId);
- OnMessageRecieved(connection, packet.Value.Data);
- }
- }
- }
- }
- #region Callbacks
- void OnLobbyChatMessage(Lobby lobby, Friend friend, string message)
- {
- Debug.Log("SteamP2P: OnLobbyChatMessage(" + message + ")");
- }
- void OnLobbyCreated(Result result, Lobby lobby)
- {
- Debug.Log("SteamP2P: OnLobbyCreated");
- this.lobby = lobby;
- lobby.SetPublic();
- lobby.SetJoinable(true);
- bIsServer = true;
- bIsConnecting = false;
- bIsConnected = true;
- SteamConnection connection = new SteamConnection();
- connection.Id = -1;
- connection.IsHost = true;
- connection.Me = true;
- connection.steamId = lobby.Owner.Id;
- OnPlayerConnected(connection);
- OnServerCreated();
- }
- void OnLobbyEntered(Lobby lobby)
- {
- Debug.Log("SteamP2P: OnLobbyEntered");
- bIsConnecting = false;
- bIsConnected = true;
- if (!IsServer())
- {
- SteamConnection connection = new SteamConnection();
- connection.Id = 0;
- connection.IsHost = true;
- connection.steamId = lobby.Owner.Id;
- OnPlayerConnected(connection);
- }
- }
- void OnPlayerJoined(Lobby lobby, Friend friend)
- {
- if (!IsServer() || friend.IsMe) return;
- Debug.Log("SteamP2P: OnPlayerJoined");
- SteamConnection connection = new SteamConnection();
- connection.Id = ConnectionID++;
- connection.steamId = friend.Id;
- OnPlayerConnected(connection);
- }
- void OnPlayerLeft(Lobby lobby, Friend friend)
- {
- if (!IsServer() || friend.IsMe) return;
- Debug.Log("SteamP2P: OnPlayerLeft");
- HawkConnection connection = connections.Find(x => ((SteamConnection)x).steamId == friend.Id);
- if (connection != null)
- {
- OnPlayerDisconnected(connection);
- }
- }
- void OnP2PConnectionFailed(SteamId player, P2PSessionError sessionError)
- {
- Debug.Log("SteamP2P: OnP2PConnectionFailed");
- OnErrorOccured(HawkSocketError.Error, "P2P Failed");
- }
- void OnP2PSessionRequest(SteamId player)
- {
- Debug.Log("SteamP2P: OnP2PSessionRequest");
- SteamNetworking.AcceptP2PSessionWithUser(player);
- }
- #endregion
- #region Getters/Setters
- public override bool IsConnected()
- {
- return base.IsConnected() || bIsConnected;
- }
- public bool IsConnecting()
- {
- return bIsConnecting;
- }
- public override bool IsServer()
- {
- return base.IsServer() || bIsServer;
- }
- public Lobby? GetLobby()
- {
- return lobby;
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement