コード例 #1
0
 /// <summary>
 /// Initializes a new instance of the SiS.Communication.Process.ShareMemoryBase
 /// </summary>
 /// <param name="uniqueName">An unique name for share memory communication</param>
 /// <param name="bufferSize">The buffer size of the share memory</param>
 public ShareMemoryBase(string uniqueName, int bufferSize)
 {
     _uniqueName    = uniqueName;
     _bufferSize    = bufferSize;
     _packetSpliter = new SimplePacketSpliter();
     _sendBuffer    = new DynamicBufferStream();
 }
コード例 #2
0
        /// <summary>
        /// Send message data to server in synchronous mode.
        /// </summary>
        /// <param name="messageData">The message data to be sent.</param>
        /// <param name="offset">The offset of the message data.</param>
        /// <param name="count">The count of bytes to be sent.</param>
        /// <returns>The number of bytes sent to the server.</returns>
        public int SendMessage(byte[] messageData, int offset, int count)
        {
            if (_clientStatus != ClientStatus.Connected || _clients.Count == 0)
            {
                throw new Exception(Constants.ExMessageClientNotConnected);
            }
            if (count <= 0)
            {
                throw new ArgumentException("The count must be greater than zero.");
            }
            Contract.Assert(messageData != null, "The messageData can not be null");
            Contract.Assert(offset >= 0, "The offset can not be less than zero");
            DynamicBufferStream sendBuffer = _clients.First().Value.SendBuffer;

            lock (sendBuffer)
            {
                ArraySegment <byte> packetForSend = _packetSpliter.MakePacket(messageData, offset, count, sendBuffer);
                int sendTotal = 0;
                int sendIndex = packetForSend.Offset;
                while (sendTotal < packetForSend.Count)
                {
                    int single = _clientSocket.Send(packetForSend.Array, sendIndex, packetForSend.Count - sendTotal, SocketFlags.None);
                    sendTotal += single;
                    sendIndex += single;
                }
            }
            return(messageData.Length);
        }
コード例 #3
0
 /// <summary>
 /// Initializes a new instance of the SiS.Communication.Tcp.ClientContext
 /// </summary>
 public HttpClientContext()
 {
     ReceiveBuffer       = new RingQueue();
     RecvSpeedController = new SpeedController();
     SendController      = new SpeedController();
     SendBuffer          = new DynamicBufferStream();
 }
コード例 #4
0
 /// <summary>
 /// Initializes a new instance of the SiS.Communication.Tcp.ClientContext
 /// </summary>
 public ClientContext()
 {
     ReceiveBuffer       = new RingQueue();
     RecvSpeedController = new SpeedController();
     SendController      = new SpeedController();
     RecvRawMessage      = new TcpRawMessage();
     SendBuffer          = new DynamicBufferStream();
 }
コード例 #5
0
        /// <summary>
        /// Send message data to server in asynchronous mode.
        /// </summary>
        /// <param name="messageData">The message data to be sent.</param>
        /// <param name="offset">The offset of the message data.</param>
        /// <param name="count">The count of bytes to be sent.</param>
        /// <param name="callback">The callback that will be called after sending operation.</param>
        /// <param name="state">The user defined state.</param>
        /// <returns>An System.IAsyncResult that references the asynchronous send.</returns>
        public IAsyncResult SendMessageAsync(byte[] messageData, int offset, int count, AsyncCallback callback, object state)
        {
            if (_clientStatus != ClientStatus.Connected || _clients.Count == 0)
            {
                throw new Exception("the client is not connected");
            }
            DynamicBufferStream sendBuffer = _clients.First().Value.SendBuffer;

            lock (sendBuffer)
            {
                ArraySegment <byte> packetForSend = _packetSpliter.MakePacket(messageData, offset, count, sendBuffer);
                return(_clientSocket.BeginSend(packetForSend.Array, packetForSend.Offset, packetForSend.Count, SocketFlags.None, callback, state));
            }
        }
コード例 #6
0
ファイル: PipeBase.cs プロジェクト: xingx001/SiS.Communcation
 protected PipeBase()
 {
     _packetSpliter = new SimplePacketSpliter();
     _recvQueue     = new RingQueue();
     _sendBuffer    = new DynamicBufferStream();
     if (SynchronizationContext.Current != null)
     {
         _syncContext = SynchronizationContext.Current;
     }
     else
     {
         _syncContext = new SynchronizationContext();
         SynchronizationContext.SetSynchronizationContext(_syncContext);
     }
 }
コード例 #7
0
        /// <summary>
        /// Send message data to server in asynchronous mode.
        /// </summary>
        /// <param name="messageData">The message data to be sent.</param>
        /// <param name="offset">The offset of the message data.</param>
        /// <param name="count">The count of bytes to be sent.</param>
        /// <param name="callback">The callback that will be called after sending operation.</param>
        /// <param name="state">The user defined state.</param>
        /// <returns>An System.IAsyncResult that references the asynchronous send.</returns>
        public IAsyncResult SendMessageAsync(byte[] messageData, int offset, int count, AsyncCallback callback, object state)
        {
            if (_clientStatus != ClientStatus.Connected || _clients.Count == 0)
            {
                throw new Exception(Constants.ExMessageClientNotConnected);
            }
            if (count <= 0)
            {
                throw new ArgumentException("The count must be greater than zero.");
            }
            Contract.Assert(messageData != null, "The messageData can not be null");
            Contract.Assert(offset >= 0, "The offset can not be less than zero");
            DynamicBufferStream sendBuffer = _clients.First().Value.SendBuffer;

            lock (sendBuffer)
            {
                ArraySegment <byte> packetForSend = _packetSpliter.MakePacket(messageData, offset, count, sendBuffer);
                return(_clientSocket.BeginSend(packetForSend.Array, packetForSend.Offset, packetForSend.Count, SocketFlags.None, callback, state));
            }
        }
