Exemplo n.º 1
0
        public void Update()
        {
            if (client == null)
            {
                return;
            }

            if (connectionView == null)
            {
                Process process = Process.ThisProcess();
                connectionView = zCView.Create(process, 0, 0, 0x2000, 0x2000);
                zString    str = zString.Create(process, "Connection aborted!");
                zCViewText vt  = connectionView.CreateText(0x950, 0x800, str);
                str.Dispose();
                vt.Color.G = 0;
                vt.Color.B = 0;
                vt.Color.R = 255;
                vt.Color.A = 255;

                vt.Timed = 0;
                vt.Timer = -1;
            }

            if (!shown && (client.GetLastPing(client.GetSystemAddressFromIndex(0)) > 1000 || client.GetLastPing(client.GetSystemAddressFromIndex(0)) <= -1))
            {
                zCView.GetStartscreen(Process.ThisProcess()).InsertItem(connectionView, 0);
                shown = true;
            }
            else if (shown && (client.GetLastPing(client.GetSystemAddressFromIndex(0)) < 700 && client.GetLastPing(client.GetSystemAddressFromIndex(0)) > -1))
            {
                zCView.GetStartscreen(Process.ThisProcess()).RemoveItem(connectionView);
                shown = false;
            }

            int    counter = 0;
            Packet packet  = client.Receive();


            while (packet != null)
            {
                WinApi.Kernel.Process.SetWindowText(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle, "Gothic II - Untold Chapters - " + client.GetLastPing(client.GetSystemAddressFromIndex(0)));

                switch (packet.data[0])
                {
                case (byte)DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED:
                    isConnected    = true;
                    connectionTrys = 0;
                    break;

                case (byte)DefaultMessageIDTypes.ID_CONNECTION_ATTEMPT_FAILED:
                    logError("Connection Failed!");
                    isConnected = false;
                    break;

                case (byte)DefaultMessageIDTypes.ID_ALREADY_CONNECTED:
                    logError("Already Connected!");
                    break;

                case (byte)DefaultMessageIDTypes.ID_CONNECTION_BANNED:
                    logError("Client banned!");
                    break;

                case (byte)DefaultMessageIDTypes.ID_INVALID_PASSWORD:
                    logError("Wrong password");
                    break;

                case (byte)DefaultMessageIDTypes.ID_INCOMPATIBLE_PROTOCOL_VERSION:
                    logError("ID_INCOMPATIBLE_PROTOCOL_VERSION");
                    break;

                case (byte)DefaultMessageIDTypes.ID_NO_FREE_INCOMING_CONNECTIONS:
                    logError("ID_NO_FREE_INCOMING_CONNECTIONS");
                    break;

                case (byte)DefaultMessageIDTypes.ID_DISCONNECTION_NOTIFICATION:
                case (byte)DefaultMessageIDTypes.ID_CONNECTION_LOST:
                    isConnected = false;
                    break;

                case (byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM:
                    try
                    {
                        receiveBitStream.Reset();
                        receiveBitStream.Write(packet.data, packet.length);
                        receiveBitStream.IgnoreBytes(2);
                        if ((Player.Hero == null || Player.Hero.ID == 0) && packet.data[1] != (byte)NetworkID.ConnectionMessage)
                        {
                            break;
                        }
                        if (messageListener.ContainsKey(packet.data[1]))
                        {
                            messageListener[packet.data[1]].Read(receiveBitStream, packet, this);
                        }
                    }
                    catch (Exception ex) { zERROR.GetZErr(Process.ThisProcess()).Report(2, 'G', ex.Message + ex.StackTrace, 0, "Client.cs", 0); }
                    break;
                }

                client.DeallocatePacket(packet);


                counter++;
                if (counter >= 1000)
                {
                    counter = 0;
                    zERROR.GetZErr(Process.ThisProcess()).Report(2, 'G', "1000 Packete hintereinander", 0, "Client.cs", 0);
                }

                packet = client.Receive();
            }
        }
Exemplo n.º 2
0
        internal static void Update()
        {
            int            counter = 0;
            ServerMessages msgType;
            Packet         packet;

            // Receive packets
            while ((packet = clientInterface.Receive()) != null)
            {
                try
                {
                    receivedBytes += packet.length;

                    packetReader.Load(packet.data, (int)packet.length);
                    msgType = (ServerMessages)packetReader.ReadByte();
                    ReadMessage(msgType, packetReader);

                    counter++;
                    if (counter >= 1000)
                    {
                        counter = 0;
                        Logger.Log("1000 Pakete hintereinander");
                    }
                }
                catch (Exception e)
                {
                    if (packet.length >= 1)
                    {
                        Logger.LogError("{0}: {1}: {2}\n{3}", (ServerMessages)packet.data[0], e.Source, e.Message, e.StackTrace);
                    }
                    else
                    {
                        Logger.LogError("{0}: {1}\n{2}", e.Source, e.Message, e.StackTrace);
                    }
                }
                finally
                {
                    clientInterface.DeallocatePacket(packet);
                }
            }

            #region Debug Info

            // update only every second
            if (infoTimer.IsReady)
            {
                int ping = clientInterface.GetLastPing(clientInterface.GetSystemAddressFromIndex(0));

                if (isDisconnected)
                {
                    abortInfo.Texts[0].Text = "Verbindung geschlossen!";
                    abortInfo.Show();
                }
                else if (isConnected)
                {
                    if (ping > 300 || ping < 0)
                    {
                        abortInfo.Show();
                    }
                    else
                    {
                        abortInfo.Hide();
                    }
                }

                // update ping text on screen
                int           devIndex = 0;
                GUCVisualText pingText = devInfo.Texts[devIndex++];
                pingText.Text = string.Format("Ping: {0}ms", ping);
                ColorRGBA color;
                if (ping <= 120)
                {
                    color = new ColorRGBA((byte)(40 + 180 * ping / 120), 220, 40);
                }
                else if (ping <= 220)
                {
                    color = new ColorRGBA(220, (byte)(220 - 180 * (ping - 100) / 120), 40);
                }
                else
                {
                    color = new ColorRGBA(220, 40, 40);
                }
                pingText.SetColor(color);

                long          fps     = Hooks.hGame.LastElapsedTicks > 0 ? TimeSpan.TicksPerSecond / Hooks.hGame.LastElapsedTicks : 999;
                GUCVisualText fpsText = devInfo.Texts[devIndex++];
                fpsText.Text = "FPS: " + fps;
                if (fps < 10)
                {
                    color = new ColorRGBA(220, 40, 40);
                }
                else if (fps < 40)
                {
                    color = new ColorRGBA(220, (byte)(40 + 180 * (fps - 10) / 30), 40);
                }
                else if (fps < 90)
                {
                    color = new ColorRGBA((byte)(220 - 180 * (fps - 40) / 50), 220, 40);
                }
                else
                {
                    color = new ColorRGBA(40, 220, 40);
                }
                fpsText.SetColor(color);

                devInfo.Texts[devIndex++].Text = "Spike: " + Hooks.hGame.SpikeLongest / TimeSpan.TicksPerMillisecond + "ms";

                // update kB/s text on screen
                int kbs = (int)(receivedBytes);
                devInfo.Texts[devIndex++].Text = ("Net received: " + kbs + "B/s");
                kbs = (int)(sentBytes);
                devInfo.Texts[devIndex++].Text = ("Net Sent: " + kbs + "B/s");
                receivedBytes = 0;
                sentBytes     = 0;

                if (World.Current != null)
                {
                    devIndex = 8;
                    devInfo.Texts[devIndex++].Text = World.Current.VobCount + " Vobs";
                    devInfo.Texts[devIndex++].Text = Client.guidedIDs.Count + " guided";

                    devInfo.Texts[devIndex++].Text = "Weather: " + World.Current.WeatherCtrl.CurrentWeight + " " + World.Current.Clock.Time.ToString(false);
                    devInfo.Texts[devIndex++].Text = "Barrier: " + World.Current.BarrierCtrl.CurrentWeight + " " + World.Current.BarrierCtrl.EndWeight;

                    devInfo.Texts[devIndex++].Text = "VobSounds: " + SoundHandler.VobSoundCount;
                    devInfo.Texts[devIndex++].Text = "PosSounds: " + SoundHandler.PosSoundCount;

                    //if (NPC.Hero != null)
                    //    devInfo.Texts[devIndex++].Text =  NPC.Hero.Movement.ToString();
                }
            }

            #endregion
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            RakNetStatistics rss    = new RakNetStatistics();
            RakPeerInterface client = RakPeerInterface.GetInstance();
            Packet           p      = new Packet();
            byte             packetIdentifier;
            bool             isServer = false;

            SystemAddress ClientID = RakNet.RakNet.UNASSIGNED_SYSTEM_ADDRESS;

            string ip, serverPort, clientPort;

            Console.WriteLine("This is a sample implementation of a text based chat client");
            Console.WriteLine("Connect to the project 'Chat Example Server'");

            Console.WriteLine("Enter the client port to listen on");
            clientPort = Console.ReadLine();
            if (clientPort.Length == 0)
            {
                clientPort = "0";
            }

            Console.WriteLine("Enter the IP to connect to");
            ip = Console.ReadLine();
            if (ip.Length == 0)
            {
                ip = "127.0.0.1";
            }

            Console.WriteLine("Enter the port to connect to");
            serverPort = Console.ReadLine();
            if (serverPort.Length == 0)
            {
                serverPort = "1234";
            }

            SocketDescriptor socketDescriptor = new SocketDescriptor(Convert.ToUInt16(clientPort), "0");

            socketDescriptor.socketFamily = AF_INET;

            client.Startup(8, socketDescriptor, 1);
            client.SetOccasionalPing(true);

            ConnectionAttemptResult car = client.Connect(ip, Convert.ToUInt16(serverPort), "Rumpelstiltskin", "Rumpelstiltskin".Length);

            if (car != RakNet.ConnectionAttemptResult.CONNECTION_ATTEMPT_STARTED)
            {
                throw new Exception();
            }

            Console.WriteLine("My IP Addresses:");
            for (uint i = 0; i < client.GetNumberOfAddresses(); i++)
            {
                Console.WriteLine(client.GetLocalIP(i).ToString());
            }
            Console.WriteLine("My GUID is " + client.GetGuidFromSystemAddress(RakNet.RakNet.UNASSIGNED_SYSTEM_ADDRESS).ToString());
            Console.WriteLine("'quit' to quit. 'stat' to show stats. 'ping' to ping.\n'disconnect' to disconnect. 'connect' to reconnnect. Type to talk.");
            string message;

            while (true)
            {
                System.Threading.Thread.Sleep(30);

                //Entire networking is threaded
                if (Console.KeyAvailable)
                {
                    message = Console.ReadLine();
                    if (message == "quit")
                    {
                        Console.WriteLine("Quitting");
                        break;
                    }

                    if (message == "stat")
                    {
                        string message2 = "";
                        rss = client.GetStatistics(client.GetSystemAddressFromIndex(0));
                        RakNet.RakNet.StatisticsToString(rss, out message2, 2);
                        Console.WriteLine(message2);
                        continue;
                    }

                    if (message == "disconnect")
                    {
                        Console.WriteLine("Enter index to disconnect: ");
                        string str = Console.ReadLine();
                        if (str == "")
                        {
                            str = "0";
                        }
                        uint index = Convert.ToUInt32(str, 16);
                        client.CloseConnection(client.GetSystemAddressFromIndex(index), false);
                        Console.WriteLine("Disconnecting");
                        continue;
                    }

                    if (message == "shutdown")
                    {
                        client.Shutdown(100);
                        Console.WriteLine("Disconnecting");
                        continue;
                    }

                    if (message == "ping")
                    {
                        if (client.GetSystemAddressFromIndex(0) != RakNet.RakNet.UNASSIGNED_SYSTEM_ADDRESS)
                        {
                            client.Ping(client.GetSystemAddressFromIndex(0));
                        }
                        continue;
                    }
                    if (message == "connect")
                    {
                        Console.WriteLine("Enter the IP to connect to");
                        ip = Console.ReadLine();
                        if (ip.Length == 0)
                        {
                            ip = "127.0.0.1";
                        }

                        Console.WriteLine("Enter the port to connect to");
                        serverPort = Console.ReadLine();
                        if (serverPort.Length == 0)
                        {
                            serverPort = "1234";
                        }

                        ConnectionAttemptResult car2 = client.Connect(ip, Convert.ToUInt16(serverPort), "Rumpelstiltskin", "Rumpelstiltskin".Length);

                        continue;
                    }
                    if (message == "getlastping")
                    {
                        if (client.GetSystemAddressFromIndex(0) != RakNet.RakNet.UNASSIGNED_SYSTEM_ADDRESS)
                        {
                            Console.WriteLine(client.GetLastPing(client.GetSystemAddressFromIndex(0)));
                        }

                        continue;
                    }

                    if (message.Length > 0)
                    {
                        client.Send(message, message.Length + 1, PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, RakNet.RakNet.UNASSIGNED_SYSTEM_ADDRESS, true);
                    }
                }

                for (p = client.Receive(); p != null; client.DeallocatePacket(p), p = client.Receive())
                {
                    packetIdentifier = GetPacketIdentifier(p);
                    switch ((DefaultMessageIDTypes)packetIdentifier)
                    {
                    case DefaultMessageIDTypes.ID_DISCONNECTION_NOTIFICATION:
                        Console.WriteLine("ID_DISCONNECTION_NOTIFICATION");
                        break;

                    case DefaultMessageIDTypes.ID_ALREADY_CONNECTED:
                        Console.WriteLine("ID_ALREADY_CONNECTED with guid " + p.guid);
                        break;

                    case DefaultMessageIDTypes.ID_INCOMPATIBLE_PROTOCOL_VERSION:
                        Console.WriteLine("ID_INCOMPATIBLE_PROTOCOL_VERSION ");
                        break;

                    case DefaultMessageIDTypes.ID_REMOTE_DISCONNECTION_NOTIFICATION:
                        Console.WriteLine("ID_REMOTE_DISCONNECTION_NOTIFICATION ");
                        break;

                    case DefaultMessageIDTypes.ID_REMOTE_CONNECTION_LOST:     // Server telling the clients of another client disconnecting forcefully.  You can manually broadcast this in a peer to peer enviroment if you want.
                        Console.WriteLine("ID_REMOTE_CONNECTION_LOST");
                        break;

                    case DefaultMessageIDTypes.ID_CONNECTION_BANNED:     // Banned from this server
                        Console.WriteLine("We are banned from this server.\n");
                        break;

                    case DefaultMessageIDTypes.ID_CONNECTION_ATTEMPT_FAILED:
                        Console.WriteLine("Connection attempt failed ");
                        break;

                    case DefaultMessageIDTypes.ID_NO_FREE_INCOMING_CONNECTIONS:
                        Console.WriteLine("Server is full ");
                        break;

                    case DefaultMessageIDTypes.ID_INVALID_PASSWORD:
                        Console.WriteLine("ID_INVALID_PASSWORD\n");
                        break;

                    case DefaultMessageIDTypes.ID_CONNECTION_LOST:
                        // Couldn't deliver a reliable packet - i.e. the other system was abnormally
                        // terminated
                        Console.WriteLine("ID_CONNECTION_LOST\n");
                        break;

                    case DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED:
                        // This tells the client they have connected
                        Console.WriteLine("ID_CONNECTION_REQUEST_ACCEPTED to %s " + p.systemAddress.ToString() + "with GUID " + p.guid.ToString());
                        Console.WriteLine("My external address is:" + client.GetExternalID(p.systemAddress).ToString());
                        break;

                    case DefaultMessageIDTypes.ID_CONNECTED_PING:
                    case DefaultMessageIDTypes.ID_UNCONNECTED_PING:
                        Console.WriteLine("Ping from " + p.systemAddress.ToString(true));
                        break;

                    default:
                        Console.WriteLine(System.Text.Encoding.UTF8.GetString(p.data));
                        break;
                    }
                }
            }
            client.Shutdown(300);
            RakNet.RakPeerInterface.DestroyInstance(client);
            Console.Read();
        }