Exemple #1
0
        private void AddTcpSocketSession(Socket client)
        {
            if (client == null)
            {
                return;
            }

            lock (ChannelManager.SyncLock)
            {
                ISocketAsyncEventArgsProxy socketProxy = this._SocketAsyncPool.Pop();
                if (socketProxy == null)
                {
                    AsyncUtil.AsyncRun(client.SafeClose);
                    Logger.Info(false, "已经到达最大连接数");
                    return;
                }

                ISocketSession socketSession = new TcpSocketSession(client, (IPEndPoint)client.RemoteEndPoint, socketProxy);
                socketSession.Setup(this);
                socketSession.Initialize();

                if (ChannelManager.AddChannel(socketSession.SessionID, socketSession))
                {
                    socketSession.CloseSocket += socketChannel_CloseSocket;
                    if (ServerConfig.ControlMode == ControlMode.Self ||
                        ServerConfig.ControlMode == ControlMode.Parallel ||
                        ServerConfig.ControlMode == ControlMode.Singleton)
                    {
                        socketSession.SocketReceiveData += socketChannel_SocketReceiveData;
                        AsyncUtil.AsyncRun(socketSession.TryReceive);
                    }

                    OnSocketConnected(socketSession.RemoteIP, socketSession.RemotePort);

                    OnChannelChanged(socketSession.RemoteIP, CommunicateType.NET, ChannelState.Open);

                    Logger.Info(false, String.Format("增加远程连接>>{0}:{1} 成功", socketSession.RemoteIP, socketSession.RemotePort));
                }
                else
                {
                    ISocketAsyncEventArgsProxy proxy = socketSession.SocketAsyncProxy;
                    proxy.Reset();
                    if (proxy.SocketReceiveEventArgsEx.InitOffset != proxy.SocketReceiveEventArgsEx.Offset)
                    {
                        proxy.SocketReceiveEventArgsEx.SetBuffer(proxy.SocketReceiveEventArgsEx.InitOffset, ServerConfig.NetReceiveBufferSize);
                    }
                    _SocketAsyncPool.Push(proxy);
                    socketSession.Close();
                    socketSession = null;
                    Logger.Info(true, String.Format("增加远程连接>>{0}:{1} 失败", socketSession.RemoteIP, socketSession.RemotePort));
                }
            }
        }
        // Add a SocketAsyncEventArg instance to the pool.
        // "item" = SocketAsyncEventArgs instance to add to the pool.
        internal void Push(ISocketAsyncEventArgsProxy item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("Items added to a SocketAsyncEventArgsPool cannot be null");
            }

            lock (this.pool)
            {
                this.pool.Enqueue(item);
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="proxy"></param>
        public SocketSession(Socket socket, ISocketAsyncEventArgsProxy proxy)
            : base()
        {
            SessionID = Guid.NewGuid().ToString();
            string[] temp = socket.RemoteEndPoint.ToString().Split(':');
            if (temp.Length >= 2)
            {
                RemoteIP   = temp[0];
                RemotePort = Convert.ToInt32(temp[1]);
            }
            Client = socket;

            SocketAsyncProxy = proxy;
        }
Exemple #4
0
        protected SocketSession(Socket socket, IPEndPoint remoteEndPoint, ISocketAsyncEventArgsProxy proxy)
        {
            SessionID = Guid.NewGuid().ToString();

            string[] temp = remoteEndPoint.ToString().Split(':');
            if (temp.Length >= 2)
            {
                RemoteIP   = temp[0];
                RemotePort = Convert.ToInt32(temp[1]);
            }
            RemoteEndPoint   = remoteEndPoint;
            Client           = socket;
            SocketAsyncProxy = proxy;

            StartTime      = DateTime.Now;
            LastActiveTime = StartTime;
        }
Exemple #5
0
        private void RemoveTcpSocketSession(ISocketSession socketSession)
        {
            if (socketSession == null)
            {
                return;
            }

            lock (ChannelManager.SyncLock)
            {
                if (ChannelManager.ContainChannel(socketSession.SessionID))
                {
                    string ip   = socketSession.RemoteIP;
                    int    port = socketSession.RemotePort;
                    if (ChannelManager.RemoveChannel(socketSession.SessionID))
                    {
                        ISocketAsyncEventArgsProxy proxy = socketSession.SocketAsyncProxy;

                        if (proxy.SocketReceiveEventArgsEx.InitOffset != proxy.SocketReceiveEventArgsEx.Offset)
                        {
                            proxy.SocketReceiveEventArgsEx.SetBuffer(proxy.SocketReceiveEventArgsEx.InitOffset, ServerConfig.NetReceiveBufferSize);
                        }

                        _SocketAsyncPool.Push(proxy);

                        socketSession.Close();

                        OnSocketClosed(socketSession.RemoteIP, socketSession.RemotePort);

                        OnChannelChanged(socketSession.RemoteIP, CommunicateType.NET, ChannelState.Close);

                        socketSession = null;

                        Logger.Info(false, String.Format("远程连接断开>>{0}:{1} 成功", ip, port));
                    }
                    else
                    {
                        Logger.Info(true, String.Format("远程连接断开>>{0}:{1} 失败", ip, port));
                    }
                }
            }
        }
Exemple #6
0
        private void RemoveSocketSession(ISocketSession socketSession)
        {
            if (socketSession == null)
            {
                return;
            }

            lock (ChannelManager.SyncLock)
            {
                if (ChannelManager.ContainChannel(socketSession.SessionID))
                {
                    if (ChannelManager.RemoveChannel(socketSession.SessionID))
                    {
                        ISocketAsyncEventArgsProxy proxy = socketSession.SocketAsyncProxy;

                        if (proxy.ReceiveOffset != proxy.SocketReceiveEventArgs.Offset)
                        {
                            proxy.SocketReceiveEventArgs.SetBuffer(proxy.ReceiveOffset, Config.NetReceiveBufferSize);
                        }

                        _SocketAsyncPool.Push(proxy);

                        socketSession.Close();

                        OnSocketClosed(socketSession.RemoteIP, socketSession.RemotePort);

                        OnChannelChanged(socketSession.RemoteIP, CommunicateType.NET, ChannelState.Close);

                        socketSession = null;
                    }
                    else
                    {
                        Logger.Info(true, "关闭网络连接失败");
                    }
                }
            }
        }
Exemple #7
0
 public UdpSocketSession(Socket socket, IPEndPoint remoteEndPoint, ISocketAsyncEventArgsProxy proxy) : base(socket, remoteEndPoint, proxy)
 {
 }