0

i am sending data from netty to an c# application in unity 3d game engine where it reads packet id and executes different methods depending on packet id ( inside a thread ),the packet id that the BinaryReader Reads is Wrong and i cant understand why

here is the classes custombuffer,datainputstream,dataoutputstream : https://docs.google.com/document/d/1LmW8fzGFFnkgqV_kS-TnBi7XvVYTC4Q4kHWJT60UF00/edit?usp=sharing

you dont have to read them carefully i posted them so you get an idea of what they are

this is the login method

public void SendLogin(string username,string password)
{
    Debug.Log ("Sending username and password");
    CustomBuffer buffer = new CustomBuffer (ClientOutput);
    buffer.WriteUTF (username);
    buffer.WriteUTF (password);
    buffer.SendBuffer ();
}

here is the server handler for the sendLogin

    public PacketEncoder sendLogin(boolean value, Account account,byte opcode) {

    ChannelBuffer buffer = new DynamicChannelBuffer(2);
    ChannelBufferOutputStream out = new ChannelBufferOutputStream(buffer);
    try {
        out.writeInt(0);
        out.writeBoolean(value);
        System.out.println("sendig login back packet 0");
        if(value) {
            System.out.println("value true sending username");
            out.writeUTF(account.getUsername());
        }

        else if(!value) {
            System.out.println("value false sending opcode : "+ String.valueOf( (int)opcode));
            out.writeByte(opcode);
        }

        Send(out.buffer());
        out.close();

    } catch(IOException e) {

        e.printStackTrace();
    }


    return this;
}

HERE ITS WORKING PROPERLY! below is the problem

this is the netty method that sends back information after character creation request

public PacketEncoder SendCharacterCreationResult(boolean exists,boolean success) {
        ChannelBuffer buffer = new DynamicChannelBuffer(2);
        ChannelBufferOutputStream out = new ChannelBufferOutputStream(buffer);
        try {
            out.writeInt(2);
            out.writeBoolean(exists);
            out.writeBoolean(success);

            //debug console 
            System.out.println(" ");
            System.out.println("Sending Character Creation Result packet : 2");
            System.out.println("exists : " + String.valueOf(exists));
            System.out.println("success : " + String.valueOf(success));

            Send(out.buffer());
            out.close();
        } catch(IOException e)
        {
            e.printStackTrace();
        }

        return this;
    }

this is the packet handler from c#

    void InputLoop()
    {
        while(Client.Connected)
        {
            int packetID = ClientInput.ReadInt();
            Debug.Log("packedID : " + packetID);
            switch(packetID)
            {
            case 0: //Login
                PacketsIn.Login();
                break;
            case 1://Character Request Response
                PacketsIn.CharacterRequest();
                break;
            case 2: //Character Creation Response
                PacketsIn.CharacterCreate();
                break;
            }
        }
    }

after login which its handled perfectrly server sends character creation result with packet id 2 and when it comes to the client first it runs the packetsIn.Login() (which is the packetid of 0) and the writeInt(2) from the SendCharacterCreationResult is readed from the method below where byte opcode = ClientInput.ReadByte (); that equals (2) i tried it like this int opcode = clientInput.ReadInt(); it gave the same result. if i confused u tell me to add full files in text

this is the function of case 0 in the inputloop (which is running in a thread)

public void Login()
{
    bool success = ClientInput.ReadBoolean ();
    if(success)
    {
        string username = ClientInput.ReadUTF();
        Debug.Log("Successfuly Logged In. " + username);
        MasterClient.singleton.ChangeScene(4);
    }
    byte opcode = ClientInput.ReadByte ();
    //GameObject.Find ("MessageLogsText").GetComponent<UILabel> ().text
        Debug.Log( GetLoginMessage (opcode) );
}

private string GetLoginMessage(byte opcode)
{
    switch(opcode)
    {
    case 0:
        return "Invalid Usarename Or Password";
    case 1:
        return "Account Is Already Logged In";
    case 2:
        return "test";
    default:
        return "Invalid opcode Sent From Server";
    }
}

then packet id becomes 513 and i cant understand why

3
  • 1
    Without seeing the actual code that's receiving the data, there's no way to even begin commenting on what might be wrong. But your first step is to debug the problem more; you need to do more research, which includes checking the data at every step, from transmission to the final receipt, so you first know exactly where the data is being misinterpreted. I will bet you have a byte-ordering problem somewhere, but that's just a wild guess. See stackoverflow.com/help/mcve and stackoverflow.com/help/how-to-ask Nov 17, 2014 at 22:55
  • hey peter my english is not very good i tried my best...i will try to edit it and add more content Nov 17, 2014 at 23:18
  • Also, I would like to comment that Peter Duniho is also correct, but that's not what's currently effecting the application. (I would know, because I wrote the original tutorial.) However, the version of DataInputStream that you're using deprecated and should be replaced. (Download link in part 11 of tutorial series)
    – Hobbyist
    Nov 18, 2014 at 9:06

2 Answers 2

0

In PacketEncoder#SendCharacterCreationResult you're sending three variables over the network

out.writeInt(2);
out.writeBoolean(exists);
out.writeBoolean(success);

Make sure that wherever you're reading the data you're reading all three variables, it seems like you're still using my deprecated packet-system which really needs to be replaced(You'll get there later in the series, come part 11)

You failed to provide the code that's handled when the client receives a packet-id of two, but the result that you're seeing is due to there being left-over data in the TCP Stream.

In the deprecated system the first thing that you send over the client is in integer(32-bits) which is interpreted by the client as the packets identification number (or opcode). In a video somewhere around you're current positon we changed the way that the packet-header was being read from a Byte(8bit) to an Int(32bit) make sure that whever you're reading the packets id(opcode) on the client you're reading an integer and not a Byte.

EDIT: I see that you already did this, didn't notice the posted code

Once you've done that, go into whatever method you're calling when you receive the Opcode of 2, then make sure that you're reading all of the data properly from PacketsIn#CharacterCreate you'll need to issue two DataInputStream#ReadBoolean() calls. (Note: DataInputStream instance is called ClientInput in your application).

Until you post the code from PacketsIn#CharacterCreate() I can't help any further.

1
  • i didn't provided the CharacterCreate() because the InputLoop doesn't run the case 2 which calls the CharacterCreate() instead it runs the case 0 which is the Login() which contains int opcode = ClientInput.ReadInt () , and opcode becomes 2 so something is wrong with the thread this 2 should be readed by the InputLoop's int packetID = ClientInput.ReadInt(); . if you notised i have Debug.Log(packetID) after the clientINput.ReadInt(); and it's executed after the opcode's Debug.Log , and it prints the number 513 , i don't how they execute out of order its not even possible, Nov 18, 2014 at 22:28
0

finally i figured it out! the problem was in the PacketDecoder#Login() Method, it was reading a int that isn't send by the server when the log in is successful so what i did, i putted these 2 lines of code in "else" condition this code is inside Login()

byte opcode = ClientInput.ReadByte (); Debug.Log( GetLoginMessage (opcode) ); }

that works because the server sends the opcode if value is false

and everything is working fine! maybe you should inform others for this because i checked the video out and u got it like me outside of an else condition

1
  • Didn't even notice that in your code, but yeah that would throw everything for a loop.
    – Hobbyist
    Nov 21, 2014 at 21:48

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.