-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathClient.cs
More file actions
165 lines (148 loc) · 4.52 KB
/
Copy pathClient.cs
File metadata and controls
165 lines (148 loc) · 4.52 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using StardewValleyMP.Connections;
using StardewValleyMP.Interface;
using StardewValleyMP.Packets;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Threading;
using SFarmer = StardewValley.Farmer;
namespace StardewValleyMP
{
public class Client
{
public enum NetStage
{
WaitingForID,
WaitingForWorldData,
Waiting,
Playing,
}
internal IConnection conn;
private Thread receiver;
private BlockingCollection<Packet> toReceive = new BlockingCollection<Packet>(new ConcurrentQueue<Packet>());
public byte id = 255;
public NetStage stage = NetStage.WaitingForID;
public Dictionary< byte, SFarmer > others = new Dictionary< byte, SFarmer >();
public bool tempStopUpdating = false;
public Client( IConnection theConn )
{
conn = theConn;
receiver = new Thread(receiveAndQueue);
receiver.Start();
new VersionPacket().writeTo(conn.getStream());
Multiplayer.sendFunc = send;
}
~Client()
{
conn.disconnect();
receiver.Join();
}
private Queue<Packet> packetDelay = new Queue<Packet>();
public void processDelayedPackets()
{
while (packetDelay.Count > 0)
{
try
{
packetDelay.Dequeue().process(this);
}
catch ( Exception e )
{
Log.error("Exception processing delayed packet: " + e);
}
}
}
public void update()
{
if ( !conn.isConnected() )
{
ChatMenu.chat.Add(new ChatEntry(null, "You lost connection to the server."));
Multiplayer.mode = Mode.Singleplayer;
Multiplayer.client = null;
return;
}
if (tempStopUpdating) return;
if (stage != NetStage.Waiting)
{
processDelayedPackets();
}
if ( stage == NetStage.Playing )
{
Multiplayer.doMyPlayerUpdates(id);
}
try
{
while (toReceive.Count > 0)
{
Packet packet;
bool success = toReceive.TryTake(out packet);
if (!success) continue;
if (stage == NetStage.Waiting && packet.id != ID.NextDay && packet.id != ID.Chat /*&& packet.id != ID.WorldData*/)
packetDelay.Enqueue(packet);
else packet.process(this);
}
}
catch ( Exception e )
{
Log.error("Exception receiving: " + e);
}
}
public void forceUpdate()
{
try
{
Packet packet = Packet.readFrom(conn.getStream());
if (packet != null) packet.process(this);
}
catch (Exception e)
{
Log.error("Exception logging packet: " + e);
}
}
public void send(Packet packet)
{
#if false
try
{
using (MemoryStream s = new MemoryStream())
{
packet.writeTo(s);
byte[] bytes = s.GetBuffer();
stream.Write( bytes, 0, bytes.Length );
}
}
catch ( Exception e )
{
Log.Async("Exception sending " + packet + " to server: " + e);
}
#endif
try
{
packet.writeTo(conn.getStream());
}
catch ( Exception e )
{
Log.error("Exception sending: " + e);
}
}
private void receiveAndQueue()
{
while (conn.isConnected())
{
try
{
Packet packet = Packet.readFrom(conn.getStream());
toReceive.Add(packet);
}
catch ( Exception e )
{
Log.error("Exception while receiving: " + e);
Multiplayer.mode = Mode.Singleplayer;
conn.disconnect();
Multiplayer.client = null;
}
}
}
}
}