Ejemplo n.º 1
0
        /// <summary>
        /// 发送消息。
        /// </summary>
        /// <param name="message">消息内容。</param>
        /// <returns>一个任务。</returns>
        public async Task SendAsync(TransportMessage message)
        {
            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation("准备发送消息:" + message.Id);
            }

            var data = GetByteBuffer(message);
            await _session.SendAsync(data, 0, data.Length);
        }
Ejemplo n.º 2
0
 public static void SendMessage(TcpSocketSaeaSession session, short messageHead, byte[] payload)
 {
     byte[] pack = new byte[payload.Length + sizeof(short)];
     BitConverter.GetBytes(messageHead).CopyTo(pack, 0);
     Array.Copy(payload, 0, pack, sizeof(short), payload.Length);
     session?.SendAsync(pack);
 }
Ejemplo n.º 3
0
        public override void SendAsync(byte[] data, int offset, int length)
        {
            if (this._disposable == 1)
            {
                return;
            }

            byte[] bytes = CompressHelper.Compress(data, offset, length);
            if ((SessionWorkType)_session.AppTokens[0] == SessionWorkType.ManagerSession)
            {
                byte[] body = new byte[sizeof(Int64) + sizeof(Int32) + bytes.Length];
                BitConverter.GetBytes(this.RemoteId).CopyTo(body, 0);
                BitConverter.GetBytes(bytes.Length).CopyTo(body, 8);
                bytes.CopyTo(body, 12);

                SendMessageHelper.SendMessage(_session, MsgCommand.Msg_MessageData, body);
            }
            else
            {
                byte[] body = new byte[bytes.Length + sizeof(Int32)];
                BitConverter.GetBytes(bytes.Length).CopyTo(body, 0);
                bytes.CopyTo(body, 4);

                _session.SendAsync(body, 0, body.Length);
            }
        }
Ejemplo n.º 4
0
        private void Client_CompletetionNotify(TcpSocketCompletionNotify e, TcpSocketSaeaSession session)
        {
            this.Invoke(new Action(() =>
            {
                switch (e)
                {
                case TcpSocketCompletionNotify.OnConnected:
                    break;

                case TcpSocketCompletionNotify.OnDataReceiveing:
                    //this.listBox1.Items.Add("len:" + session.CompletedBuffer.Length + " msg:" + Encoding.UTF8.GetString(session.CompletedBuffer, 0, session.ReceiveBytesTransferred));
                    break;

                case TcpSocketCompletionNotify.OnDataReceived:
                    if (session.CompletedBuffer.Length != 15)
                    {
                        throw new Exception();
                    }
                    Console.WriteLine(session.CompletedBuffer.Length + ":" + Encoding.UTF8.GetString(session.CompletedBuffer, 0, session.CompletedBuffer.Length));
                    session.SendAsync(session.CompletedBuffer);
                    //this.listBox1.Items.Add(session.CompletedBuffer.Length);
                    break;

                case TcpSocketCompletionNotify.OnClosed:
                    var xx = session.AppTokens;
                    this.listBox1.Items.Add("client disconnect");
                    break;
                }
            }));
        }
Ejemplo n.º 5
0
        public async Task OnSessionDataReceived(TcpSocketSaeaSession session, byte[] data, int offset, int count)
        {
            var text = Encoding.UTF8.GetString(data, offset, count);
            //Console.Write(string.Format("Client : {0} --> ", session.RemoteEndPoint));
            //Console.WriteLine(text);

            await session.SendAsync(Encoding.UTF8.GetBytes(text));
        }
Ejemplo n.º 6
0
        public async Task OnSessionDataReceived(TcpSocketSaeaSession session, byte[] data, int offset, int count)
        {
            var text = Encoding.UTF8.GetString(data, offset, count);
            Console.Write(string.Format("Client : {0} --> ", session.RemoteEndPoint));
            Console.WriteLine(text);

            await session.SendAsync(Encoding.UTF8.GetBytes(text));
        }
Ejemplo n.º 7
0
 private void Send(byte[] data)
 {
     if (_trunkTcpSession == null)
     {
         return;
     }
     _trunkTcpSession.SendAsync(data);
 }
Ejemplo n.º 8
0
 private void SendMessageToServer(byte[] data)
 {
     if (_session == null)
     {
         return;
     }
     _session.SendAsync(data);
 }
Ejemplo n.º 9
0
        public void SendMessage(byte[] data, int offset, int lenght)
        {
            if (this._session == null)
            {
                return;
            }

            _session.SendAsync(data, offset, lenght);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// 发送确认包
 ///
 /// 作用:连接确认,以便服务端识别这是一个有效的工作连接,type = 中间服务器识别
 /// </summary>
 /// <param name="session"></param>
 public void SendAckPack(TcpSocketSaeaSession session, ConnectionWorkType type)
 {
     byte[] data = MessageHelper.CopyMessageHeadTo(MessageHead.C_GLOBAL_CONNECT,
                                                   new AckPack()
     {
         AccessKey = AppConfiguartion.AccessKey,
         Type      = type
     });
     session.SendAsync(data);
 }
Ejemplo n.º 11
0
        public static void SendMessage(TcpSocketSaeaSession session, MsgCommand cmd, byte[] body = null)
        {
            if (body == null)
            {
                body = new byte[] { 0 }
            }
            ;

            byte[] bytes = new byte[sizeof(Int32) + body.Length + 1];
            BitConverter.GetBytes(body.Length + 1).CopyTo(bytes, 0);
            bytes[4] = (byte)cmd;
            body.CopyTo(bytes, 5);
            session.SendAsync(bytes);
        }
    }
