Beispiel #1
0
 protected override bool Process(Event e)
 {
     switch (e.GetTypeId())
     {
         case BuiltinEventType.HeartbeatEvent:
             // Do nothing
             break;
         default:
             return base.Process(e);
     }
     return true;
 }
 protected override bool Process(Event e)
 {
     switch (e.GetTypeId())
     {
         case (int)SocketLinkEventType.KeepaliveEvent:
             break;
         default:
             return base.Process(e);
     }
     return true;
 }
Beispiel #3
0
        protected override void OnEventSent(Event e)
        {
            hasSent = true;

            if (e.GetTypeId() != BuiltinEventType.HeartbeatEvent)
            {
                Log.Debug("{0} {1} sent event {2}",
                    link.Name, InternalHandle, e);
            }
            else
            {
                Log.Trace("{0} {1} sent event {2}",
                    link.Name, InternalHandle, e);
            }

            base.OnEventSent(e);
        }
        protected override void LogEventSent(Event e)
        {
            hasSent = true;

            if (e.GetTypeId() != SocketLinkEventType.KeepaliveEvent)
            {
                Log.Debug("{0} {1} sent event {2}", link.Name, Handle, e);
            }
            else
            {
                Log.Trace("{0} {1} sent event {2}", link.Name, Handle, e);
            }

            base.LogEventSent(e);
        }
