コード例 #1
0
        //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        public static void Login(ByteBuffer buffer, RequestViewCallback requestViewCallback)
        {
            RequestTypes requestType = (RequestTypes)buffer.GetByte();

            if ((requestType != RequestTypes.AuthByLogin) && (requestType != RequestTypes.AuthByToken))
            {
                if (requestType == RequestTypes.Error)
                {
                    Client.Log(buffer.GetLongString());
                }
                return;
            }

            connectState = ConnectStates.AuthPassed;

            ushort serverVersion = buffer.GetUShort();

            serverName = buffer.GetShortString();
            userName   = buffer.GetShortString();

            if (requestType == RequestTypes.AuthByLogin)
            {
                userToken = buffer.GetShortString();
                Client.Log("Get token: " + userToken);
            }

            if (Settings.serverVersion != serverVersion)
            {
                Client.Log("Версии клиента и сервера различаются " + serverVersion + " - " + Settings.serverVersion + ". Обновите клиент", LogTypes.Error);
            }

            Client.Log("Auth success. Server version: " + serverVersion);
        }
コード例 #2
0
        //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        public static void ConnectToSocket(IPAddress ipAddr, int port)
        {
            try
            {
                if (socket != null)
                {
                    if (socket.Connected == true)
                    {
                        socket.Close();
                    }
                }

                IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, port);

                socket = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                Console.WriteLine("socket.ReceiveBufferSize " + socket.ReceiveBufferSize);
                socket.ReceiveBufferSize = 3 * 1024 * 1024;
                Console.WriteLine("socket.ReceiveBufferSize 2 " + socket.ReceiveBufferSize);

                SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
                socketEventArg.RemoteEndPoint = ipEndPoint;

                socketEventArg.Completed += new EventHandler <SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
                {
                    if (e.SocketError != SocketError.Success)
                    {
                        Console.WriteLine("Ошибка подключения к серверу 1" + ipEndPoint + "  " + e.SocketError.ToString(), LogTypes.Error);
                    }
                });

                socket.ConnectAsync(socketEventArg);

                Stopwatch waitTimer = new Stopwatch();
                waitTimer.Start();

                while (true)
                {
                    if (waitTimer.ElapsedMilliseconds > 4000)
                    {
                        Console.WriteLine("Ошибка подключения к серверу 2" + ipEndPoint, LogTypes.Error);
                        break;
                    }

                    if (socket.Connected)
                    {
                        Console.WriteLine("Подключение к серверу успешно " + ipEndPoint);
                        connectState = ConnectStates.SocketConnected;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
コード例 #3
0
        //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        public static void ConnectToServer()
        {
            connectState  = ConnectStates.None;
            lastRequestID = 0;
            requestsList.Clear();
            socketBuffer.Reset();
            userName = "";

            try
            {
                IPHostEntry serverMachineInfo = Dns.GetHostEntry(currentServer.dnsName);
                IPAddress   ipAddr;

                for (int s = 0; s < serverMachineInfo.AddressList.Length; s++)
                {
                    ipAddr = serverMachineInfo.AddressList[s];

                    Console.WriteLine("Trying to connect by DNS '" + currentServer.dnsName + "', ipv4: " + ipAddr);

                    ConnectToSocket(ipAddr, currentServer.port);

                    if (socket.Connected == true)
                    {
                        break;
                    }
                }

                if (socket.Connected == false)
                {
                    for (int s = 0; s < serverMachineInfo.AddressList.Length; s++)
                    {
                        ipAddr = serverMachineInfo.AddressList[s].MapToIPv6();

                        Console.WriteLine("Trying to connect by DNS '" + currentServer.dnsName + "', ipv6: " + ipAddr);

                        ConnectToSocket(ipAddr, currentServer.port);

                        if (socket.Connected == true)
                        {
                            break;
                        }
                    }
                }

                if (socket.Connected == false)
                {
                    ipAddr = new IPAddress(currentServer.ipv4Address);

                    Console.WriteLine("Trying to connect ipv4: " + ipAddr);

                    ConnectToSocket(ipAddr, currentServer.port);
                }

                if (socket.Connected == false)
                {
                    return;
                }

                nextSendPacketNum = 0;
                nextGetPacketNum  = 0;

                if (userToken != null)
                {
                    SendAuthByTokenRequest(userToken, Login);
                }
                else if (login != null)
                {
                    SendAuthByLoginRequest(login, password, Login);
                }
                else
                {
                    socket.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }