-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client
137 lines (124 loc) · 3.79 KB
/
Client
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Timers;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
#pragma warning disable CS0618 // Type or member is obsolete
public class Client : MonoBehaviour
{
[SerializeField] private int MAX_PLAYERS = 100;
[SerializeField] private int PORT = 20000;
[SerializeField] private int WEB_PORT = 20001;
[SerializeField] private int BYTE_SIZE = 1024;
[SerializeField] private string SERVER_IP = "127.0.0.1";
private static Client instance;
private NetworkConnection connection;
private NetworkDataReceiver dataReceiver;
private NetworkDataHandler dataHandler;
private NetworkDataSender dataSender;
public static Action OnUpdate;
#region monobehavior
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this);
}
else
{
if (instance != this)
{
Destroy(this);
}
}
}
private void Update() => OnUpdate?.Invoke();
#endregion
public static Client GetInstance() => instance;
public NetworkConnection GetConnection() => this.connection;
public NetworkDataHandler GetDataHandler() => this.dataHandler;
public void ConnectToServer()
{
if (this.connection != null)
{
if (this.connection.isStarted)
{
Lobby.OpenLoginPanel();
return;
}
else
{
this.connection.InitServer();
return;
}
}
this.connection = new NetworkConnection(BYTE_SIZE, MAX_PLAYERS, PORT, WEB_PORT, SERVER_IP);
dataSender = new NetworkDataSender(BYTE_SIZE);
dataReceiver = new NetworkDataReceiver(BYTE_SIZE);
dataHandler = new NetworkDataHandler();
dataReceiver.DataReceived += dataHandler.OnData;
}
public void DisconnectFromServer()
{
if(connection != null)
{
StartCoroutine(OnDisconnectEvent());
}
}
private IEnumerator OnDisconnectEvent()
{
if (!SceneManager.GetSceneByName("Lobby").isLoaded)
{
AsyncOperation asyncLoadLevel = SceneManager.LoadSceneAsync("Lobby");
while (!asyncLoadLevel.isDone)
{
yield return null;
}
}
connection.Shutdown();
Lobby.OpenConnectToServerPanel();
Lobby.ChangeAuthenticationText("Connection to the server has been lost", Color.red);
Lobby.EnableInput();
}
public void SendToServer(QosType channelType, NetMsg message)
{
switch (channelType)
{
case QosType.Unreliable:
break;
case QosType.UnreliableFragmented:
break;
case QosType.UnreliableSequenced:
break;
case QosType.Reliable:
dataSender.SendServerReliable(message);
break;
case QosType.ReliableFragmented:
break;
case QosType.ReliableSequenced:
break;
case QosType.StateUpdate:
break;
case QosType.ReliableStateUpdate:
break;
case QosType.AllCostDelivery:
break;
case QosType.UnreliableFragmentedSequenced:
break;
case QosType.ReliableFragmentedSequenced:
break;
default:
Debug.Log("ChannelType not implemented");
break;
}
}
}