//****************************************************************************************************** // TransportProvider.cs - Gbtc // // Copyright © 2010, 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 Eclipse Public License -v 1.0 (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/eclipse-1.0.php // // 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. // 10/8/2012 - Danyelle Gilliam // Modified Header // //****************************************************************************************************** #region [ Contributor License Agreements ] //****************************************************************************************************** // // Copyright © 2011, Grid Protection Alliance. All Rights Reserved. // // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (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/eclipse-1.0.php // // 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. // //****************************************************************************************************** #endregion using System; 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; // Internally managed I/O buffers private byte[] m_sendBuffer; private byte[] m_receiveBuffer; private int m_receiveBufferSize; #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 { return m_sendBuffer; } } /// /// Gets send buffer requested size. /// public int SendBufferSize { get { return (object)m_sendBuffer != null ? m_sendBuffer.Length : 0; } } /// /// Gets buffer used for receiving data. /// /// /// Use to reset and/or establish receive buffer size. /// public byte[] ReceiveBuffer { get { return m_receiveBuffer; } } /// /// Gets receive buffer requested size. /// public int ReceiveBufferSize { get { return m_receiveBufferSize; } } #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) { m_sendBuffer = new byte[size]; return m_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) { if (size != m_receiveBufferSize) { if (m_receiveBuffer != null) BufferPool.ReturnBuffer(m_receiveBuffer); // Take a buffer from the pool of the desired size m_receiveBuffer = BufferPool.TakeBuffer(size); // The buffer returned may be bigger than the requested size, but only the specified size will be usable m_receiveBufferSize = size; } return m_receiveBuffer; } /// /// Resets . /// public void Reset() { if (m_receiveBuffer != null) BufferPool.ReturnBuffer(m_receiveBuffer); m_receiveBuffer = null; m_receiveBufferSize = 0; m_sendBuffer = null; BytesReceived = -1; // Reset the statistics. Statistics.Reset(); // Cleanup the provider. try { if ((object)Provider != null) ((IDisposable)Provider).Dispose(); } catch { // Ignore encountered exception during dispose. } finally { Provider = default(T); } } #endregion } }