コード例 #1
0
ファイル: ListenerProxy.cs プロジェクト: rongxiong/Scut
        public ListenerProxy(SocketTransponder transponder, ConnectSetting setting)
        {
            _transponder = transponder;
            _setting = setting;
            var localPoint = SocketSettings.GetHostAddress(_setting.Host, _setting.Port);
            var socketSetting = new SocketSettings(localPoint, setting.MaxPoolSize, setting.BufferSize, setting.Backlog);
            socketSetting.ContinuedTimeout = _setting.ConnectTimeout;
            socketSetting.EnableReceiveTimeout = _setting.EnableReceiveTimeout;
            socketSetting.ReceiveTimeout = _setting.ReceiveTimeout;

            _listener = new SocketListener(socketSetting);
            _listener.ConnectCompleted += OnConnectCompleted;
            _listener.ReceiveCompleted += _transponder.Receive;
            _listener.SocketSending += _transponder.OnSending;
            _listener.SocketClosing += OnSocketClosing;
            _listener.ReceiveTimeout += _transponder.OnReceiveTimeout;
            _transponder.SendCompleted += (address, data) =>
            {
                if (!_listener.PushSend(address, data))
                {
                    Console.WriteLine("发送到{0} {1}byte失败", address, data.Length);
                }
            };
            _transponder.CheckConnectedHandle += OnCheckConnected;
        }
コード例 #2
0
ファイル: SocketListener.cs プロジェクト: JerryDeveloper/Scut
        /// <summary>
        /// Initializes a new instance of the <see cref="ZyGames.Framework.RPC.Sockets.SocketListener"/> class.
        /// </summary>
        /// <param name="socketSettings">Socket settings.</param>
        public SocketListener(SocketSettings socketSettings)
        {
            this.socketSettings = socketSettings;
            this.prefixHandler = new PrefixHandler();
            this.messageHandler = new MessageHandler();

            this.bufferManager = new BufferManager(this.socketSettings.BufferSize * this.socketSettings.NumOfSaeaForRecSend, this.socketSettings.BufferSize);

            this.ioEventArgsPool = new ThreadSafeStack<SocketAsyncEventArgs>(socketSettings.NumOfSaeaForRecSend);
            this.acceptEventArgsPool = new ThreadSafeStack<SocketAsyncEventArgs>(socketSettings.MaxAcceptOps);
            this.maxConnectionsEnforcer = new Semaphore(this.socketSettings.MaxConnections, this.socketSettings.MaxConnections);
            Init();
            expireTimer = new Timer(CheckExpire, null, socketSettings.ExpireInterval, socketSettings.ExpireInterval);
        }
コード例 #3
0
ファイル: SocketProxy.cs プロジェクト: houguohua/Scut
        public SocketProxy(GSConnectionManager gsConnectionManager)
        {
            this.gsConnectionManager = gsConnectionManager;

            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, ListenPort);
            var socketSettings = new SocketSettings(maxConnections, backlog, maxAcceptOps, bufferSize, localEndPoint, expireInterval, expireTime);
            listener = new SocketListener(socketSettings);
            listener.DataReceived += new ConnectionEventHandler(socketLintener_DataReceived);
            listener.Connected += new ConnectionEventHandler(socketLintener_Connected);
            listener.Disconnected += new ConnectionEventHandler(socketLintener_Disconnected);
            listener.StartListen();
            TraceLog.ReleaseWrite("TCP listent is started, The port:{0}.", ListenPort);

            timer = new Timer(Check, null, proxyCheckPeriod, proxyCheckPeriod);
        }
コード例 #4
0
ファイル: SocketListener.cs プロジェクト: rongxiong/Scut
        /// <summary>
        /// 
        /// </summary>
        public SocketListener(SocketSettings settings)
        {
            _settings = settings;
            _sessioinListen = new CacheListener("__SOCKET_SESSION_POOL", 10, OnRemoveSession);
            _resetEvent = new AutoResetEvent[1];
            _resetEvent[0] = new AutoResetEvent(false);

            _sessionPool = new SocketSessionPool();
            _readWritePool = new SocketAsyncPool(_settings.NumOfSaeaForRecSend);
            _acceptPool = new SocketAsyncPool(_settings.MaxConnection);
            int numSize = settings.NumOfSaeaForRecSend * settings.BufferSize;
            _bufferManager = new BufferManager(numSize, settings.BufferSize);
            _saeaProxy = new SocketAsyncEventArgsProxy(settings.BufferSize);
            _saeaProxy.ReceiveCompleted += OnReceiveCompleted;
            _saeaProxy.SendCompleted += OnSendCompleted;
            _saeaProxy.ClosedHandle += OnSocketClosing;
            Init();
        }
コード例 #5
0
ファイル: GameSocketHost.cs プロジェクト: JohnnyXq/Scut
        /// <summary>
        /// 
        /// </summary>
        protected GameSocketHost()
        {
            ActionDispatcher = new ActionDispatcher();
            int port = GameEnvironment.Setting.GamePort;
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, port);

            int maxConnections = ConfigUtils.GetSetting("MaxConnections", 10000);
            int backlog = ConfigUtils.GetSetting("Backlog", 1000);
            int maxAcceptOps = ConfigUtils.GetSetting("MaxAcceptOps", 1000);
            int bufferSize = ConfigUtils.GetSetting("BufferSize", 8192);
            int expireInterval = ConfigUtils.GetSetting("ExpireInterval", 600) * 1000;
            int expireTime = ConfigUtils.GetSetting("ExpireTime", 3600) * 1000;

            threadPool = new SmartThreadPool(180 * 1000, 100, 5);
            threadPool.Start();

            var socketSettings = new SocketSettings(maxConnections, backlog, maxAcceptOps, bufferSize, localEndPoint, expireInterval, expireTime);
            socketLintener = new SocketListener(socketSettings);
            socketLintener.DataReceived += new ConnectionEventHandler(socketLintener_DataReceived);
            socketLintener.Connected += new ConnectionEventHandler(socketLintener_OnConnectCompleted);
            socketLintener.Disconnected += new ConnectionEventHandler(socketLintener_Disconnected);

            httpListener = new HttpListener();
            var httpHost = ConfigUtils.GetSetting("Game.Http.Host");
            var httpPort = ConfigUtils.GetSetting("Game.Http.Port", 80);
            var httpName = ConfigUtils.GetSetting("Game.Http.Name", "Service.aspx");

            if (!string.IsNullOrEmpty(httpHost))
            {
                EnableHttp = true;
                var hosts = httpHost.Split(',');
                foreach (var host in hosts)
                {
                    string address = host.StartsWith("http", StringComparison.InvariantCultureIgnoreCase)
                                         ? host
                                         : "http://" + host;
                    httpListener.Prefixes.Add(string.Format("{0}:{1}/{2}/", address, httpPort, httpName));
                }
            }
            Interlocked.Exchange(ref _runningQueue, 1);
            queueProcessThread = new Thread(ProcessQueue);
            queueProcessThread.Start();
            _LockedQueueChecker = new Timer(LockedQueueChecker, null, 100, 100);
        }
コード例 #6
0
ファイル: GameSocketHost.cs プロジェクト: yunjoker/Scut
        /// <summary>
        /// 
        /// </summary>
        protected GameSocketHost()
        {
            _setting = GameEnvironment.Setting;
            int port = _setting != null ? _setting.GamePort : 0;
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, port);

            var section = GetSection();
            int maxConnections = section.SocketMaxConnection;
            int backlog = section.SocketBacklog;
            int maxAcceptOps = section.SocketMaxAcceptOps;
            int bufferSize = section.SocketBufferSize;
            int expireInterval = section.SocketExpireInterval;
            int expireTime = section.SocketExpireTime;

            //threadPool = new SmartThreadPool(180 * 1000, 100, 5);
            //threadPool.Start();

            var socketSettings = new SocketSettings(maxConnections, backlog, maxAcceptOps, bufferSize, localEndPoint, expireInterval, expireTime);
            socketListener = new SocketListener(socketSettings);
            socketListener.DataReceived += new ConnectionEventHandler(socketLintener_DataReceived);
            socketListener.Connected += new ConnectionEventHandler(socketLintener_OnConnectCompleted);
            socketListener.Disconnected += new ConnectionEventHandler(socketLintener_Disconnected);


            httpListener = new HttpListener();
            var httpHost = section.HttpHost;
            var httpPort = section.HttpPort;
            var httpName = section.HttpName;

            if (!string.IsNullOrEmpty(httpHost))
            {
                EnableHttp = true;
                var hosts = httpHost.Split(',');
                foreach (var point in hosts)
                {
                    var addressList = point.Split(':');
                    string host = addressList[0];
                    int hport = httpPort;
                    if (addressList.Length > 1)
                    {
                        int.TryParse(addressList[1], out hport);
                    }

                    string address = host.StartsWith("http", StringComparison.InvariantCultureIgnoreCase)
                                         ? host
                                         : "http://" + host;
                    httpListener.Prefixes.Add(string.Format("{0}:{1}/{2}/", address, hport, httpName));
                }
            }
        }