コード例 #8
0
        /// <summary>
        /// Send message data to server in synchronous mode.
        /// </summary>
        /// <param name="messageData">The message data to be sent.</param>
        /// <param name="offset">The offset of the message data.</param>
        /// <param name="count">The count of bytes to be sent.</param>
        /// <returns>The number of bytes sent to the server.</returns>
        public int SendMessage(byte[] messageData, int offset, int count)
        {
            if (_clientStatus != ClientStatus.Connected || _clients.Count == 0)
            {
                throw new Exception("the client is not connected");
            }
            DynamicBufferStream sendBuffer = _clients.First().Value.SendBuffer;

            lock (sendBuffer)
            {
                ArraySegment <byte> packetForSend = _packetSpliter.MakePacket(messageData, offset, count, sendBuffer);
                int sendTotal = 0;
                int sendIndex = packetForSend.Offset;
                while (sendTotal < packetForSend.Count)
                {
                    int single = _clientSocket.Send(packetForSend.Array, sendIndex, packetForSend.Count - sendTotal, SocketFlags.None);
                    sendTotal += single;
                    sendIndex += single;
                }
            }
            return(messageData.Length);
        }
コード例 #9
0
 /// <summary>
 /// Convert a message to a packet
 /// </summary>
 /// <param name="messageData">The message data to convert.</param>
 /// <param name="offset">The offset of the message data.</param>
 /// <param name="count">The count of bytes to convert.</param>
 /// <param name="sendBuffer">The send buffer which is associated with each connection. It is used to avoid allocating memory every time.</param>
 /// <returns>The packed byte array segment</returns>
 public ArraySegment <byte> MakePacket(byte[] messageData, int offset, int count, DynamicBufferStream sendBuffer)
 {
     byte[] sendData = new byte[count];
     Buffer.BlockCopy(messageData, offset, sendData, 0, count);
     byte[] packetData = MakePacket(sendData);
     return(new ArraySegment <byte>(packetData));
 }
コード例 #10
0
        /// <summary>
        /// Convert a message to a packet using packet length
        /// </summary>
        /// <param name="messageData">The message data to convert.</param>
        /// <param name="offset">The offset of the message data.</param>
        /// <param name="count">The count of bytes to convert.</param>
        /// <param name="sendBuffer">The send buffer which is associated with each connection. It is used to avoid allocating memory every time.</param>
        /// <returns>The packed byte array segment with length if UseMakePacket property is true; otherwise the input message data with doing nothing.</returns>
        public ArraySegment <byte> MakePacket(byte[] messageData, int offset, int count, DynamicBufferStream sendBuffer)
        {
            Contract.Requires(messageData != null && count > 0);
            if (!UseMakePacket)
            {
                return(new ArraySegment <byte>(messageData, offset, count));
            }
            int dataLen   = count;
            int packetLen = dataLen;

            if (UseNetworkByteOrder)
            {
                packetLen = IPAddress.HostToNetworkOrder(dataLen);
            }

            sendBuffer.SetLength(4 + dataLen);
            Buffer.BlockCopy(BitConverter.GetBytes(packetLen), 0, sendBuffer.Buffer, 0, 4);
            Buffer.BlockCopy(messageData, offset, sendBuffer.Buffer, 4, dataLen);
            return(new ArraySegment <byte>(sendBuffer.Buffer, 0, (int)sendBuffer.Length));
        }
コード例 #11
0
 /// <summary>
 /// Make a message into packet with doing nothing.
 /// </summary>
 /// <param name="messageData">The message data to convert.</param>
 /// <param name="offset">The offset of the message data.</param>
 /// <param name="count">The count of bytes to convert.</param>
 /// <param name="sendBuffer">The send buffer which is associated with each connection. It is not used is this spliter.</param>
 /// <returns>The same packed byte array segment as input.</returns>
 public ArraySegment <byte> MakePacket(byte[] messageData, int offset, int count, DynamicBufferStream sendBuffer)
 {
     return(new ArraySegment <byte>(messageData, offset, count));
 }
コード例 #12
0
        /// <summary>
        /// Convert a message to a packet using end mark.
        /// </summary>
        /// <param name="messageData">The message data to convert.</param>
        /// <param name="offset">The offset of the message data.</param>
        /// <param name="count">The count of bytes to convert.</param>
        /// <param name="sendBuffer">The send buffer which is associated with each connection. It is used to avoid allocating memory every time.</param>
        /// <returns>The packed byte array segment with end mark if UseMakePacket property is true; otherwise the input message data with doing nothing.</returns>
        public ArraySegment <byte> MakePacket(byte[] messageData, int offset, int count, DynamicBufferStream sendBuffer)
        {
            Contract.Requires(messageData != null && messageData.Length > 0);
            if (!UseMakePacket)
            {
                return(new ArraySegment <byte>(messageData, offset, count));
            }

            sendBuffer.SetLength(count + _endMark.Length);
            //write data
            Buffer.BlockCopy(messageData, offset, sendBuffer.Buffer, 0, count);
            //write end mark
            Buffer.BlockCopy(_endMark, 0, sendBuffer.Buffer, count, _endMark.Length);
            return(new ArraySegment <byte>(sendBuffer.Buffer, 0, (int)sendBuffer.Length));
        }