Exemplo n.º 1
0
        private void ApportionTypeHandlerEvent(ApportionDispatcher apportionDispatcher, ConnectionWorkType type)
        {
            switch (type)
            {
            case ConnectionWorkType.MainServiceConnection:
                this.MainServiceConnect(apportionDispatcher);
                break;

            case ConnectionWorkType.MainApplicationConnection:
                this.MainApplicationConnect(apportionDispatcher);
                break;

            case ConnectionWorkType.ApplicationServiceConnection:
                this.ApplicationServiceConnect(apportionDispatcher);
                break;

            case ConnectionWorkType.ApplicationConnection:
                this.ApplicationConnect(apportionDispatcher);
                break;

            case ConnectionWorkType.None:
                break;

            default:
                break;
            }
        }
Exemplo n.º 2
0
        private void MainServiceConnect(ApportionDispatcher apportionDispatcher)
        {
            var mainServiceChannelDispatcher = apportionDispatcher.CreateMainServiceChannelDispatcher(_dispatchers);

            var data = MessageHelper.CopyMessageHeadTo(MessageHead.MID_SESSION,
                                                       new SessionPacket()
            {
                SessionItems = new SessionItemPacket[]
                {
                    new SessionItemPacket()
                    {
                        Id            = mainServiceChannelDispatcher.DispatcherId,
                        ACKPacketData = mainServiceChannelDispatcher.ACKPacketData
                    }
                }
            });

            //通知所有主控端上线
            _dispatchers
            .Select(c => c.Value)
            .Where(c => c.ConnectionWorkType == ConnectionWorkType.MainApplicationConnection)
            .ForEach(c => c.SendTo(data));

            _dispatchers.Add(mainServiceChannelDispatcher.DispatcherId, mainServiceChannelDispatcher);
        }
        private void ApplicationServiceConnect(ApportionDispatcher apportionDispatcher)
        {
            var appWorkerConnectionDispatcher = apportionDispatcher.CreateApplicationWorkerChannelDispatcher(_dispatchers, ConnectionWorkType.ApplicationServiceConnection);

            appWorkerConnectionDispatcher.ListByteBuffer.AddRange(apportionDispatcher.GetACKPacketData().BuilderHeadPacket());

            if (apportionDispatcher.ListByteBuffer.Count > 0)
            {
                var bufferData = apportionDispatcher.ListByteBuffer.ToArray();
                appWorkerConnectionDispatcher.ListByteBuffer.AddRange(bufferData);
            }

            this._dispatchers.Add(appWorkerConnectionDispatcher.DispatcherId, appWorkerConnectionDispatcher);
            this._appServiceChannels.Add(new Tuple <long, long>(apportionDispatcher.GetAccessId(), appWorkerConnectionDispatcher.DispatcherId));
            this.OnConnectedEventHandler?.Invoke(appWorkerConnectionDispatcher);
            //找到相应主控端连接
            TcpSessionChannelDispatcher dispatcher;

            if (_dispatchers.TryGetValue(apportionDispatcher.GetAccessId(), out dispatcher))
            {
                var data = MessageHelper.CopyMessageHeadTo(MessageHead.MID_APPWORK);
                dispatcher.SendTo(data);
            }
            else
            {
                appWorkerConnectionDispatcher.CloseSession();
                this.LogOutputEventHandler?.Invoke(LogOutLevelType.Debug, $"应用服务工作连接未找到相应的主控端!");
            }
        }
        private void ApportionTypeHandlerEvent(ApportionDispatcher apportionDispatcher, ConnectionWorkType type)
        {
            switch (type)
            {
            case ConnectionWorkType.MainServiceConnection:
                this.MainServiceConnect(apportionDispatcher);
                break;

            case ConnectionWorkType.MainApplicationConnection:
                this.MainApplicationConnect(apportionDispatcher);
                break;

            case ConnectionWorkType.ApplicationServiceConnection:
                this.ApplicationServiceConnect(apportionDispatcher);
                break;

            case ConnectionWorkType.ApplicationConnection:
                this.ApplicationConnect(apportionDispatcher);
                break;

            case ConnectionWorkType.None:
                break;

            default:
                break;
            }
            this.LogOutputEventHandler?.Invoke(LogOutLevelType.Debug, $"工作类型:{type.ToString()} 的连接已分配!");
        }
Exemplo n.º 5
0
        private void ApplicationServiceConnect(ApportionDispatcher apportionDispatcher)
        {
            //找到相应主控端连接
            TcpSessionChannelDispatcher dispatcher;

            if (_dispatchers.TryGetValue(apportionDispatcher.GetAccessId(), out dispatcher))
            {
                var appWorkerConnectionDispatcher = apportionDispatcher.CreateApplicationWorkerChannelDispatcher(_dispatchers, ConnectionWorkType.ApplicationServiceConnection);
                this._appServiceChannels.Add(new Tuple <long, long>(apportionDispatcher.GetAccessId(), appWorkerConnectionDispatcher.DispatcherId));

                var data = MessageHelper.CopyMessageHeadTo(MessageHead.MID_APPWORK);
                dispatcher.SendTo(data);
            }
        }
Exemplo n.º 6
0
        public static TcpSessionMainConnection CreateMainServiceChannelDispatcher(this ApportionDispatcher apportionDispatcher, IDictionary <long, TcpSessionChannelDispatcher> dispatchers)
        {
            var mainServiceChannelDispatcher = new TcpSessionMainConnection(dispatchers);

            mainServiceChannelDispatcher.ACKPacketData = apportionDispatcher.GetACKPacketData();
            mainServiceChannelDispatcher.SetSession(apportionDispatcher.CurrentSession);
            var bufferData = apportionDispatcher.ListByteBuffer.ToArray();

            if (bufferData.Length > 0)//如缓冲区有数据,则处理消息
            {
                mainServiceChannelDispatcher.ListByteBuffer.AddRange(bufferData);
                mainServiceChannelDispatcher.OnMessage();
            }
            return(mainServiceChannelDispatcher);
        }
Exemplo n.º 7
0
        private void ApplicationConnect(ApportionDispatcher apportionDispatcher)
        {
            TcpSessionChannelDispatcher dispatcher;
            var serviceWorkerChannelItem = this._appServiceChannels.FirstOrDefault(c => c.Item1 == apportionDispatcher.GetAccessId());

            if (!serviceWorkerChannelItem.IsNull() && _dispatchers.TryGetValue(serviceWorkerChannelItem.Item2, out dispatcher))
            {
                var serviceChannelDispatcher = dispatcher.ConvertTo <TcpSessionApplicationWorkerConnection>();
                var appChannelDispatcher     = apportionDispatcher.CreateApplicationWorkerChannelDispatcher(_dispatchers, ConnectionWorkType.ApplicationConnection);
                if (!serviceChannelDispatcher.IsJoin)
                {
                    serviceChannelDispatcher.Join(appChannelDispatcher);
                    serviceChannelDispatcher.OnMessage();
                    appChannelDispatcher.OnMessage();
                }
                this._appServiceChannels.Remove(serviceWorkerChannelItem);

                _dispatchers.Add(appChannelDispatcher.DispatcherId, appChannelDispatcher);
            }
        }
Exemplo n.º 8
0
        public bool StartService()
        {
            var serverConfig = new TcpSocketSaeaServerConfiguration();

            serverConfig.ReuseAddress             = false;
            serverConfig.KeepAlive                = true;
            serverConfig.KeepAliveInterval        = 5000;
            serverConfig.KeepAliveSpanTime        = 1000;
            serverConfig.PendingConnectionBacklog = 0;

            var ipe = new IPEndPoint(IPAddress.Parse(ApplicationConfiguartion.LocalAddress), ApplicationConfiguartion.ServicePort);

            _tcpSaeaServer = TcpSocketsFactory.CreateServerAgent(TcpSocketSaeaSessionType.Full, serverConfig, (notify, session) =>
            {
                switch (notify)
                {
                case TcpSocketCompletionNotify.OnConnected:

                    ApportionDispatcher apportionDispatcher        = new ApportionDispatcher();
                    apportionDispatcher.ApportionTypeHandlerEvent += ApportionTypeHandlerEvent;
                    apportionDispatcher.SetSession(session);

                    break;

                case TcpSocketCompletionNotify.OnSend:
                    break;

                case TcpSocketCompletionNotify.OnDataReceiveing:
                    this.OnDataReceiveingHandler(session);
                    break;

                case TcpSocketCompletionNotify.OnClosed:
                    this.OnClosedHandler(session);
                    break;

                default:
                    break;
                }
            });
            return(true);
        }
Exemplo n.º 9
0
        private void MainApplicationConnect(ApportionDispatcher apportionDispatcher)
        {
            var accessId = apportionDispatcher.GetAccessId();
            var mainappChannelDispatcher = apportionDispatcher.CreateMainApplicationChannelDispatcher(_dispatchers);

            if (!_dispatchers.ContainsKey(accessId))//可能重连太快
            {
                //主控端使用自身AccessId作索引
                _dispatchers.Add(accessId, mainappChannelDispatcher);
            }
            else
            {
                var aboutOfCloseDispatcher = _dispatchers[accessId];

                var data = MessageHelper.CopyMessageHeadTo(MessageHead.MID_LOGOUT,
                                                           new LogOutPacket()
                {
                    Message = "已有相同Id的主控端登陆,你已被登出!"
                });
                aboutOfCloseDispatcher.SendTo(data);
                aboutOfCloseDispatcher.CloseSession();
            }
        }
        private void MainApplicationConnect(ApportionDispatcher apportionDispatcher)
        {
            var accessId = apportionDispatcher.GetAccessId();
            var mainappChannelDispatcher = apportionDispatcher.CreateMainApplicationChannelDispatcher(_dispatchers);

            if (_dispatchers.ContainsKey(accessId))//可能重连太快
            {
                var aboutOfCloseDispatcher = _dispatchers[accessId];

                var data = MessageHelper.CopyMessageHeadTo(MessageHead.MID_LOGOUT,
                                                           new LogOutPacket()
                {
                    Message = "已有相同Id的主控端登陆,你已被登出"
                });
                aboutOfCloseDispatcher.SendTo(data);
                aboutOfCloseDispatcher.CloseSession();//调用后底层会触发Closed事件

                this.LogOutputEventHandler?.Invoke(LogOutLevelType.Debug, $"有相同Id:{accessId}的主控端登陆!");
            }
            _dispatchers.Add(accessId, mainappChannelDispatcher);

            this.OnConnectedEventHandler?.Invoke(mainappChannelDispatcher);
        }
        private void ApplicationConnect(ApportionDispatcher apportionDispatcher)
        {
            TcpSessionChannelDispatcher dispatcher = null;
            var serviceWorkerChannelItem           = this._appServiceChannels.FirstOrDefault(c => c.Item1 == apportionDispatcher.GetAccessId());

            if (!serviceWorkerChannelItem.IsNull() && _dispatchers.TryGetValue(serviceWorkerChannelItem.Item2, out dispatcher))
            {
                this._appServiceChannels.Remove(serviceWorkerChannelItem);
                var serviceChannelDispatcher = dispatcher.ConvertTo <TcpSessionApplicationWorkerConnection>();
                var appChannelDispatcher     = apportionDispatcher.CreateApplicationWorkerChannelDispatcher(_dispatchers, ConnectionWorkType.ApplicationConnection);

                if (apportionDispatcher.ListByteBuffer.Count > 0)
                {
                    var bufferData = apportionDispatcher.ListByteBuffer.ToArray();
                    appChannelDispatcher.ListByteBuffer.AddRange(bufferData);
                }

                this._dispatchers.Add(appChannelDispatcher.DispatcherId, appChannelDispatcher);
                this.OnConnectedEventHandler?.Invoke(appChannelDispatcher);

                if (!serviceChannelDispatcher.IsJoin)
                {
                    serviceChannelDispatcher.Join(appChannelDispatcher);
                    serviceChannelDispatcher.OnMessage();
                    appChannelDispatcher.OnMessage();
                }
                else
                {
                    appChannelDispatcher.CloseSession();
                }
            }
            else
            {
                apportionDispatcher.CloseSession();
                this.LogOutputEventHandler?.Invoke(LogOutLevelType.Debug, $"主控端应用工作连接未找到可匹配的应用服务工作连接!");
            }
        }
Exemplo n.º 12
0
        public static TcpSessionApplicationWorkerConnection CreateApplicationWorkerChannelDispatcher(this ApportionDispatcher apportionDispatcher, IDictionary <long, TcpSessionChannelDispatcher> dispatchers, ConnectionWorkType workType)
        {
            var workerConnection = new TcpSessionApplicationWorkerConnection();

            workerConnection.ConnectionWorkType = workType;
            workerConnection.SetSession(apportionDispatcher.CurrentSession);
            return(workerConnection);
        }
Exemplo n.º 13
0
        public static TcpSessionApplicationWorkerConnection CreateApplicationWorkerChannelDispatcher(this ApportionDispatcher apportionDispatcher, IDictionary <long, TcpSessionChannelDispatcher> dispatchers, ConnectionWorkType workType)
        {
            var workerConnection = new TcpSessionApplicationWorkerConnection();

            workerConnection.ConnectionWorkType = workType;
            workerConnection.SetSession(apportionDispatcher.GetCurrentSession());

            var bufferData = apportionDispatcher.ListByteBuffer.ToArray();

            if (bufferData.Length > 0)//如缓冲区有数据,则处理消息
            {
                workerConnection.ListByteBuffer.AddRange(bufferData);
            }
            return(workerConnection);
        }
Exemplo n.º 14
0
        public static TcpSessionMainApplicationConnection CreateMainApplicationChannelDispatcher(this ApportionDispatcher apportionDispatcher, IDictionary <long, TcpSessionChannelDispatcher> dispatchers)
        {
            var accessId = apportionDispatcher.GetAccessId();
            var mainappChannelDispatcher = new TcpSessionMainApplicationConnection(dispatchers);

            mainappChannelDispatcher.DispatcherId = accessId;
            mainappChannelDispatcher.SetSession(apportionDispatcher.GetCurrentSession());

            var bufferData = apportionDispatcher.ListByteBuffer.ToArray();

            if (bufferData.Length > 0)//如缓冲区有数据,则处理消息
            {
                mainappChannelDispatcher.ListByteBuffer.AddRange(bufferData);
                mainappChannelDispatcher.OnMessage();
            }
            return(mainappChannelDispatcher);
        }
        public bool StartService(StartServiceOptions options)
        {
            ApplicationConfiguartion.SetOptions(options);

            var serverConfig = new TcpSocketSaeaServerConfiguration();

            serverConfig.ReuseAddress             = false;
            serverConfig.KeepAlive                = true;
            serverConfig.KeepAliveInterval        = 5000;
            serverConfig.KeepAliveSpanTime        = 1000;
            serverConfig.PendingConnectionBacklog = 0;

            var ipe = new IPEndPoint(IPAddress.Parse(ApplicationConfiguartion.Options.LocalAddress), ApplicationConfiguartion.Options.ServicePort);

            _tcpSaeaServer = TcpSocketsFactory.CreateServerAgent(TcpSocketSaeaSessionType.Full, serverConfig, (notify, session) =>
            {
                if (SynchronizationContext.IsNull())
                {
                    NotifyProc(null);
                }
                else
                {
                    SynchronizationContext.Send(NotifyProc, null);
                }

                void NotifyProc(object @object)
                {
                    switch (notify)
                    {
                    case TcpSessionNotify.OnConnected:

                        ApportionDispatcher apportionDispatcher        = new ApportionDispatcher();
                        apportionDispatcher.ApportionTypeHandlerEvent += ApportionTypeHandlerEvent;
                        apportionDispatcher.LogOutputEventHandler     += ApportionDispatcher_LogOutputEventHandler;
                        apportionDispatcher.SetSession(session);

                        break;

                    case TcpSessionNotify.OnSend:
                        break;

                    case TcpSessionNotify.OnDataReceiveing:
                        this.OnDataReceiveingHandler(session);
                        break;

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

                    default:
                        break;
                    }
                }
            });

            try
            {
                _tcpSaeaServer.Listen(ipe);
                LogOutputEventHandler?.Invoke(LogOutLevelType.Debug, $"SiMay中间会话服务器监听 {ipe.Port} 端口启动成功!");
                return(true);
            }
            catch (Exception ex)
            {
                LogOutputEventHandler?.Invoke(LogOutLevelType.Error, $"SiMay中间会话服务器启动发生错误,端口:{ipe.Port} 错误信息:{ex.Message}!");
                return(false);
            }
        }