private void Context_TcpAwaitnotifyProc(TcpChannelContext context, TcpChannelContextServiceType type)
        {
            this.AddChannelListViewItem(context);

            LogShowQueueHelper.WriteLog("连接被确认,工作类型:" + type.ToString());

            switch (type)
            {
            case TcpChannelContextServiceType.MainChannel:
                this.MainChannelProcess(context);
                break;

            case TcpChannelContextServiceType.WorkChannel:

                //通知发起工作连接
                if (this._managerchannel_login > 0)
                {
                    SendMessage(_manager_channel, MessageHelper.CommandCopyTo(MsgCommand.Msg_Connect_Work));
                }
                else
                {
                    context.Session.Close(true);
                }

                break;

            case TcpChannelContextServiceType.ManagerChannel:
                this.ManagerChannelProcess(context);
                break;

            case TcpChannelContextServiceType.ManagerWorkChannel:

                this.JoinWorkChannelContext(context);
                break;

            default:
                LogShowQueueHelper.WriteLog("未确认的连接:" + type.ToString());
                break;
            }
        }
예제 #2
0
        public void OnMessage(TcpSocketSaeaSession session)
        {
            byte[] data = new byte[session.ReceiveBytesTransferred];
            Array.Copy(session.CompletedBuffer, 0, data, 0, data.Length);

            var type = (TcpChannelContextServiceType)session.AppTokens[1];

            if (type == TcpChannelContextServiceType.None)
            {
                //当通道类型未确认时,进入该区域处理消息

                _buffer.AddRange(data);
                if (_buffer.Count < 4)
                {
                    return;
                }

                byte[] lenBytes   = _buffer.GetRange(0, 4).ToArray();
                int    packageLen = BitConverter.ToInt32(lenBytes, 0);

                if (packageLen < 0 || packageLen > 1024 * 1024 * 2)
                {
                    session.Close(true);
                    return;
                }

                if (packageLen > _buffer.Count - 4)
                {
                    return;
                }

                //保留ack,与控制端连接时要用到
                this._ack_buffer = _buffer.GetRange(0, packageLen + 4).ToArray();

                byte[] Ack = CompressHelper.Decompress(_buffer.GetRange(4, packageLen).ToArray());

                short headMsg = BitConverter.ToInt16(Ack, 0);

                if (headMsg == AckPacket)
                {
                    //                                                    命令头            消息体             连接类型
                    this._channelType = (TcpChannelContextServiceType)Ack[sizeof(Int16) + sizeof(Int64) + sizeof(Byte) - 1];

                    if (this._channelType == TcpChannelContextServiceType.ManagerChannel)
                    {
                        long accessKey = BitConverter.ToInt64(Ack, 2);

                        if (!AccessKeyExamine.CheckOut(accessKey))//连接密码确认
                        {
                            LogShowQueueHelper.WriteLog("ManagerChannel AccessKey Wrong " + accessKey.ToString(), "err");

                            byte[] body = MessageHelper.CommandCopyTo(MsgCommand.Msg_AccessKeyWrong);

                            byte[] pack = new byte[body.Length + sizeof(Int32)];
                            BitConverter.GetBytes(body.Length).CopyTo(pack, 0);
                            body.CopyTo(pack, 4);

                            session.SendAsync(pack);
                            //session.Close(true);
                            return;
                        }
                    }

                    this._session.AppTokens[1] = this._channelType;

                    if (this._channelType == TcpChannelContextServiceType.ManagerWorkChannel ||
                        this._channelType == TcpChannelContextServiceType.ManagerChannel)
                    {
                        _buffer.RemoveRange(0, packageLen + 4);
                    }

                    this.OnChannelTypeNotify?.Invoke(this, this._channelType);

                    if (this._channelType == TcpChannelContextServiceType.ManagerChannel)
                    {
                        this.ManagerPackageProcess();//如果缓冲区还有数据
                    }
                }
                else
                {
                    session.Close(true);
                }
            }
            else if (type == TcpChannelContextServiceType.WorkChannel && this._isJoin)
            {
                if (this._desession == null)
                {
                    return;
                }

                this._desession.SendAsync(data);
            }
            else if (type == TcpChannelContextServiceType.ManagerWorkChannel && this._channelType != type)//当前连接类型为ManagerWorkChannel且当前通道类型为ManagerWorkChannel时表示通道此时未与工作类型关联,暂时将数据存放在缓存区,待关联时再将数据转发
            {
                if (this._session == null)
                {
                    return;
                }

                this._session.SendAsync(data);
            }
            else if (type == TcpChannelContextServiceType.ManagerChannel)
            {
                this._buffer.AddRange(data);
                this.ManagerPackageProcess();
            }
            else if (type == TcpChannelContextServiceType.MainChannel)
            {
                this.OnMainChannelMessage?.Invoke(this, data);//直接转发
            }
            else
            {
                _buffer.AddRange(data);
            }
        }