-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkManager_Custom.cs
115 lines (106 loc) · 4 KB
/
NetworkManager_Custom.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.Text.RegularExpressions;
using UnityEngine.SceneManagement;
using System;
public class NetworkManager_Custom : NetworkManager
{
private bool onlyOne = true;
//Script used to override UNet's base NetworkManager
//Beyond regular functionality, contains some additional functions for my game's use of networking
private void Start()
{
GameObject[] managers = GameObject.FindGameObjectsWithTag("MatchManager");
for (int i = 0; i < managers.Length; i++)
{
if (managers[i] != this.gameObject)
Destroy(managers[i]);
}
}
//Function checks if room code in textbox is a valid roomcode
private bool RoomCodeValid()
{
try
{
string TextboxText = this.GetComponent<RoomHandlerCode>().CodeTextbox.text;
TextboxText = TextboxText.ToUpper().Replace(" ", "").Replace("\n", "");
if (!TextboxText.Contains("-"))
return false;
if (!Regex.IsMatch(TextboxText, @"^[A-Z-]+$"))
return false;
string ipAddress = this.GetComponent<RoomHandlerCode>().RoomcodeToIp(TextboxText);//TODO - Could get IP from somewhere else
return true;
}
catch
{
return false;
}
}
void SetIPAddress()
{
string ipAddress = this.GetComponent<RoomHandlerCode>().RoomcodeToIp(this.GetComponent<RoomHandlerCode>().CodeTextbox.text);//TODO - Could get IP from somewhere else
NetworkManager.singleton.networkAddress = ipAddress;
}
void SetPort()
{
NetworkManager.singleton.networkPort = 7777;//TODO - Could get port from roomCode
}
public override void OnClientDisconnect(NetworkConnection conn)
{
//THIS FUNCTION IS CALLED WHEN CLIENT DISCONNECTS
try
{
base.OnClientDisconnect(conn);
}
catch
{
Debug.Log("Exception disconnecting.");
}
}
private void InitializeNetwork()
{
Debug.Log("Began initializeNetwork");
ConnectionConfig myConfig = new ConnectionConfig();
myConfig.AddChannel(QosType.Reliable);
myConfig.AddChannel(QosType.Unreliable);
myConfig.AddChannel(QosType.StateUpdate);
myConfig.AddChannel(QosType.AllCostDelivery);
myConfig.NetworkDropThreshold = 95; //95% packets that need to be dropped before connection is dropped
myConfig.OverflowDropThreshold = 30; //30% packets that need to be dropped before sendupdate timeout is increased
myConfig.InitialBandwidth = 0;
myConfig.MinUpdateTimeout = 10;
myConfig.ConnectTimeout = 2000; // timeout before re-connect attempt will be made
myConfig.PingTimeout = 1500; // should have more than 3 pings per disconnect timeout, and more than 5 messages per ping
myConfig.DisconnectTimeout = 6000; // with a ping of 500 a disconnectimeout of 2000 is supposed to work well
myConfig.PacketSize = 1470;
myConfig.SendDelay = 2;
myConfig.FragmentSize = 1300;
myConfig.AcksType = ConnectionAcksType.Acks128;
myConfig.MaxSentMessageQueueSize = 256;
myConfig.AckDelay = 1;
HostTopology myTopology = new HostTopology(myConfig, 4); //up to 4 connection allowed
NetworkServer.Configure(myTopology);
Debug.Log("Finished initializeNetwork");
}
public void StartupHost()
{
SetPort();
Debug.Log("Hosting match at address " + NetworkManager.singleton.networkAddress + ":" + NetworkManager.singleton.networkPort);
NetworkManager.singleton.StartHost();
NetworkServer.SpawnObjects();
}
public void JoinGame()
{
try
{
NetworkManager.singleton.StartClient();
NetworkServer.SpawnObjects();
}
catch (Exception ex)
{
Debug.Log("Failed to join game");
}
}
}