-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFullPacketRetriever.cs
119 lines (97 loc) · 4.1 KB
/
FullPacketRetriever.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
116
117
118
119
using Networking.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using Networking.Exceptions;
using Networking.ConnectionResolver;
namespace Networking
{
//TODO: change to sync
[Obsolete]
public class FullPacketRetriever
{
//Assuming that on one recieve callback we retrieve 1024 bytes of data
//looping 1024 times at maximum, means we can retrieve 1024x1024 bytes of data
//wich is 1 mb.
public readonly byte[] CONNECTION_ERROR = new byte[1] { BufferInfo.CLIENT_DISCONNECTED };
private int MAX_DATA_RECEIVE_GUARD = 2048;
private int MaxMegabyteRetrieval { get { return (1024 * MAX_DATA_RECEIVE_GUARD) / 1000000; } }
private IProtocolHandler _protocolHandler;
private IConnectionResolver _connectionResolver;
private List<byte> _fullData = new List<byte>();
private byte[] _localStreamBuffer = new byte[BufferInfo.MAX_BUFFER_SIZE];
public FullPacketRetriever(IProtocolHandler protocolHandler, IConnectionResolver connectionResolver)
{
_protocolHandler = protocolHandler;
_connectionResolver = connectionResolver;
}
/// <summary>
/// Returns an array with one element 0x00 if the client is disconnected.
/// Use
/// </summary>
public byte[] RetrievePacketWithPostProcessing(IAsyncResult ar, SocketState state)
{
byte[] resultData = RetrievePacket(ar, state);
return _protocolHandler.PostProcess(resultData);
}
/// <summary>
/// Returns an array with one element 0x00 if the client is disconnected.
/// </summary>
public byte[] RetrievePacket(IAsyncResult ar, SocketState state)
{
int asd = MaxMegabyteRetrieval;
//Here stops the asynchronous recieve
int dataLength = state.Client.Client.EndReceive(ar);
byte[] bufferData = new byte[dataLength];
Buffer.BlockCopy(state.Buffer, 0, bufferData, 0, dataLength);
_fullData.AddRange(bufferData);
//Get the packet length
ProtocolHeader header;
if (!ProtocolHeader.TryGetHeader(bufferData, out header))
{
}
//Index for packet retrieval, start from the first retrieved ones.
long packetIndex = bufferData.Length;
//Index for maximum data retrieval
int i = 0;
//Check if the data is full, if not loop while data is being send, but non-asynchronously.
while (packetIndex < header.DataLength)
{
if (i > MAX_DATA_RECEIVE_GUARD)
{
throw new MaximumTransferSizeReachedException(string.Format("More than {0}mb of data transfer or non-valid data reached the endpoint.", MaxMegabyteRetrieval));
}
i++;
state.ResetBuffer();
int recieveLength = state.Client.Client.Receive(state.Buffer, 0, state.Buffer.Length, SocketFlags.None);
packetIndex += recieveLength;
bufferData = new byte[recieveLength];
Buffer.BlockCopy(state.Buffer, 0, bufferData, 0, recieveLength);
_fullData.AddRange(bufferData);
}
byte[] resultData = _fullData.ToArray();
ResetBuffers();
return resultData;
}
public void SetMaximumRetrieveMegabytes(int megabytes)
{
if (megabytes <= 0 )
{
throw new ArgumentException("Maximum data retrieval cannot be less or equal to zero.");
}
else if (megabytes >= 1024)
{
throw new ArgumentException("Maximum data retrieval cannot be more than 1GB.");
}
MAX_DATA_RECEIVE_GUARD = 1024 * megabytes;
}
private void ResetBuffers()
{
_fullData.Clear();
_localStreamBuffer = new byte[BufferInfo.MAX_BUFFER_SIZE];
}
}
}