Beispiel #1
0
        //-------------------------------------------------流程----

        public void Start(int id, int nPort, string type, dynamic[] serverList)
        {
            //dynamic User = Config.ApplyJsonFromPath(config);
            //mId = User.Id;
            mServiceName = type;
            mPort        = nPort;

            try
            {
                Logger.Debug("Start listen at:{0}", mPort);

                var settings = new SocketSettings(100, 100, new IPEndPoint(IPAddress.Any, mPort));
                mFrontEndServer = new SocketListener(settings);

                mFrontEndServer.ClientConnected    += ClientConnected;
                mFrontEndServer.ClientDisconnected += ClientDisconnected;
                mFrontEndServer.OnException        += OnSocketListenerException;

                mFrontEndServer.StartListen();
            }
            catch (Exception ex)
            {
                Logger.Fatal(ex, "Server start failed.");
            }

            //Thread.Sleep(1000);

            foreach (var server in serverList)
            {
                try
                {
                    Logger.Debug("Connect to server {0} {1}:{2}", mServiceName, server.Ip, server.Port);
                    var serverItem = server;
                    var settings   = new SocketClientSettings(new IPEndPoint(IPAddress.Parse(server.Ip), server.Port), 1024 * 64);
                    var b          = new SocketClient(settings);
                    b.Connected       += () => { OnSocketClientConnected(b); };
                    b.Disconnected    += OnSocketClientDisconnected;
                    b.OnException     += OnSocketClientException;
                    b.MessageReceived += ServerOnMessageReceived;

                    mBackEnds.Add(b);
                    b.StartConnect();
                }
                catch (Exception ex)
                {
                    Logger.Fatal(ex,
                                 "Server {0} at {1}:{2} can not reached.", type, server.Ip, server.Port);
                }
            }
        }
Beispiel #2
0
        //public void SendPacket
        public void SendPacket(Packet packet)
        {
            _communicationClientSettings = new SocketClientSettings
                {
                    KeepAlive = false,
                    KeepAliveTimeout = 10000,
                    Reconnect = true,
                    ReconnectionCount = 10,
                    ReconnectionTimeout = 10000,
                    RemoteHost = "127.0.0.1",
                    RemotePort = 11100,
                    DataFormat = SocketDataFormat.PACKET
                };

                _communicationClient = new SocketClient(_communicationClientSettings);
                _communicationClient.OnSocketClientEvent += OnCommunicationClientEvent;
                _communicationClient.Start();
                _communicationClient.Send(packet);
        }
Beispiel #3
0
        private Communication()
        {
            //_communicationClientSettings = SocketClientSettings.Load("communication.config");
                _communicationClientSettings = new SocketClientSettings
                                                   {
                                                       KeepAlive = false,
                                                       KeepAliveTimeout = 10000,
                                                       Reconnect = true,
                                                       ReconnectionCount = 10,
                                                       ReconnectionTimeout = 10000,
                                                       RemoteHost = "127.0.0.1",
                                                       RemotePort = 11100,
                                                       DataFormat = SocketDataFormat.PACKET
                                                   };

                _communicationClient = new SocketClient(_communicationClientSettings);
                _communicationClient.OnSocketClientEvent += OnCommunicationClientEvent;
                _communicationClient.Start();
        }
Beispiel #4
0
    public void TestConnectGate(string addr)
    {
        SocketClient client = null;

        try
        {
            var splittedAddress = addr.Trim().Split(':');
            var ip       = splittedAddress[0].Trim();
            var port     = Convert.ToInt32(splittedAddress[1].Trim());
            var settings = new SocketClientSettings(new IPEndPoint(Dns.GetHostAddresses(ip)[0], port));
            client = new SocketClient(settings);
        }
        catch (Exception exception)
        {
            Logger.Error(exception.ToString());
            return;
        }

        client.OnException += ex =>
        {
            Logger.Error(ex.ToString());
        };

        client.OnConnected += () =>
        {
            if (testGateConnected == false)
            {
                testGateConnected = true;

                ChooseGateAddress = addr;
            }
        };

        try
        {
            client.StartConnect();
        }
        catch (Exception ex)
        {
            Logger.Error(ex.ToString());
        }
    }
Beispiel #5
0
        private void ConnectToBroker(List <dynamic> list)
        {
            foreach (var broker in list)
            {
                if (broker.Type == "GameMaster")
                {
                    continue;
                }

                try
                {
                    var settings = new SocketClientSettings(new IPEndPoint(IPAddress.Parse(broker.Ip), broker.Port), 1024 * 64);
                    var b        = new SocketClient(settings);
                    {
                        b.Connected += () => { OnSocketClientConnectedBroker(b); };

                        b.Disconnected    += OnSocketClientDisconnectedBroker;
                        b.MessageReceived += desc => { OnSocketClienMessageReceivedBroker(b, desc); };

                        string service = broker.Type;
                        {
                            ServiceType tempseServiceType;
                            if (!Enum.TryParse(service, out tempseServiceType))
                            {
                                Logger.Error("ServiceType convet faild!! service={0} ", service);
                            }
                            mFromServiceType2Brokers[(int)tempseServiceType] = b;
                        }
                        b.StartConnect();
                    }
                }
                catch (Exception ex)
                {
                    Logger.FatalException(
                        "Broker " + broker.Type + " at " + broker.Ip + ":" + broker.Port + " can not reached.", ex);
                }
            }
        }
Beispiel #6
0
        public virtual void Start(int id, int nPort, string type, dynamic[] serverList)
        {
            mServiceName = type;
            mPort        = nPort;

            if (mServiceName == "Login")
            {
                mServiceFunc = 2000;
                mServiceType = ServiceType.Login;
            }
            else if (mServiceName == "Activity")
            {
                mServiceFunc = 4000;
                mServiceType = ServiceType.Activity;
            }
            else if (mServiceName == "Logic")
            {
                mServiceFunc = 1000;
                mServiceType = ServiceType.Logic;
            }
            else if (mServiceName == "Rank")
            {
                mServiceFunc = 6000;
                mServiceType = ServiceType.Rank;
            }
            else if (mServiceName == "Scene")
            {
                mServiceFunc = 3000;
                mServiceType = ServiceType.Scene;
            }
            else if (mServiceName == "Chat")
            {
                mServiceFunc = 5000;
                mServiceType = ServiceType.Chat;
            }
            else if (mServiceName == "Team")
            {
                mServiceFunc = 7000;
                mServiceType = ServiceType.Team;
            }
            else if (mServiceName == "GameMaster")
            {
                mServiceFunc = 9000;
                mServiceType = ServiceType.GameMaster;
            }

            Logger = LogManager.GetLogger("Broker." + mServiceName + "Broker");
            try
            {
                Logger.Debug("Start listen at:{0}", mPort);

                var settings = new SocketSettings(100, 100, new IPEndPoint(IPAddress.Any, mPort));
                mFrontEndServer = new SocketListener(settings);

                mFrontEndServer.ClientConnected    += ClientConnected;
                mFrontEndServer.ClientDisconnected += ClientDisconnected;
                mFrontEndServer.OnException        += OnSocketListenerException;

                mFrontEndServer.StartListen();
            }
            catch (Exception ex)
            {
                Logger.Fatal(ex, "Server start failed.");
            }

            //Thread.Sleep(1000);
            var index = 0;

            foreach (var server in serverList)
            {
                try
                {
                    Logger.Debug("Connect to server {0} {1}:{2}", mServiceName, server.Ip, server.Port);

                    //var serverItem = server;
                    var settings = new SocketClientSettings(new IPEndPoint(IPAddress.Parse(server.Ip), server.Port),
                                                            1024 * 1024 * 1024);
                    var b = new SocketClient(settings);
                    {
                        b.UserData         = CreateUserData((int)server.Id);
                        b.Connected       += () => { OnSocketClientConnected(b); };
                        b.Disconnected    += () => { OnSocketClientDisconnected(b); };
                        b.MessageReceived += ServerOnMessageReceived;
                        b.OnException     += OnSocketClientException;

                        mBackEnds.Add(b);
                        BackEndsOnConnect(b, index);
                        b.StartConnect();
                    }
                }
                catch (Exception ex)
                {
                    Logger.FatalException(
                        "Server " + server.ServiceName + " at " + server.Ip + ":" + server.Port + " can not reached.",
                        ex);
                }
                finally
                {
                    index++;
                }
            }

            Console.WriteLine("{1} startOver. [{0}]", id, mServiceName);
        }
Beispiel #7
0
        private void ConnectToServer(dynamic configObject)
        {
            foreach (var co in configObject)
            {
                dynamic[]   serverList;
                ServiceType serviceType;
                var         key = co.Key as string;
                switch (key)
                {
                case "LoginServer":
                    serverList  = configObject.LoginServer;
                    serviceType = ServiceType.Login;
                    break;

                case "SceneServer":
                    serverList  = configObject.SceneServer;
                    serviceType = ServiceType.Scene;
                    break;

                case "ActivityServer":
                    serverList  = configObject.ActivityServer;
                    serviceType = ServiceType.Activity;
                    break;

                case "LogicServer":
                    serverList  = configObject.LogicServer;
                    serviceType = ServiceType.Logic;
                    break;

                case "RankServer":
                    serverList  = configObject.RankServer;
                    serviceType = ServiceType.Rank;
                    break;

                case "ChatServer":
                    serverList  = configObject.ChatServer;
                    serviceType = ServiceType.Chat;
                    break;

                case "TeamServer":
                    serverList  = configObject.TeamServer;
                    serviceType = ServiceType.Team;
                    break;

//                     case "GameMasterServer":
//                         serverList = configObject.GameMasterServer;
//                         break;
                default:
                    continue;
                }


                foreach (var server in serverList)
                {
                    try
                    {
                        Logger.Debug("Connect to server {0} {1}:{2}", co.Key, server.Ip, server.Port);

                        var settings = new SocketClientSettings(new IPEndPoint(IPAddress.Parse(server.Ip), server.Port), 1024 * 64);
                        var b        = new SocketClient(settings);
                        {
                            b.Connected       += () => { OnSocketClientConnectedServer(b); };
                            b.Disconnected    += OnSocketClientDisconnectedServer;
                            b.MessageReceived += desc => { OnSocketClientMessageReceivedServer(b, desc); };
                            b.OnException     += OnSocketClientExceptionServer;

                            mFromId2Servers.AddOrUpdate(serviceType, arg1 => { return(new List <SocketClient> {
                                    b
                                }); },
                                                        (argKey, oldValue) =>
                            {
                                oldValue.Add(b);
                                return(oldValue);
                            });
                            //mFromId2Servers.TryAdd(server.Id, b);

                            b.StartConnect();
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.FatalException(
                            "Server " + server.ServiceName + " at " + server.Ip + ":" + server.Port +
                            " can not reached.",
                            ex);
                    }
                }
            }
        }