//******************************************************************************************************
// 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;
using GSF.Diagnostics;
namespace GSF.Communication
{
///
/// A class for managing the communication between server and client.
///
/// of the object used for server-client communication.
public class TransportProvider
{
#region [ Members ]
// Fields
///
/// ID of the object.
///
public Guid ID;
///
/// Provider for the transportation of data.
///
public T Provider;
///
/// Number of bytes received in .
///
public int BytesReceived;
///
/// for the object.
///
public TransportStatistics Statistics;
///
/// Optional multicast membership addresses used when a multicast source address is specified.
///
public byte[] MulticastMembershipAddresses;
#endregion
#region [ Constructors ]
///
/// Initializes a new instance of the class.
///
public TransportProvider()
{
ID = Guid.NewGuid();
Statistics = new TransportStatistics();
}
#endregion
#region [ Properties ]
///
/// Gets buffer used for sending data.
///
///
/// Use to reset and/or establish send buffer size.
///
public byte[] SendBuffer { get; private set; }
///
/// Gets send buffer requested size.
///
public int SendBufferSize => SendBuffer?.Length ?? 0;
///
/// Gets buffer used for receiving data.
///
///
/// Use to reset and/or establish receive buffer size.
///
public byte[] ReceiveBuffer { get; private set; }
///
/// Gets receive buffer requested size.
///
public int ReceiveBufferSize => ReceiveBuffer?.Length ?? 0;
#endregion
#region [ Methods ]
///
/// Establishes (or reestablishes) a send buffer of a given size.
///
/// Desired minimum size of send buffer.
/// New send buffer.
public byte[] SetSendBuffer(int size)
{
SendBuffer = new byte[size];
return SendBuffer;
}
///
/// Establishes (or reestablishes) a receive buffer of a given size.
///
/// Desired minimum size of receive buffer.
/// New receive buffer.
public byte[] SetReceiveBuffer(int size)
{
ReceiveBuffer = new byte[size];
return ReceiveBuffer;
}
///
/// Resets .
///
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.
Logger.SwallowException(ex);
}
finally
{
Provider = default(T);
}
}
#endregion
}
}