示例#1
0
        protected override void OnStart()
        {
            _messenger = new Messenger(_handler);

            PacketStream stream;
            ConnectorInfo info;

            stream = _agent.Connect("127.0.0.1", Port.Database);
            stream.Write(new ConnectorInfo("Login"));
            _messenger.Register("Database", stream);
            Console.WriteLine("Database connected.");

            //Listen Proxy
            _agent.Bind("127.0.0.1", Port.Login, 1);
            stream = _agent.Listen();
            info = (ConnectorInfo)stream.Read().body;
            _messenger.Register(info.name, stream);
            Console.WriteLine(info.name + " connected.");

            foreach (var key in _messenger.Keys)
            {
                var dispatcher = Task.Run(() =>
                {
                    while (stopped == false)
                        _messenger.Dispatch(key);
                });
                _dispatchers.Add(key, dispatcher);
            }

            _messenger.Start();
        }
示例#2
0
        protected override void OnStart()
        {
            _messenger = new Messenger(_handler);

            Action listenner = () =>
            {
                _agent.Bind("127.0.0.1", Port.Database, 1);
                var stream = _agent.Listen();
                var info = (ConnectorInfo)stream.Read().body;
                _messenger.Register(info.name, stream);
                Console.WriteLine(info.name + " connected.");
            };

            listenner();//Login
            listenner();//Town
            listenner();//Forest
            listenner();//Mine
            listenner();//Boss
            listenner();//Proxy

            foreach (var key in _messenger.Keys)
            {
                var dispatcher = Task.Run(() =>
                {
                    while (stopped == false)
                        _messenger.Dispatch(key);
                });
                _dispatchers.Add(key, dispatcher);
            }

            _messenger.Start();
        }
示例#3
0
        protected override void OnStart()
        {
            _messenger = new Messenger(this);
            _confirmMessenger = new Messenger(this);

            Action<string, Port> connector = (string ip, Port port) =>
            {
                var stream = _agent.Connect(ip, port);
                stream.Write(new ConnectorInfo("Proxy"));
                _messenger.Register(port.ToString(), stream);
                Console.WriteLine(port.ToString() + " connected.");
                _worldByUser.Add(port.ToString(), port.ToString());
                _serverKeys.Add(port.ToString());
                _rpcBufferByWorld.Add(port.ToString(), new List<RPC>());
            };

            connector("127.0.0.1", Port.Database);
            connector("127.0.0.1", Port.Login);
            connector("127.0.0.1", Port.Town);
            connector("127.0.0.1", Port.Forest);
            connector("127.0.0.1", Port.Mine);
            connector("127.0.0.1", Port.Boss);

            foreach (var key in _messenger.Keys)
            {
                var dispatcher = Task.Run(() =>
                {
                    while (stopped == false)
                        _messenger.Dispatch(key);
                });
                _dispatchers.Add(key, dispatcher);
            }

            _accepter = Task.Run(() =>
            {
                _agent.Bind("127.0.0.1", Port.Proxy, 4);
                while (stopped == false)
                {
                    try
                    {
                        if (_agent.HasConnectReq())
                        {
                            string stringID = _currentConfirmId.ToString();
                            int ID = _currentConfirmId;
                            lock (_lock)
                            {
                                _confirmMessenger.Register(stringID, _agent.Listen());
                                _confirmMessenger.Send(stringID, new ConfirmID(ID));
                            }
                            _currentConfirmId++;
                            Console.WriteLine("Client connected.");
                        }
                    }
                    catch (Exception e)
                    {
                        History.Log(e.ToString());
                    }
                }
            });

            _confirm = Task.Run(() =>
            {
                while (stopped == false)
                {
                    try
                    {
                        lock (_lock)
                        {
                            foreach (var confirmID in _confirmMessenger.Keys)
                                _confirmMessenger.Dispatch(confirmID);
                        }
                    }
                    catch (Exception e)
                    {
                        History.Log(e.ToString());
                    }
                }
            });

            _client = Task.Run(() =>
            {
                while (stopped == false)
                {
                    try
                    {
                        lock (_lock)
                        {
                            foreach (var key in _messenger.Keys)
                            {
                                if (_serverKeys.Contains(key) == false)
                                    _messenger.Dispatch(key);
                            }
                        }

                    }
                    catch (Exception e)
                    {
                        History.Log(e.ToString());
                    }
                }
            });

            _messenger.Start();
            _messenger.onDisconnected = key =>
            {
                foreach (var target in _messenger.Keys)
                {
                    if (target != key && _worldByUser[target] == _worldByUser[key])
                        _messenger.Send(target, new PlayerDisconnect(key));
                }
                _rpcBufferByWorld[_worldByUser[key]].RemoveAll(rpc => rpc.sender == key);
                _dispatchers.Remove(key);
                _worldByUser.Remove(key);
            };
            _confirmMessenger.Start();
        }