Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection improvement #29

Closed
fenglinnet opened this issue Mar 1, 2017 · 9 comments
Closed

Connection improvement #29

fenglinnet opened this issue Mar 1, 2017 · 9 comments

Comments

@fenglinnet
Copy link

We are trying to use it instead of Lidgren.Network, hoping to have a connection approval mechanism and NetPeer's tag (binding object within the game). Thanks!

@RevenantX RevenantX self-assigned this Mar 1, 2017
RevenantX added a commit that referenced this issue Mar 1, 2017
@fenglinnet
Copy link
Author

Great, thanks for the quick response!
I still have some suggestions for connection improvements,

  1. I want to change ContionKey to byte [], and pass this data in the Connect event, which makes it easier to store authentication information and use it in connection event handling
  2. The server uses the developer custom authentication event processing
  3. Merge NetDataWriter and NetDateReader, for both read and write scenarios.
  4. Add the WriteAt interface to overwrite the placeholder data

By the way, I personally think that using Read or Write may be easier to replace Lidgren.Network :) Finally, my English is very poor and hope it is easy to understand, thanks!

@RevenantX
Copy link
Owner

RevenantX commented Mar 2, 2017

  1. I want to change ContionKey to byte [], and pass this data in the Connect event, which makes it easier to store authentication information and use it in connection event handling

Connection key is used for dropping traffic related to other network applications that can accidentally send some packets to your client/server.
I can add Lidgren-like ConnectionAccept packet. But i think its better and easier to do it in application layer. Because you will have full controll of this process)

  1. The server uses the developer custom authentication event processing

Can you explain in detail what this means? :)

  1. Merge NetDataWriter and NetDateReader, for both read and write scenarios.

This will lead to tons of potential bugs and sad users :). And there is rare case when you really need read and write. Can you show an example?

  1. Add the WriteAt interface to overwrite the placeholder data

What placeholder data? Where?) In NetDataWriter?

@fenglinnet
Copy link
Author

fenglinnet commented Mar 2, 2017

  1. I want to change ContionKey to byte [], and pass this data in the Connect event, which makes it easier to store authentication information and use it in connection event handling

Connection key is used for dropping traffic related to other network applications that can accidentally send some packets to your client/server.
I can add Lidgren-like ConnectionAccept packet. But i think its better and easier to do it in application layer. Because you will have full controll of this process)

I think ConnectKey can put more information, such as: the player's token and gameId and so on, I think less than 64 bytes should be enough to use, and no need to send ConnectionAccept packet again.

  1. The server uses the developer custom authentication event processing

Can you explain in detail what this means? :)

As above, because each player's token is unique, our service may use NetPeer's ConnectKey to make more judgments. Provide a delegate interface that can be done by the application layer

  1. Merge NetDataWriter and NetDateReader, for both read and write scenarios.

This will lead to tons of potential bugs and sad users :). And there is rare case when you really need read and write. Can you show an example?

When we collect the game states, we pass a NetDataWriter to the entity to write the state data. When the data returned, we need to read some information from the data, then we need to new a NetDataReader to do it. We are trying to avoid the GC. Of course, I'm not sure it good, but now it working: D

  1. Add the WriteAt interface to overwrite the placeholder data

What placeholder data? Where?) In NetDataWriter?

Yes!Sometimes we will first write an empty header information, after the data is written, update its contents.

@RevenantX
Copy link
Owner

RevenantX commented Mar 2, 2017

1 and 2 i understand) I will try to do something with this.
3. You can create NetDataReader once, and then just call dataReder.SetSource(dataWriter) (i added this method). It doesn't create copy.
4. Strange solution))) I used packets structure to resolve your problem)

    public class SpawnPacket : IBasePacket
    {
        public byte Id;
        public Vector3 Position;
        public PlayerTeam Team;
        public byte Weapon;

        public const int Size = 15;

        public void FromBytes(DataReader reader)
        {
            Id = reader.GetByte();
            Position = reader.GetVector3();
            Team = (PlayerTeam)reader.GetByte();
            Weapon = reader.GetByte();
        }

        public void ToBytes(DataWriter writer)
        {
            writer.Put(Id);
            writer.Put(Position);
            writer.Put((byte)Team);
            writer.Put(Weapon);
        }
    }

You can work easily with packet. And write only when sending.
And you can store this packet for later use.

@fenglinnet
Copy link
Author

To reduce the message size, we use bit to mark each field, for example:

public enum SpawnField
{
    Position    = 1 << 1,
    Team        = 1 << 2,
    Weapon      = 1 << 3,
}
public class SpawnPacket : IBasePacket
{
    public byte Id;
    public Vector3 Position;
    public PlayerTeam Team;
    public byte Weapon;

    public const int Size = 15;

    public void FromBytes(DataReader reader)
    {
        byte flags = reader.GetByte();
        Id = reader.GetByte();

        if ((flags & (byte)SpawnField.Position) != 0)
        {
            Position = reader.GetVector3();
        }

        Team = (PlayerTeam)reader.GetByte();
        Weapon = reader.GetByte();
    }

    public void ToBytes(DataWriter writer, SpawnPacket cachedPacket)
    {
        int flagsPosition = writer.Length;
        byte flags = 0;
        writer.Put(flags);
        writer.Put(Id);

        if (Position != cachedPacket.Position)
        {
            writer.Put(Position);
            Flags |= (byte)SpawnField.Position;
        }

        writer.Put((byte)Team);
        writer.Put(Weapon);

        // overwrite the flags
        writer.PutAt(flagsPosition, flags)
    }
}

@forestrf
Copy link
Contributor

forestrf commented Mar 3, 2017

I grabbed the Lidgren Networks's NetBuffer class and modified it to be able to write bits and use the method WriteRangedInteger and the like.
To account for the fact that now I need to know the bit length, not only the byte length, I am using an extra header of 3 bits at the start of the buffer to store the number of bits used in the last used byte. When I am done I update those 3 bits and send it by giving the byte array and its length.

It makes the buffer more cpu intensive, but for me it is worth it. I don't think it needs to be an option for the library as it has the send method that allows custom buffers to be sent

@RevenantX
Copy link
Owner

RevenantX commented Dec 30, 2017

Connection approval and reliable disconnect added and need testing in "packetprocessor" branch. Need your help guys in testing)

@RevenantX
Copy link
Owner

almost all implemented. (except write at)
0250bd3

@RevenantX
Copy link
Owner

RevenantX commented Jan 15, 2018

@fenglinnet so i think i can close this. For WriteAt() methods i think will be better to create another task (if you still need this :) ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants