Example #1
0
        public Server()
        {
            ServerInterface  = RakPeer.GetInstance();
            MessageListener  = new Dictionary <byte, IMessage>();
            ReceiveBitStream = new BitStream();
            SendBitStream    = new BitStream();


            initMessageListener();
        }
Example #2
0
    public int Start(out string ip, out ushort port, ushort maxConnections)
    {
        this.server = RakPeer.GetInstance();
        this.server.SetMaximumIncomingConnections(maxConnections);
        StartupResult result = this.server.Startup(maxConnections, new SocketDescriptor(), 1);

        SystemAddress adr = this.server.GetMyBoundAddress();

        ip   = adr.ToString(false);
        port = adr.GetPort();

        Log.WriteLine(string.Format("监听server {0}:{1}", ip, port));

        if (result != StartupResult.RAKNET_STARTED)
        {
            Log.WriteLine(string.Format("服务器启动失败 retCode:{0}", result));
            return(-1);
        }

        return(0);
    }
Example #3
0
    public int Start(string ip, ushort port)
    {
        this.client = RakPeer.GetInstance();

        SocketDescriptor socketDescriptor = new SocketDescriptor();

        socketDescriptor.socketFamily = 2;

        StartupResult result = this.client.Startup(1, socketDescriptor, 1);

        if (result != StartupResult.RAKNET_STARTED)
        {
            string str = string.Format("客户端启动失败 retCode:{0}", result);
            Log.WriteLine(str);
            return(-1);
        }

        ConnectionAttemptResult connResult = this.client.Connect(ip, port, "", 0);

        if (connResult != ConnectionAttemptResult.CONNECTION_ATTEMPT_STARTED)
        {
            string str = string.Format("连接失败 retCode:{0}", result);
            Log.WriteLine(str);
            return(-2);
        }

        begin = Time.time + SEND_INTERVAL;

        // remote target
        this.remote = new SystemAddress(ip, port);

        // send data
        this.data = new RakNet.BitStream();
        this.data.Reset();
        this.data.Write((byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM);
        this.data.Write("hello world!");

        return(0);
    }
Example #4
0
        static void Main(string[] args)
        {
            if (!File.Exists("RakNet.dll"))
            {
                Console.WriteLine("The SWIG build of the DLL has not been copied to the executable directory\nCopy from Swig/SwigWindowsCSharpSample/SwigTestApp/bin/X86/Debug/RakNet.dll to\nSwigWindowsCSharpSample/SwigTestApp/bin/Debug/RakNet.dll\nPress enter to quit.");
                Console.Read();
                return;
            }

            try
            {
                RakString dllCallTest = new RakString();
            }
            catch (Exception e)
            {
                Console.WriteLine("DLL issue\nMake sure RakNetWrap.cxx is included in the DLL project.\nPress enter to quit.");
                Console.Read();
                return;
            }

            Packet    testPacket;
            int       loopNumber;
            BitStream stringTestSendBitStream    = new BitStream();
            BitStream rakStringTestSendBitStream = new BitStream();
            BitStream receiveBitStream           = new BitStream();
            String    holdingString;
            TimeSpan  startTimeSpan;
            RakString rakStringTest = new RakString();

            RakPeerInterface testClient = RakPeer.GetInstance();

            testClient.Startup(1, new SocketDescriptor(60000, "127.0.0.1"), 1);

            RakPeerInterface testServer = RakPeer.GetInstance();

            testServer.Startup(1, new SocketDescriptor(60001, "127.0.0.1"), 1);
            testServer.SetMaximumIncomingConnections(1);

            Console.WriteLine("Send and receive loop using BitStream.\nBitStream read done into RakString");

            testClient.Connect("127.0.0.1", 60001, "", 0);

            String sendString = "The test string";

            stringTestSendBitStream.Write((byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM);
            stringTestSendBitStream.Write(sendString);

            RakString testRakString = new RakString("Test RakString");

            rakStringTestSendBitStream.Write((byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM);
            rakStringTestSendBitStream.Write(testRakString);

            startTimeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1));
            loopNumber    = 0;

            while (startTimeSpan.TotalSeconds + 5 > (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds)
            {
                testPacket = testServer.Receive();
                if (testPacket != null && testPacket.data[0] == (byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM)
                {
                    receiveBitStream.Reset();
                    receiveBitStream.Write(testPacket.data, testPacket.length);
                    receiveBitStream.IgnoreBytes(1);
                    receiveBitStream.Read(rakStringTest);
                    Console.WriteLine("Loop number: " + loopNumber + "\nData: " + rakStringTest.C_String());
                }
                testServer.DeallocatePacket(testPacket);
                loopNumber++;
                System.Threading.Thread.Sleep(50);
                testClient.Send(rakStringTestSendBitStream, PacketPriority.LOW_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, new AddressOrGUID(new SystemAddress("127.0.0.1", 60001)), false);
            }

            Console.WriteLine("String send and receive loop using BitStream.\nBitStream read done into String");

            SystemAddress[] remoteSystems;
            ushort          numberOfSystems = 1;

            testServer.GetConnectionList(out remoteSystems, ref numberOfSystems);

            startTimeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1));
            loopNumber    = 0;
            while (startTimeSpan.TotalSeconds + 5 > (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds)
            {
                testPacket = testServer.Receive();
                if (testPacket != null && testPacket.data[0] == (byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM)
                {
                    receiveBitStream.Reset();
                    receiveBitStream.Write(testPacket.data, testPacket.length);
                    receiveBitStream.IgnoreBytes(1);
                    receiveBitStream.Read(out holdingString);
                    Console.WriteLine("Loop number: " + loopNumber + "\nData: " + holdingString);
                }
                testServer.DeallocatePacket(testPacket);
                loopNumber++;
                System.Threading.Thread.Sleep(50);
                testClient.Send(stringTestSendBitStream, PacketPriority.LOW_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, new AddressOrGUID(new SystemAddress("127.0.0.1", 60001)), false);
            }
            //If RakString is not freed before program exit it will crash
            rakStringTest.Dispose();
            testRakString.Dispose();

            RakPeer.DestroyInstance(testClient);
            RakPeer.DestroyInstance(testServer);
            Console.WriteLine("Demo complete. Press Enter.");
            Console.Read();
        }
Example #5
0
        public Client()
        {
            client = RakPeer.GetInstance();

            messageListener.Add((byte)NetworkID.ConnectionMessage, new ConnectionMessage());
            messageListener.Add((byte)NetworkID.CreateItemInstanceMessage, new CreateItemInstanceMessage());
            messageListener.Add((byte)NetworkID.PlayerFreezeMessage, new PlayerFreezeMessage());
            messageListener.Add((byte)NetworkID.AddItemMessage, new AddItemMessage());
            messageListener.Add((byte)NetworkID.StartDialogAnimMessage, new StartDialogAnimMessage());
            messageListener.Add((byte)NetworkID.AnimationMessage, new AnimationMessage());
            messageListener.Add((byte)NetworkID.NPCChangeAttributeMessage, new NPCChangeAttributeMessage());
            messageListener.Add((byte)NetworkID.NPCChangeSkillMessage, new NPCChangeSkillMessage());

            messageListener.Add((byte)NetworkID.NPCSpawnMessage, new NPCSpawnMessage());
            messageListener.Add((byte)NetworkID.GuiMessage, new GuiMessage());

            messageListener.Add((byte)NetworkID.DropUnconsciousMessage, new DropUnconsciousMessage());
            messageListener.Add((byte)NetworkID.ReviveMessage, new ReviveMessage());
            messageListener.Add((byte)NetworkID.SetVisualMessage, new SetVisualMessage());
            messageListener.Add((byte)NetworkID.DropItemMessage, new DropItemMessage());
            messageListener.Add((byte)NetworkID.TakeItemMessage, new TakeItemMessage());
            messageListener.Add((byte)NetworkID.CamToVobMessage, new CamToVobMessage());
            messageListener.Add((byte)NetworkID.ChangeNameMessage, new ChangeNameMessage());

            messageListener.Add((byte)NetworkID.CreateVobMessage, new CreateVobMessage());
            messageListener.Add((byte)NetworkID.SpawnVobMessage, new SpawnVobMessage());
            messageListener.Add((byte)NetworkID.DespawnVobMessage, new DespawnVobMessage());

            messageListener.Add((byte)NetworkID.SetVobPositionMessage, new SetVobPositionMessage());
            messageListener.Add((byte)NetworkID.SetVobDirectionMessage, new SetVobDirectionMessage());
            messageListener.Add((byte)NetworkID.SetVobPosDirMessage, new SetVobPosDirMessage());

            messageListener.Add((byte)NetworkID.DisconnectMessage, new DisconnectMessage());
            messageListener.Add((byte)NetworkID.AnimationUpdateMessage, new AnimationUpdateMessage());

            messageListener.Add((byte)NetworkID.NPCUpdateMessage, new NPCUpdateMessage());

            messageListener.Add((byte)NetworkID.ItemChangeAmount, new ItemChangeAmount());

            messageListener.Add((byte)NetworkID.ItemChangeContainer, new ItemChangeContainer());

            messageListener.Add((byte)NetworkID.ClearInventory, new ClearInventory());

            messageListener.Add((byte)NetworkID.TimerMessage, new TimerMessage());
            messageListener.Add((byte)NetworkID.RainMessage, new RainMessage());

            messageListener.Add((byte)NetworkID.CallbackNPCCanSee, new CallbackNPCCanSee());

            messageListener.Add((byte)NetworkID.ExitGameMessage, new ExitGameMessage());

            messageListener.Add((byte)NetworkID.ReadIniEntryMessage, new ReadIniEntryMessage());
            messageListener.Add((byte)NetworkID.ReadMd5Message, new ReadMd5Message());

            messageListener.Add((byte)NetworkID.EquipItemMessage, new EquipItemMessage());
            messageListener.Add((byte)NetworkID.ChangeWorldMessage, new ChangeWorldMessage());

            messageListener.Add((byte)NetworkID.MobInterMessage, new MobInterMessage());

            messageListener.Add((byte)NetworkID.NPCSetInvisibleMessage, new NPCSetInvisibleMessage());
            messageListener.Add((byte)NetworkID.NPCSetInvisibleName, new NPCSetInvisibleName());
            messageListener.Add((byte)NetworkID.PlayVideo, new PlayVideo());

            messageListener.Add((byte)NetworkID.NPCControllerMessage, new NPCControllerMessage());

            messageListener.Add((byte)NetworkID.ScaleMessage, new ScaleMessage());
            messageListener.Add((byte)NetworkID.NPCFatnessMessage, new NPCFatnessMessage());


            messageListener.Add((byte)NetworkID.NPCEnableMessage, new NPCEnableMessage());
            messageListener.Add((byte)NetworkID.CreateSpellMessage, new CreateSpellMessage());

            messageListener.Add((byte)NetworkID.PlayEffectMessage, new PlayEffectMessage());

            messageListener.Add((byte)NetworkID.CastSpell, new CastSpellMessage());
            messageListener.Add((byte)NetworkID.SpellInvestMessage, new SpellInvestMessage());

            messageListener.Add((byte)NetworkID.NPCProtoSetWeaponMode, new NPCProtoSetWeaponMode());
            messageListener.Add((byte)NetworkID.SetSlotMessage, new SetSlotMessage());

            messageListener.Add((byte)NetworkID.CamToPlayerFront, new CamToPlayerFront());

            messageListener.Add((byte)NetworkID.InterfaceOptionsMessage, new InterfaceOptionsMessage());
        }
Example #6
0
    // Use this for initialization
    void Start()
    {
        try
        {
            RakString dllCallTest = new RakString();
        }
        catch (Exception e)
        {
            Debug.Log("DLL issue\nAdd SwigOutput/CplusDLLIncludes/RakNetWrap.cxx to the project\nDLL_Swig/RakNet.sln and rebuild.\nPress enter to quit.");
            return;
        }

        Packet testPacket;
        int    loopNumber;

        RakNet.BitStream stringTestSendBitStream    = new RakNet.BitStream();
        RakNet.BitStream rakStringTestSendBitStream = new RakNet.BitStream();
        RakNet.BitStream receiveBitStream           = new RakNet.BitStream();
        String           holdingString;
        TimeSpan         startTimeSpan;
        RakString        rakStringTest = new RakString();

        RakPeerInterface testClient = RakPeer.GetInstance();

        testClient.Startup(1, new SocketDescriptor(60000, "127.0.0.1"), 1);

        RakPeerInterface testServer = RakPeer.GetInstance();

        testServer.Startup(1, new SocketDescriptor(60001, "127.0.0.1"), 1);
        testServer.SetMaximumIncomingConnections(1);

        Console.WriteLine("Send and receive loop using BitStream.\nBitStream read done into RakString");

        testClient.Connect("127.0.0.1", 60001, "", 0);

        String sendString = "The test string";

        stringTestSendBitStream.Write((byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM);
        stringTestSendBitStream.Write(sendString);

        RakString testRakString = new RakString("Test RakString");

        rakStringTestSendBitStream.Write((byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM);
        rakStringTestSendBitStream.Write(testRakString);

        startTimeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1));
        loopNumber    = 0;

        while (startTimeSpan.TotalSeconds + 5 > (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds)
        {
            testPacket = testServer.Receive();
            if (testPacket != null && testPacket.data[0] == (byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM)
            {
                receiveBitStream.Reset();
                receiveBitStream.Write(testPacket.data, testPacket.length);
                receiveBitStream.IgnoreBytes(1);
                receiveBitStream.Read(rakStringTest);
                Debug.Log("Loop number: " + loopNumber + "\nData: " + rakStringTest.C_String());
            }
            testServer.DeallocatePacket(testPacket);
            loopNumber++;
            System.Threading.Thread.Sleep(50);
            testClient.Send(rakStringTestSendBitStream, PacketPriority.LOW_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, new AddressOrGUID(new SystemAddress("127.0.0.1", 60001)), false);
        }

        Debug.Log("String send and receive loop using BitStream.\nBitStream read done into String");

        SystemAddress[] remoteSystems;
        ushort          numberOfSystems = 1;

        testServer.GetConnectionList(out remoteSystems, ref numberOfSystems);

        startTimeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1));
        loopNumber    = 0;
        while (startTimeSpan.TotalSeconds + 5 > (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds)
        {
            testPacket = testServer.Receive();
            if (testPacket != null && testPacket.data[0] == (byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM)
            {
                receiveBitStream.Reset();
                receiveBitStream.Write(testPacket.data, testPacket.length);
                receiveBitStream.IgnoreBytes(1);
                receiveBitStream.Read(out holdingString);
                Debug.Log("Loop number: " + loopNumber + "\nData: " + holdingString);
            }
            testServer.DeallocatePacket(testPacket);
            loopNumber++;
            System.Threading.Thread.Sleep(50);
            SystemAddress sa = RakNet.RakNet.UNASSIGNED_SYSTEM_ADDRESS;
            testClient.Send(stringTestSendBitStream, PacketPriority.LOW_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, new AddressOrGUID(new SystemAddress("127.0.0.1", 60001)), false);
        }
        //If RakString is not freed before program exit it will crash
        rakStringTest.Dispose();
        testRakString.Dispose();

        RakPeer.DestroyInstance(testClient);
        RakPeer.DestroyInstance(testServer);
        Debug.Log("Demo complete. Press Enter.");
    }
Example #7
0
        static void Main(string[] args)
        {
            Packet    testPacket;
            int       loopNumber;
            BitStream stringTestSendBitStream    = new BitStream();
            BitStream rakStringTestSendBitStream = new BitStream();
            BitStream receiveBitStream           = new BitStream();
            String    holdingString;
            TimeSpan  startTimeSpan;
            RakString rakStringTest = new RakString();

            RakPeerInterface testClient = RakPeer.GetInstance();

            testClient.Startup(1, new SocketDescriptor(60000, "127.0.0.1"), 1);

            RakPeerInterface testServer = RakPeer.GetInstance();

            testServer.Startup(1, new SocketDescriptor(60001, "127.0.0.1"), 1);
            testServer.SetMaximumIncomingConnections(1);

            Console.WriteLine("Press enter to start RakString send and receive loop using BitStream.\nBitStream read done into RakString");
            Console.WriteLine("Loop will run for 15 seconds");
            Console.ReadLine();

            testClient.Connect("127.0.0.1", 60001, "", 0);

            String sendString = "The test string";

            stringTestSendBitStream.Write((byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM);
            stringTestSendBitStream.Write(sendString);

            RakString testRakString = new RakString("Test RakString");

            rakStringTestSendBitStream.Write((byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM);
            rakStringTestSendBitStream.Write(testRakString);

            startTimeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1));
            loopNumber    = 0;

            while (startTimeSpan.TotalSeconds + 15 > (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds)
            {
                testPacket = testServer.Receive();
                if (testPacket != null && testPacket.data[0] == (byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM)
                {
                    receiveBitStream.Reset();
                    receiveBitStream.Write(testPacket.data, testPacket.length);
                    receiveBitStream.IgnoreBytes(1);
                    receiveBitStream.Read(rakStringTest);
                    Console.WriteLine("Loop number: " + loopNumber + "\nData: " + rakStringTest.C_String());
                }
                testServer.DeallocatePacket(testPacket);
                loopNumber++;
                System.Threading.Thread.Sleep(50);
                testClient.Send(rakStringTestSendBitStream, PacketPriority.LOW_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, new AddressOrGUID(new SystemAddress("127.0.0.1", 60001)), false);
            }

            Console.WriteLine("Press enter to start String send and receive loop using BitStream.\nBitStream read done into String");
            Console.WriteLine("Loop will run for 15 seconds");
            Console.ReadLine();

            startTimeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1));
            loopNumber    = 0;
            while (startTimeSpan.TotalSeconds + 15 > (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds)
            {
                testPacket = testServer.Receive();
                if (testPacket != null && testPacket.data[0] == (byte)DefaultMessageIDTypes.ID_USER_PACKET_ENUM)
                {
                    receiveBitStream.Reset();
                    receiveBitStream.Write(testPacket.data, testPacket.length);
                    receiveBitStream.IgnoreBytes(1);
                    receiveBitStream.Read(out holdingString);
                    Console.WriteLine("Loop number: " + loopNumber + "\nData: " + holdingString);
                }
                testServer.DeallocatePacket(testPacket);
                loopNumber++;
                System.Threading.Thread.Sleep(50);
                testClient.Send(stringTestSendBitStream, PacketPriority.LOW_PRIORITY, PacketReliability.RELIABLE_ORDERED, (char)0, new AddressOrGUID(new SystemAddress("127.0.0.1", 60001)), false);
            }
            //If RakString is not freed before program exit it will crash
            rakStringTest.Dispose();
            testRakString.Dispose();

            RakPeer.DestroyInstance(testClient);
            RakPeer.DestroyInstance(testServer);
            Console.WriteLine("Demo complete. Press Enter.");
            Console.Read();
        }