Пример #1
0
        /// <summary>
        /// session代理提供器构造函数
        /// </summary>
        /// <param name="options">代理配置设置</param>
        /// <param name="onSessionNotifyProc">session事件通知</param>
        /// <param name="onProxyNotify">代理事件通知</param>
        internal TcpProxySessionProvider(SessionProviderOptions options)
        {
            ApplicationConfiguartion.SetOptions(options);

            var clientConfig = new TcpSocketSaeaClientConfiguration();

            clientConfig.ReuseAddress      = true;
            clientConfig.KeepAlive         = true;
            clientConfig.KeepAliveInterval = 5000;
            clientConfig.KeepAliveSpanTime = 1000;

            //不启用压缩
            clientConfig.CompressTransferFromPacket = false;
            //停用应用心跳检测线程
            clientConfig.AppKeepAlive = false;

            _clientAgent = TcpSocketsFactory.CreateClientAgent(TcpSocketSaeaSessionType.Packet, clientConfig, (notify, session) =>
            {
                switch (notify)
                {
                case TcpSessionNotify.OnConnected:
                    this.ConnectedHandler(session);
                    break;

                case TcpSessionNotify.OnSend:
                    this.OnSend(session);
                    break;

                case TcpSessionNotify.OnDataReceived:
                    this.OnMessageHandler(session);
                    break;

                case TcpSessionNotify.OnClosed:
                    this.OnClosed(session);
                    break;

                default:
                    break;
                }
            });
        }
        internal TcpSocketSessionProvider(SessionProviderOptions options)
        {
            ApplicationConfiguartion.SetOptions(options);
            var serverConfig = new TcpSocketSaeaServerConfiguration();

            serverConfig.AppKeepAlive = true;
            serverConfig.CompressTransferFromPacket = false;
            serverConfig.PendingConnectionBacklog   = ApplicationConfiguartion.Options.PendingConnectionBacklog;

            _server = TcpSocketsFactory.CreateServerAgent(TcpSocketSaeaSessionType.Packet, serverConfig, (notify, session) =>
            {
                switch (notify)
                {
                case TcpSessionNotify.OnConnected:

                    SessionProviderContext sessionBased = new TcpSocketSessionContext(session);
                    this.SessionNotify(sessionBased, TcpSessionNotify.OnConnected);
                    break;

                case TcpSessionNotify.OnSend:
                    this.SessionNotify(session.AppTokens.First().ConvertTo <SessionProviderContext>(), TcpSessionNotify.OnSend);
                    break;

                case TcpSessionNotify.OnDataReceiveing:
                    this.SessionNotify(session.AppTokens.First().ConvertTo <SessionProviderContext>(), TcpSessionNotify.OnDataReceiveing);
                    break;

                case TcpSessionNotify.OnDataReceived:
                    this.SessionNotify(session.AppTokens.First().ConvertTo <SessionProviderContext>(), TcpSessionNotify.OnDataReceived);
                    break;

                case TcpSessionNotify.OnClosed:
                    this.SessionNotify(session.AppTokens.First().ConvertTo <SessionProviderContext>(), TcpSessionNotify.OnClosed);
                    break;

                default:
                    break;
                }
            });
        }