SHARE
TWEET

Untitled

a guest Feb 24th, 2020 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using HawkNetworking;
  5. using Steamworks;
  6. using Steamworks.Data;
  7. using System;
  8.  
  9. public class SteamP2PNetworkManager : HawkNetworkManager
  10. {
  11.     private static int ConnectionID = 0;
  12.  
  13.     private Steamworks.Data.Lobby lobby;
  14.     private bool bIsConnected = false;
  15.     private bool bIsConnecting = false;
  16.     private bool bIsServer = false;
  17.  
  18.     // Converted it to use a lookup the same as HawkDeliveryMethod
  19.     private P2PSend[] deliveryMethods = new P2PSend[]
  20.     {
  21.         P2PSend.Unreliable,
  22.         P2PSend.Reliable
  23.     };
  24.  
  25.     protected override void OnEnable()
  26.     {
  27.         base.OnEnable();
  28.  
  29.         SteamMatchmaking.OnLobbyCreated += OnLobbyCreated;
  30.         SteamMatchmaking.OnLobbyEntered += OnLobbyEntered;
  31.         SteamMatchmaking.OnLobbyMemberJoined += OnPlayerJoined;
  32.         SteamMatchmaking.OnLobbyMemberLeave += OnPlayerLeft;
  33.         SteamNetworking.OnP2PSessionRequest += OnP2PSessionRequest;
  34.         SteamNetworking.OnP2PConnectionFailed += OnP2PConnectionFailed;
  35.         SteamMatchmaking.OnChatMessage += OnLobbyChatMessage;
  36.     }
  37.  
  38.     protected override void OnDisable()
  39.     {
  40.         base.OnDisable();
  41.  
  42.         SteamMatchmaking.OnLobbyCreated -= OnLobbyCreated;
  43.         SteamMatchmaking.OnLobbyEntered -= OnLobbyEntered;
  44.         SteamMatchmaking.OnLobbyMemberJoined -= OnPlayerJoined;
  45.         SteamMatchmaking.OnLobbyMemberLeave -= OnPlayerLeft;
  46.         SteamNetworking.OnP2PSessionRequest -= OnP2PSessionRequest;
  47.         SteamNetworking.OnP2PConnectionFailed -= OnP2PConnectionFailed;
  48.         SteamMatchmaking.OnChatMessage -= OnLobbyChatMessage;
  49.     }
  50.  
  51.     public override void HostServer(int maxPlayers)
  52.     {
  53.         if (!SteamClient.IsLoggedOn || IsConnected() || IsConnecting()) return;
  54.  
  55.         bIsConnecting = true;
  56.         SteamMatchmaking.CreateLobbyAsync(maxPlayers);
  57.     }
  58.  
  59.     public override void JoinServer()
  60.     {
  61.     }
  62.  
  63.     public void JoinLobby(Lobby lobbyid)
  64.     {
  65.         if (!SteamClient.IsLoggedOn || IsConnected() || IsConnecting()) return;
  66.  
  67.         bIsConnecting = true;
  68.         SteamMatchmaking.JoinLobbyAsync(lobbyid.Id);
  69.     }
  70.  
  71.     public override void Disconnect()
  72.     {
  73.         if (!IsConnected()) return;
  74.  
  75.         lobby.Leave();
  76.  
  77.         bIsConnected = false;
  78.         bIsConnecting = false;
  79.         bIsServer = false;
  80.  
  81.         OnDisconnect();
  82.     }
  83.  
  84.     public override void DisconnectPlayer(HawkConnection connection)
  85.     {
  86.         SteamNetworking.CloseP2PSessionWithUser(((SteamConnection)connection).steamId);
  87.     }
  88.  
  89.     protected override void DoSend(HawkDeliveryMethod deliveryMethod, HawkConnection receiver, byte[] data)
  90.     {
  91.         SteamConnection connection = (SteamConnection)receiver;
  92.         SteamNetworking.SendP2PPacket(connection.steamId, data, data.Length, 0, deliveryMethods[(int)deliveryMethod]);
  93.     }
  94.  
  95.     protected override void FixedUpdate()
  96.     {
  97.         if (!SteamClient.IsLoggedOn) return;
  98.  
  99.         base.FixedUpdate();
  100.  
  101.         while (SteamNetworking.IsP2PPacketAvailable())
  102.         {
  103.             P2Packet? packet = SteamNetworking.ReadP2PPacket();
  104.             if (packet.HasValue)
  105.             {
  106.                 // Ensure this is a packet with data and not a p2pacceptmessage
  107.                 if (packet.Value.Data.Length > 0)
  108.                 {
  109.                     SteamConnection connection = (SteamConnection)connections.Find(x => ((SteamConnection)x).steamId == packet.Value.SteamId);
  110.                     OnMessageRecieved(connection, packet.Value.Data);
  111.                 }
  112.             }
  113.         }
  114.     }
  115.  
  116.     #region Callbacks
  117.  
  118.     void OnLobbyChatMessage(Lobby lobby, Friend friend, string message)
  119.     {
  120.         Debug.Log("SteamP2P: OnLobbyChatMessage(" + message + ")");
  121.     }
  122.  
  123.     void OnLobbyCreated(Result result, Lobby lobby)
  124.     {
  125.         Debug.Log("SteamP2P: OnLobbyCreated");
  126.  
  127.         this.lobby = lobby;
  128.  
  129.         lobby.SetPublic();
  130.         lobby.SetJoinable(true);
  131.  
  132.         bIsServer = true;
  133.         bIsConnecting = false;
  134.         bIsConnected = true;
  135.  
  136.         SteamConnection connection = new SteamConnection();
  137.         connection.Id = -1;
  138.         connection.IsHost = true;
  139.         connection.Me = true;
  140.         connection.steamId = lobby.Owner.Id;
  141.         OnPlayerConnected(connection);
  142.  
  143.         OnServerCreated();
  144.     }
  145.  
  146.     void OnLobbyEntered(Lobby lobby)
  147.     {
  148.         Debug.Log("SteamP2P: OnLobbyEntered");
  149.  
  150.         bIsConnecting = false;
  151.         bIsConnected = true;
  152.  
  153.         if (!IsServer())
  154.         {
  155.             SteamConnection connection = new SteamConnection();
  156.             connection.Id = 0;
  157.             connection.IsHost = true;
  158.             connection.steamId = lobby.Owner.Id;
  159.             OnPlayerConnected(connection);
  160.         }
  161.     }
  162.  
  163.     void OnPlayerJoined(Lobby lobby, Friend friend)
  164.     {
  165.         if (!IsServer() || friend.IsMe) return;
  166.  
  167.         Debug.Log("SteamP2P: OnPlayerJoined");
  168.  
  169.         SteamConnection connection = new SteamConnection();
  170.         connection.Id = ConnectionID++;
  171.         connection.steamId = friend.Id;
  172.  
  173.         OnPlayerConnected(connection);
  174.     }
  175.  
  176.     void OnPlayerLeft(Lobby lobby, Friend friend)
  177.     {
  178.         if (!IsServer() || friend.IsMe) return;
  179.  
  180.         Debug.Log("SteamP2P: OnPlayerLeft");
  181.  
  182.         HawkConnection connection = connections.Find(x => ((SteamConnection)x).steamId == friend.Id);
  183.  
  184.         if (connection != null)
  185.         {
  186.             OnPlayerDisconnected(connection);
  187.         }
  188.     }
  189.  
  190.     void OnP2PConnectionFailed(SteamId player, P2PSessionError sessionError)
  191.     {
  192.         Debug.Log("SteamP2P: OnP2PConnectionFailed");
  193.  
  194.         OnErrorOccured(HawkSocketError.Error, "P2P Failed");
  195.     }
  196.  
  197.     void OnP2PSessionRequest(SteamId player)
  198.     {
  199.         Debug.Log("SteamP2P: OnP2PSessionRequest");
  200.  
  201.         SteamNetworking.AcceptP2PSessionWithUser(player);
  202.     }
  203.  
  204.     #endregion
  205.  
  206.     #region Getters/Setters
  207.  
  208.     public override bool IsConnected()
  209.     {
  210.         return base.IsConnected() || bIsConnected;
  211.     }
  212.  
  213.     public bool IsConnecting()
  214.     {
  215.         return bIsConnecting;
  216.     }
  217.  
  218.     public override bool IsServer()
  219.     {
  220.         return base.IsServer() || bIsServer;
  221.     }
  222.  
  223.     public Lobby? GetLobby()
  224.     {
  225.         return lobby;
  226.     }
  227.  
  228.     #endregion
  229. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top