generated from gemstone/gemtem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransportProvider.cs
172 lines (146 loc) · 5.57 KB
/
TransportProvider.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
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
166
167
168
169
170
171
172
//******************************************************************************************************
// TransportProvider.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 11/12/2008 - Pinal C. Patel
// Generated original version of source code.
// 07/15/2009 - Pinal C. Patel
// Added error checking to Reset().
// 09/14/2009 - Stephen C. Wills
// Added new header and license agreement.
// 12/05/2010 - J. Ritchie Carroll
// Modified to make use of buffer pool instead of constant dynamic buffer allocations.
// 12/13/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
namespace Gemstone.Communication
{
/// <summary>
/// A class for managing the communication between server and client.
/// </summary>
/// <typeparam name="T"><see cref="Type"/> of the object used for server-client communication.</typeparam>
public class TransportProvider<T>
{
#region [ Members ]
// Fields
/// <summary>
/// ID of the <see cref="TransportProvider{T}"/> object.
/// </summary>
public Guid ID;
/// <summary>
/// Provider for the transportation of data.
/// </summary>
public T? Provider;
/// <summary>
/// Number of bytes received in <see cref="ReceiveBuffer"/>.
/// </summary>
public int BytesReceived;
/// <summary>
/// <see cref="TransportStatistics"/> for the <see cref="TransportProvider{T}"/> object.
/// </summary>
public TransportStatistics Statistics;
/// <summary>
/// Optional multicast membership addresses used when a multicast source address is specified.
/// </summary>
public byte[]? MulticastMembershipAddresses;
#endregion
#region [ Constructors ]
/// <summary>
/// Initializes a new instance of the <see cref="TransportProvider{T}"/> class.
/// </summary>
public TransportProvider()
{
ID = Guid.NewGuid();
Statistics = new TransportStatistics();
}
#endregion
#region [ Properties ]
/// <summary>
/// Gets buffer used for sending data.
/// </summary>
/// <remarks>
/// Use <see cref="SetSendBuffer"/> to reset and/or establish send buffer size.
/// </remarks>
public byte[]? SendBuffer { get; private set; }
/// <summary>
/// Gets send buffer requested size.
/// </summary>
public int SendBufferSize => SendBuffer?.Length ?? 0;
/// <summary>
/// Gets buffer used for receiving data.
/// </summary>
/// <remarks>
/// Use <see cref="SetReceiveBuffer"/> to reset and/or establish receive buffer size.
/// </remarks>
public byte[]? ReceiveBuffer { get; private set; }
/// <summary>
/// Gets receive buffer requested size.
/// </summary>
public int ReceiveBufferSize => ReceiveBuffer?.Length ?? 0;
#endregion
#region [ Methods ]
/// <summary>
/// Establishes (or reestablishes) a send buffer of a given size.
/// </summary>
/// <param name="size">Desired minimum size of send buffer.</param>
/// <returns>New send buffer.</returns>
public byte[] SetSendBuffer(int size)
{
SendBuffer = new byte[size];
return SendBuffer;
}
/// <summary>
/// Establishes (or reestablishes) a receive buffer of a given size.
/// </summary>
/// <param name="size">Desired minimum size of receive buffer.</param>
/// <returns>New receive buffer.</returns>
public byte[] SetReceiveBuffer(int size)
{
ReceiveBuffer = new byte[size];
return ReceiveBuffer;
}
/// <summary>
/// Resets <see cref="TransportProvider{T}"/>.
/// </summary>
public void Reset()
{
ReceiveBuffer = null;
SendBuffer = null;
BytesReceived = -1;
// Reset the statistics.
Statistics.Reset();
// Cleanup the provider.
try
{
if (Provider is IDisposable provider)
provider.Dispose();
}
catch (Exception ex)
{
// Ignore encountered exception during dispose.
LibraryEvents.OnSuppressedException(this, ex);
}
finally
{
Provider = default!;
}
}
#endregion
}
}