Beispiel #1
0
        /// <summary>
        /// Sends data via the socket asynchronously
        /// </summary>
        /// <param name="Message">Data to transmit</param>
        public void Send(ResponseData Message)
        {
            IBuffer sendBuffer = null;

            //TODO: ENHANCEMENT: Log consecutive bad request response types and use that information to disconnect socket after 3
            try
            {
                lock (syncSocket)
                {
                    if (sentMsgs.Count > 1)
                    {
                        sentMsgs.Dequeue();
                    }
                    sentMsgs.Enqueue(Message);

                    //Log error if .Data is null -- this will help raise a flag if the message is being resent after .ClearData was called
                    Diags.Assert(Message.Data != null, "ASSERTION FAILED: Message Data is null", new System.Diagnostics.StackTrace().ToString());

                    sendBuffer = bufferPool.GetBuffer(Message.Data.LongLength);
                    sendBuffer.CopyFrom(Message.Data);
                    socket.BeginSend(sendBuffer.GetSegments(), SocketFlags.None, CompleteSend, sendBuffer);

                    Message.ClearData(); //free some memory
                }
            }
            catch (Exception ex)
            {
                Diags.LogSocketException(ex);
                if (sendBuffer != null)
                {
                    sendBuffer.Dispose();
                    sendBuffer = null;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Begins an asynchronous receive
        /// </summary>
        /// <param name="Buffer">Buffer to store received data</param>
        /// <param name="ReadCallBack">Method to call on receiving data</param>
        /// <param name="StateObject">State object to be passed to ReadCallBack</param>
        /// <returns>AsyncResult for the asynchronous operation</returns>
        public IAsyncResult BeginReceive(int BufferLength, AsyncCallback ReadCallBack, object StateObject)
        {
            try
            {
                if (recvBuffer == null || recvBuffer.IsDisposed)
                {
                    recvBuffer = bufferPool.GetBuffer(BufferLength);
                }
                else if (recvBuffer.Size < BufferLength)
                {
                    recvBuffer.Dispose();
                    recvBuffer = bufferPool.GetBuffer(BufferLength);
                }

                lock (syncSocket)
                {
                    return(socket.BeginReceive(recvBuffer.GetSegments(), SocketFlags.None, ReadCallBack, StateObject));
                }
            }
            catch (Exception ex)
            {
                Diags.LogSocketException(ex);
                return(null);
            }
        }