-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkSendData.cs
108 lines (98 loc) · 3.13 KB
/
NetworkSendData.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
using BytesBuffer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Server
{
class NetworkSendData
{
public void SendDataTo(int index, byte[] data)
{
ByteBuffer buffer = new ByteBuffer();
buffer.WriteBytes(data);
Globals.Clients[index].myStream.BeginWrite(buffer.ToArray(), 0, buffer.ToArray().Length, null, null);
buffer = null;
}
public async void SendDataToAll(byte[]data)
{
for(int i = 1; i < Constants.MAX_PLAYERS; i++)
{
if(Globals.Clients[i].Socket != null)
{
await Task.Delay(1000);
SendDataTo(i, data);
}
}
}
public void SendDataToAllBut(int index, byte[] data)
{
for (int i = 1; i < Constants.MAX_PLAYERS; i++)
{
if (Globals.Clients[i].Socket != null)
{
if (i != index)
{
SendDataTo(i, data);
}
}
}
}
public void SendJoinGame(int index)
{
ByteBuffer buffer = new ByteBuffer();
buffer.WriteInteger(1);
buffer.WriteInteger(index);
SendDataTo(index, buffer.ToArray());
////Sends the player to the others
SendInstantiatePlayer(index);
////Sends other players to the index player
//SendGetOtherPlayer(index);
}
public async void SendInstantiatePlayer(int index)
{
ByteBuffer buffer = new ByteBuffer();
buffer.WriteInteger(2);
for (int i = 1; i < Constants.MAX_PLAYERS; i++)
{
if (Globals.Clients[i].Socket != null)
{
if (i != index)
{
foreach(var value in Globals.Clients)
{
buffer.WriteInteger(i);
await Task.Delay(1000);
SendDataTo(i, buffer.ToArray());
}
}
}
}
}
public async void SendGetOtherPlayer(int index)
{
ByteBuffer buffer = new ByteBuffer();
buffer.WriteInteger(3);
for (int i = 1; i < Constants.MAX_PLAYERS; i++)
{
if (Globals.Clients[i].Socket != null)
{
if(i != index)
{
buffer.WriteInteger(i);
await Task.Delay(1000);
SendDataTo(index, buffer.ToArray());
}
}
}
}
public void SendAlertMsg(int index, string msg)
{
ByteBuffer buffer = new ByteBuffer();
buffer.WriteInteger((int)ServerPackets.AlertMsg);
buffer.WriteString(msg);
SendDataTo(index, buffer.ToArray());
}
}
}