示例#1
0
 /// <summary>
 /// OnMessageReceived
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="e"></param>
 protected virtual void OnMessageReceived(IConnection connection, MessageReceivedEventArgs e)
 {
 }
        /// <summary>
        /// OnMessageReceived
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="e"></param>
        protected override void OnMessageReceived(IConnection connection, MessageReceivedEventArgs e)
        {
            base.OnMessageReceived(connection, e);

            int readlength;
            Object response = null;
            ReceivePacket packet = null;
            try
            {
                packet = this._protocol.TryToTranslateMessage(connection, e.Buffer,DefaultConfigure.MaxMessageSize, out readlength);
            }
            catch (Exception ex)
            {
                this.OnError(connection, ex);
                connection.BeginDisconnect(ex);
                e.SetReadlength(e.Buffer.Count);
                return;
            }

            if (response != null)
            {
                this.OnResponse(connection, response);
            }
            e.SetReadlength(readlength);
        }
 /// <summary>
 /// fire MessageReceived
 /// </summary>
 /// <param name="e"></param>
 private void OnMessageReceived(MessageReceivedEventArgs e)
 {
     if (this.MessageReceived != null) this.MessageReceived(this, e);
 }
示例#4
0
        /// <summary>
        /// OnMessageReceived
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="e"></param>
        protected override void OnMessageReceived(IConnection connection, MessageReceivedEventArgs e)
        {
            base.OnMessageReceived(connection, e);

            int readlength=0;
            ReceivePacket packet = null;
            Object obj = null;//解析的对象
            try
            {
                packet = this._protocol.TryToTranslateMessage(connection, e.Buffer, this._maxMessageSize, out readlength);
                if (packet == null)
                    return;
                obj = this._decoder.decode(connection, packet.Buffer);
            }
            catch (Exception ex)
            {
                this.OnError(connection, ex);
                connection.BeginDisconnect(ex);
                e.SetReadlength(e.Buffer.Count);
                return;
            }

            if (packet != null)
                ThreadPool.QueueUserWorkItem(_ =>
                {
                    try { this._handler.OnReceived(connection, obj); }
                    catch (Exception ex) { Log.Trace.Error(ex.Message, ex); }
                });
            e.SetReadlength(readlength);
        }