Beispiel #5
0
        protected virtual bool Process(Event e)
        {
            switch (e.GetTypeId())
            {
                case (int)LinkEventType.HandshakeReq:
                    {
                        var req = (HandshakeReq)e;
                        var resp = new HandshakeResp { _Transform = false };
                        byte[] response = null;
                        try
                        {
                            ManualResetEvent waitHandle =
                                LinkWaitHandlePool.Acquire(InternalHandle);
                            waitHandle.WaitOne(new TimeSpan(0, 0, 30));
                            LinkWaitHandlePool.Release(InternalHandle);
                            response = BufferTransform.Handshake(req.Data);
                        }
                        catch (Exception ex)
                        {
                            Log.Error("{0} {1} error handshaking : {2}",
                                link.Name, InternalHandle, ex.ToString());
                        }
                        if (response != null)
                        {
                            resp.Data = response;
                        }
                        Send(resp);
                    }
                    break;
                case (int)LinkEventType.HandshakeResp:
                    {
                        var ack = new HandshakeAck { _Transform = false };
                        var resp = (HandshakeResp)e;
                        try
                        {
                            if (BufferTransform.FinalizeHandshake(resp.Data))
                            {
                                rxTransformReady = true;
                                ack.Result = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error("{0} {1} error finishing handshake : {2}",
                                link.Name, InternalHandle, ex.ToString());
                        }
                        Send(ack);
                    }
                    break;
                case (int)LinkEventType.HandshakeAck:
                    {
                        var ack = (HandshakeAck)e;
                        bool result = ack.Result;

                        if (result)
                        {
                            txTransformReady = true;
                        }

                        link.OnLinkSessionConnectedInternal(result, (result ? this : null));
                    }
                    break;
                case (int)LinkEventType.SessionReq:
                    if (link.SessionRecoveryEnabled && polarity == false)
                    {
                        var server = (ServerLink)link;
                        server.OnSessionReq(this, (SessionReq)e);
                    }
                    break;
                case (int)LinkEventType.SessionResp:
                    if (link.SessionRecoveryEnabled && polarity == true)
                    {
                        var client = (ClientLink)link;
                        client.OnSessionResp(this, (SessionResp)e);
                    }
                    break;
                case (int)LinkEventType.SessionAck:
                    if (link.SessionRecoveryEnabled && polarity == false)
                    {
                        var server = (ServerLink)link;
                        server.OnSessionAck(this, (SessionAck)e);
                    }
                    break;
                case (int)LinkEventType.SessionEnd:
                    if (link.SessionRecoveryEnabled)
                    {
                        closing = true;
                    }
                    break;
                default:
                    return false;
            }
            return true;
        }
Beispiel #6
0
        /// <summary>
        /// Sends out the specified event through this link session.
        /// </summary>
        public void Send(Event e)
        {
            if (disposed && !link.SessionRecoveryEnabled)
            {
                Log.Warn("{0} {1} dropped {2}", link.Name, InternalHandle, e);
                return;
            }

            lock (syncRoot)
            {
                if (link.SessionRecoveryEnabled && !disposed &&
                    !connected && e.GetTypeId() > 0)  // not builtin events
                {
                    Log.Info("{0} {1} pre-establishment buffered {2}",
                        link.Name, InternalHandle, e);
                    preConnectionQueue.Add(e);
                    return;
                }

                eventsToSend.Add(e);

                if (txFlag || disposed)
                {
                    //Log.Debug("{0} {1} buffered {2}", link.Name, InternalHandle, e);
                    return;
                }

                txFlag = true;
            }

            BeginSend();
        }
Beispiel #7
0
        protected virtual bool Process(Event e)
        {
            switch (e.GetTypeId())
            {
                case (int)LinkEventType.HandshakeReq:
                    {
                        var req = (HandshakeReq)e;
                        var resp = new HandshakeResp { _Transform = false };
                        byte[] response = null;
                        try
                        {
                            response = BufferTransform.Handshake(req.Data);
                        }
                        catch (Exception ex)
                        {
                            Log.Error("{0} {1} error handshaking : {2}",
                                link.Name, Handle, ex.ToString());
                        }
                        if (response != null)
                        {
                            resp.Data = response;
                        }
                        Send(resp);
                    }
                    break;
                case (int)LinkEventType.HandshakeResp:
                    {
                        var ack = new HandshakeAck { _Transform = false };
                        var resp = (HandshakeResp)e;
                        try
                        {
                            if (BufferTransform.FinalizeHandshake(resp.Data))
                            {
                                rxTransformReady = true;
                                ack.Result = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error("{0} {1} error finishing handshake : {2}",
                                link.Name, Handle, ex.ToString());
                        }
                        Send(ack);
                    }
                    break;
                case (int)LinkEventType.HandshakeAck:
                    {
                        var ack = (HandshakeAck)e;
                        bool result = ack.Result;

                        if (result)
                        {
                            txTransformReady = true;
                        }

                        Link.NotifySessionConnected(result, (result ? this : null));
                    }
                    break;
                default:
                    return false;
            }
            return true;
        }
Beispiel #8
0
        private void BeginSendTo(Event e)
        {
            int handle = e._Handle;

            EndPoint endPoint;
            using (new ReadLock(rwlock))
            {
                int count = map.Count;

                if (count == 0)
                {
                    Log.Error("{0} no known peers - dropped event {1}", Name, e);
                    goto next;
                }

                if (count == 1 && handle == 0)
                {
                    endPoint = map.Values[0];
                }
                else
                {
                    if (!map.TryGetValue(handle, out endPoint))
                    {
                        Log.Error("{0} unknown handle {1} - dropped event {2}",
                            Name, handle, e);
                        goto next;
                    }
                }
            }

            // Apply the datagram length limit.
            int length = e.GetLength();
            if (length > txBuffer.BlockSize)
            {
                Log.Error("{0} dropped big event {1}", Name, e);
                goto next;
            }

            txBuffer.Reset();
            Serializer serializer = new Serializer(txBuffer);
            serializer.Write(e.GetTypeId());
            e.Serialize(serializer);

            if (BufferTransform != null)
            {
                BufferTransform.Transform(txBuffer, (int)txBuffer.Length);
            }

            try
            {
                SendToInternal(endPoint);

                Diag.IncrementEventsSent();

                Log.Debug("{0} {1} sent event {2}", Name, handle, e);

                return;
            }
            catch (ObjectDisposedException)
            {
                return;
            }
            catch (Exception ex)
            {
                Log.Info("{0} send error {1}", Name, ex);
            }

            next:
            OnSendToInternal(0);
        }