Exemplo n.º 1
0
        private void SendOneWay(SocketPool pool, int commandID, MemoryStream messageStream)
        {
            ManagedSocket socket = null;
            ResourcePoolItem <MemoryStream> rebufferedStreamItem = CreateOneWayMessage(commandID, messageStream, pool);

            try
            {
                MemoryStream rebufferedStream = rebufferedStreamItem.Item;
                socket = pool.GetSocket();
                // GetBuffer() should be used in preference to ToArray() where possible
                // as it does not allocate a new byte[] like ToArray does().
                byte[] messageBuffer = rebufferedStream.GetBuffer();
                socket.Send(messageBuffer, (int)rebufferedStream.Length, SocketFlags.None);
            }
            catch (SocketException sex)
            {
                if (socket != null)
                {
                    socket.LastError = sex.SocketErrorCode;
                }
                throw;
            }
            finally
            {
                if (socket != null)
                {
                    socket.Release();
                }
                rebufferedStreamItem.Release();
            }
        }
Exemplo n.º 2
0
        private static void SendOneWay(SocketPool pool, int commandID, MemoryStream messageStream)
        {
            ManagedSocket socket = null;
            ResourcePoolItem <MemoryStream> rebufferedStreamItem = CreateOneWayMessage(commandID, messageStream, pool);

            try
            {
                MemoryStream rebufferedStream = rebufferedStreamItem.Item;
                socket = pool.GetSocket();
                // GetBuffer() should be used in preference to ToArray() where possible
                // as it does not allocate a new byte[] like ToArray does().
                byte[] messageBuffer = rebufferedStream.GetBuffer();

                socket.Send(messageBuffer, (int)rebufferedStream.Length, SocketFlags.None);
                if (socket.ServerSupportsAck && pool.Settings.RequestOneWayAck)
                {
                    try
                    {
                        socket.GetReply();                         //make sure we got the ack
                    }
                    catch (SocketException sex)
                    {
                        log.ErrorFormat("Failed to receive ack from {0} with error {1}", pool.Destination, sex.SocketErrorCode);
                        throw;
                    }
                }
                catch (Exception ex)
                {
                    log.ErrorFormat("Failed to receive ack from {0} with error {1}", pool.Destination, ex.Message);
                    throw;
                }
            }
Exemplo n.º 3
0
        private MemoryStream SendSync(SocketPool pool, int commandID, MemoryStream messageStream)
        {
            short messageId = (short)1;             //new async scheme doesn't currently need these.
            ResourcePoolItem <MemoryStream> rebufferedStreamItem = CreateSyncMessage((short)commandID, messageId, messageStream, pool);
            MemoryStream rebufferedStream = rebufferedStreamItem.Item;

            ManagedSocket socket      = null;
            MemoryStream  replyStream = null;

            try
            {
                socket = pool.GetSocket();

                // GetBuffer() should be used in preference to ToArray() where possible
                // as it does not allocate a new byte[] like ToArray does().
                socket.Send(rebufferedStream.GetBuffer(), (int)rebufferedStream.Length, SocketFlags.None);
                replyStream = socket.GetReply();
            }
            catch (ThreadAbortException)
            {
                if (socket != null)
                {
                    socket.LastError = SocketError.TimedOut;
                }
                log.Warn("Thread aborted on SocketClient.");
                throw;
            }
            catch (SocketException ex)
            {
                if (socket != null)
                {
                    socket.LastError = ex.SocketErrorCode;
                }
                throw;
            }
            finally
            {
                rebufferedStreamItem.Release();
                if (socket != null)                 //getting the socket can throw a timedout exception due to limiting, in which case the socket will be null
                {
                    socket.Release();
                }
            }

            return(replyStream);
        }
Exemplo n.º 4
0
		private MemoryStream SendSync(SocketPool pool, int commandID, MemoryStream messageStream)
		{
			short messageId = (short)1; //new async scheme doesn't currently need these.
			ResourcePoolItem<MemoryStream> rebufferedStreamItem = CreateSyncMessage((short)commandID, messageId, messageStream, pool);
			MemoryStream rebufferedStream = rebufferedStreamItem.Item;

			ManagedSocket socket = null;
			MemoryStream replyStream = null;

			try
			{
				socket = pool.GetSocket();

				// GetBuffer() should be used in preference to ToArray() where possible
				// as it does not allocate a new byte[] like ToArray does().
				socket.Send(rebufferedStream.GetBuffer(), (int)rebufferedStream.Length, SocketFlags.None);
				replyStream = socket.GetReply();
			}
			catch (ThreadAbortException)
			{
				if (socket != null)
				{
					socket.LastError = SocketError.TimedOut;
				}
				log.Warn("Thread aborted on SocketClient.");
				throw;
			}
			catch (SocketException ex)
			{
				if (socket != null)
				{
					socket.LastError = ex.SocketErrorCode;
				}
				throw;
			}
			finally
			{
				rebufferedStreamItem.Release();
				if (socket != null) //getting the socket can throw a timedout exception due to limiting, in which case the socket will be null
				{
					socket.Release();
				}
			}

			return replyStream;
		}
Exemplo n.º 5
0
		private void SendOneWay(SocketPool pool, int commandID, MemoryStream messageStream)
		{
			ManagedSocket socket = null;
			ResourcePoolItem<MemoryStream> rebufferedStreamItem = CreateOneWayMessage(commandID, messageStream, pool);

			try
			{
				MemoryStream rebufferedStream = rebufferedStreamItem.Item;
				socket = pool.GetSocket();
				// GetBuffer() should be used in preference to ToArray() where possible
				// as it does not allocate a new byte[] like ToArray does().
				byte[] messageBuffer = rebufferedStream.GetBuffer();
				socket.Send(messageBuffer, (int)rebufferedStream.Length, SocketFlags.None);
			}
			catch (SocketException sex)
			{
				if (socket != null)
				{
					socket.LastError = sex.SocketErrorCode;
				}
				throw;
			}
			finally
			{
				if (socket != null)
				{
					socket.Release();
				}
				rebufferedStreamItem.Release();
			}
		}