コード例 #7
0
ファイル: GameWebSocketHost.cs プロジェクト: huangbenyu/Scut
        /// <summary>
        /// 
        /// </summary>
        /// <param name="requestHandler"></param>
        protected GameWebSocketHost(WebSocketRequestHandler requestHandler)
        {
            _setting = GameEnvironment.Setting;
            int port = _setting != null ? _setting.GamePort : 0;
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, port);

            var section = GetSection();
            int maxConnections = section.SocketMaxConnection;
            int backlog = section.SocketBacklog;
            int maxAcceptOps = section.SocketMaxAcceptOps;
            int bufferSize = section.SocketBufferSize;
            int expireInterval = section.SocketExpireInterval;
            int expireTime = section.SocketExpireTime;

            var socketSettings = new SocketSettings(maxConnections, backlog, maxAcceptOps, bufferSize, localEndPoint, expireInterval, expireTime);
            socketListener = new SocketListener(socketSettings, requestHandler);
            socketListener.DataReceived += new ConnectionEventHandler(OnDataReceived);
            socketListener.Connected += new ConnectionEventHandler(socketLintener_OnConnectCompleted);
            socketListener.Handshaked += new ConnectionEventHandler(OnHandshaked);
            socketListener.Disconnected += new ConnectionEventHandler(OnDisconnected);
            socketListener.OnPing += new ConnectionEventHandler(socketLintener_OnPing);
            socketListener.OnPong += new ConnectionEventHandler(socketLintener_OnPong);
            socketListener.OnClosedStatus += socketLintener_OnClosedStatus;
        }
コード例 #8
0
ファイル: SocketListener.cs プロジェクト: rongxiong/Scut
 /// <summary>
 /// 释放
 /// </summary>
 /// <param name="disposing"></param>
 protected virtual void DoDispose(bool disposing)
 {
     if (disposing)
     {
         _sessioinListen.Stop();
         _acceptPool.Dispose();
         _acceptPool = null;
         _bufferManager = null;
         _readWritePool.Dispose();
         _readWritePool = null;
         _sessionPool = null;
         _saeaProxy = null;
         _settings = null;
         if (_listenSocket != null)
         {
             try
             {
                 _listenSocket.Close();
             }
             catch { }
         }
         //清理托管对象
         GC.SuppressFinalize(this);
     }
     //清理非托管对象
 }
コード例 #9
0
ファイル: SocketListener.cs プロジェクト: lvshiling/Scut
        /// <summary>
        /// Initializes a new instance
        /// </summary>
        /// <param name="socketSettings">Socket settings.</param>
        /// <param name="requestHandler"></param>
        public SocketListener(SocketSettings socketSettings, RequestHandler requestHandler)
        {
            this.socketSettings = socketSettings;
            this.requestHandler = requestHandler;
            this.bufferManager = new BufferManager(this.socketSettings.BufferSize * this.socketSettings.NumOfSaeaForRecSend, this.socketSettings.BufferSize);

            this.ioEventArgsPool = new ThreadSafeStack<SocketAsyncEventArgs>(socketSettings.NumOfSaeaForRecSend);
            this.acceptEventArgsPool = new ThreadSafeStack<SocketAsyncEventArgs>(socketSettings.MaxAcceptOps);
            this.maxConnectionsEnforcer = new Semaphore(this.socketSettings.MaxConnections, this.socketSettings.MaxConnections);
            Init();
        }
コード例 #10
0
ファイル: SocketListener.cs プロジェクト: lvshiling/Scut
        /// <summary>
        /// 
        /// </summary>
        /// <param name="socketSettings"></param>
        public SocketListener(SocketSettings socketSettings)
            : this(socketSettings, new RequestHandler(new MessageHandler()))
        {

        }
コード例 #11
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="socketSettings"></param>
 public SocketListener(SocketSettings socketSettings)
     : this(socketSettings, new RequestHandler(new MessageHandler()))
 {
 }
コード例 #12
0
ファイル: SocketTest.cs プロジェクト: LeeWangyeol/Scut
        public void Init()
        {
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, port);

            int maxConnections = 10000;
            int backlog = 1000;
            int maxAcceptOps = 10000;
            int bufferSize = 1024;

            var socketSettings = new SocketSettings(maxConnections, backlog, maxAcceptOps, bufferSize, localEndPoint, 0, 0);
            socketListener = new SocketListener(socketSettings);
            socketListener.Connected += OnConnectCompleted;
            socketListener.DataReceived += OnReceivedCompleted;
            socketListener.StartListen();
        }