Ejemplo n.º 12
0
        private void SendAck(TcpSocketSaeaSession session, SessionWorkType type)
        {
            //                          //命令头      //accesskey     //workType
            byte[] bytes = new byte[sizeof(Int16) + sizeof(Int64) + sizeof(Byte)];
            BitConverter.GetBytes(AckPacket).CopyTo(bytes, 0);
            BitConverter.GetBytes(_options.AccessKey).CopyTo(bytes, 2);
            bytes[10] = (Byte)type;

            byte[] body = CompressHelper.Compress(bytes, 0, bytes.Length);

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

            session.SendAsync(data);
        }
Ejemplo n.º 13
0
        private void SendACK(TcpSocketSaeaSession session, ConnectionWorkType type)
        {
            var ackBody = MessageHelper.CopyMessageHeadTo(SiMay.Core.MessageHead.C_GLOBAL_CONNECT,
                                                          new AckPack()
            {
                Type      = (byte)type,
                AccessId  = ApplicationConfiguartion.Options.AccessId,
                AccessKey = type == ConnectionWorkType.MainApplicationConnection ? ApplicationConfiguartion.Options.MainAppAccessKey : ApplicationConfiguartion.Options.AccessKey
            });

            var dataBuilder = new List <byte>();

            dataBuilder.AddRange(BitConverter.GetBytes(ApplicationConfiguartion.Options.AccessId));
            dataBuilder.AddRange(GZipHelper.Compress(ackBody, 0, ackBody.Length));
            session.SendAsync(dataBuilder.ToArray());//构造发送
            dataBuilder.Clear();
        }
        private void SendAck(TcpSocketSaeaSession session, SessionWorkType type)
        {
            var typeByte = (byte)type;
            var ackBody  = SerializePacket(new AckPack()
            {
                Type      = typeByte.ConvertTo <ConnectionWorkType>(),
                AccessId  = _options.AccessId,
                AccessKey = _options.AccessKey
            });

            ackBody = GZipHelper.Compress(ackBody, 0, ackBody.Length);

            var dataBuilder = new List <byte>();

            dataBuilder.AddRange(BitConverter.GetBytes(ackBody.Length));
            dataBuilder.AddRange(ackBody);
            ackBody = dataBuilder.ToArray();
            dataBuilder.Clear();
            session.SendAsync(ackBody);//构造发送
        }
Ejemplo n.º 15
0
 private void EnumerateSessionsHandler(TcpSocketSaeaSession session)
 {
     lock (_lock)
     {
         var sessions = Win32Interop.EnumerateSessions()
                        .Select(c => new SessionItem()
         {
             UserName          = WTSAPI32.GetUserNameBySessionId(c.SessionID),
             SessionState      = (int)c.State,
             WindowStationName = c.pWinStationName,
             SessionId         = c.SessionID,
             HasUserProcess    = _userProcessSessionIdList.FindIndex(i => i.SessionId == c.SessionID) > -1 ? true : false
         })
                        .ToArray();
         LogHelper.DebugWriteLog("Session-Count:" + sessions.Count());
         var data = MessageHelper.CopyMessageHeadTo(TrunkMessageHead.C_SessionItems,
                                                    new SessionStatusPack()
         {
             Sessions = sessions
         });
         session.SendAsync(data);
     }
 }
Ejemplo n.º 16
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);
            }
        }
Ejemplo n.º 17
0
 protected virtual void SendTo(TcpSocketSaeaSession session, byte[] data)
 {
     session.SendAsync(data);
 }
 public override void SendAsync(byte[] data)
 => _session.SendAsync(data);
Ejemplo n.º 19
0
        private void Server_CompletetionNotify(TcpSocketCompletionNotify e, TcpSocketSaeaSession session)
        {
            this.Invoke(new Action(() =>
            {
                switch (e)
                {
                case TcpSocketCompletionNotify.OnConnected:

                    //session初始化完成连接的事件
                    //调用session.SendAsync();对session发送消息
                    //调用session.Close(true)断开session连接,并通知断开事件
                    //可在session.AppTokens中绑定应用对象,以便在其他异步事件中调用

                    this.listBox1.Items.Add("successful client connection");

                    break;

                case TcpSocketCompletionNotify.OnSend:

                    //session发送数据通知事件

                    //session.SendTransferredBytes == 以发送数据长度

                    break;

                case TcpSocketCompletionNotify.OnDataReceiveing:

                    //session数据接收通知事件

                    //session.ReceiveBytesTransferred == 本次接收数据长度
                    //Packet模式下 session.CompletedBuffer.Length == 完整数据包长度

                    //this.listBox1.Items.Add("len:" + session.CompletedBuffer.Length + " msg:" + Encoding.UTF8.GetString(session.CompletedBuffer, 0, session.ReceiveBytesTransferred));


                    break;

                case TcpSocketCompletionNotify.OnDataReceived:
                    if (session.CompletedBuffer.Length != 15)
                    {
                        throw new Exception();
                    }


                    session.SendAsync(session.CompletedBuffer);


                    //Packet模式session自动处理分包的完成事件

                    //var pack = session.CompletedBuffer;//完整数据包
                    break;

                case TcpSocketCompletionNotify.OnClosed:

                    //session断开通知事件

                    this.listBox1.Items.Add("server :client offline");
                    break;
                }
            }));
        }
Ejemplo n.º 20
0
 protected void SendAsyncToServer(byte[] data)
 => _session.SendAsync(data);