示例#1
0
 // Always called under a lock
 protected override void RecordClosedSocket(Socket sock)
 {
     TryRemoveClosedSocket(sock); // don't count this closed socket in IMA, we count it in Gateway.
     gateway.RecordClosedSocket(sock);
 }
示例#2
0
            private bool Send(Message msg, Socket sock)
            {
                if (Cts.IsCancellationRequested)
                {
                    return(false);
                }

                if (sock == null)
                {
                    return(false);
                }

                // Send the message
                List <ArraySegment <byte> > data;
                int headerLength;

                try
                {
                    data = msg.Serialize(out headerLength);
                }
                catch (Exception exc)
                {
                    OnMessageSerializationFailure(msg, exc);
                    return(true);
                }

                int length = data.Sum(x => x.Count);

                int    bytesSent            = 0;
                bool   exceptionSending     = false;
                bool   countMismatchSending = false;
                string sendErrorStr;

                try
                {
                    bytesSent = sock.Send(data);
                    if (bytesSent != length)
                    {
                        // The complete message wasn't sent, even though no error was reported; treat this as an error
                        countMismatchSending = true;
                        sendErrorStr         = String.Format("Byte count mismatch on send: sent {0}, expected {1}", bytesSent, length);
                        Log.Warn(ErrorCode.GatewayByteCountMismatch, sendErrorStr);
                    }
                }
                catch (Exception exc)
                {
                    exceptionSending = true;
                    string remoteEndpoint = "";
                    if (!(exc is ObjectDisposedException))
                    {
                        try
                        {
                            remoteEndpoint = sock.RemoteEndPoint.ToString();
                        }
                        catch (Exception) {}
                    }
                    sendErrorStr = String.Format("Exception sending to client at {0}: {1}", remoteEndpoint, exc);
                    Log.Warn(ErrorCode.GatewayExceptionSendingToClient, sendErrorStr, exc);
                }
                MessagingStatisticsGroup.OnMessageSend(msg.TargetSilo, msg.Direction, bytesSent, headerLength, SocketDirection.GatewayToClient);
                bool sendError = exceptionSending || countMismatchSending;

                if (sendError)
                {
                    gateway.RecordClosedSocket(sock);
                    SocketManager.CloseSocket(sock);
                }
                gatewaySends.Increment();
                msg.ReleaseBodyAndHeaderBuffers();
                return(!sendError);
            }
示例#3
0
            private bool Send(Message msg, Socket sock)
            {
                if (Cts.IsCancellationRequested)
                {
                    return(false);
                }

                if (sock == null)
                {
                    return(false);
                }

                // Send the message
                List <ArraySegment <byte> > data;
                int headerLength;

                try
                {
                    int bodyLength;
                    data = msg.Serialize(this.serializationManager, out headerLength, out bodyLength);
                    if (headerLength + bodyLength > this.serializationManager.LargeObjectSizeThreshold)
                    {
                        Log.Info(ErrorCode.Messaging_LargeMsg_Outgoing, "Preparing to send large message Size={0} HeaderLength={1} BodyLength={2} #ArraySegments={3}. Msg={4}",
                                 headerLength + bodyLength + Message.LENGTH_HEADER_SIZE, headerLength, bodyLength, data.Count, this.ToString());
                        if (Log.IsEnabled(LogLevel.Trace))
                        {
                            Log.Trace("Sending large message {0}", msg.ToLongString());
                        }
                    }
                }
                catch (Exception exc)
                {
                    this.OnMessageSerializationFailure(msg, exc);
                    return(true);
                }

                int length = data.Sum(x => x.Count);

                int    bytesSent            = 0;
                bool   exceptionSending     = false;
                bool   countMismatchSending = false;
                string sendErrorStr;

                try
                {
                    bytesSent = sock.Send(data);
                    if (bytesSent != length)
                    {
                        // The complete message wasn't sent, even though no error was reported; treat this as an error
                        countMismatchSending = true;
                        sendErrorStr         = String.Format("Byte count mismatch on send: sent {0}, expected {1}", bytesSent, length);
                        Log.Warn(ErrorCode.GatewayByteCountMismatch, sendErrorStr);
                    }
                }
                catch (Exception exc)
                {
                    exceptionSending = true;
                    string remoteEndpoint = "";
                    if (!(exc is ObjectDisposedException))
                    {
                        try
                        {
                            remoteEndpoint = sock.RemoteEndPoint.ToString();
                        }
                        catch (Exception) {}
                    }
                    sendErrorStr = String.Format("Exception sending to client at {0}: {1}", remoteEndpoint, exc);
                    Log.Warn(ErrorCode.GatewayExceptionSendingToClient, sendErrorStr, exc);
                }
                MessagingStatisticsGroup.OnMessageSend(msg.TargetSilo, msg.Direction, bytesSent, headerLength, SocketDirection.GatewayToClient);
                bool sendError = exceptionSending || countMismatchSending;

                if (sendError)
                {
                    gateway.RecordClosedSocket(sock);
                    SocketManager.CloseSocket(sock);
                }
                gatewaySends.Increment();
                msg.ReleaseBodyAndHeaderBuffers();
                return(!sendError);
            }