Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Feedback Looking for Feedback for my TCP Socket packet system

Discussion in 'Multiplayer' started by AustinRichards, Apr 2, 2019.

  1. AustinRichards

    AustinRichards

    Joined:
    Apr 4, 2013
    Posts:
    321
    Last edited: Apr 3, 2019
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Looks simple and to the point. How are packets created?
     
  3. AustinRichards

    AustinRichards

    Joined:
    Apr 4, 2013
    Posts:
    321
    It has been modified and completed since last post.
    Packets are created quite simply.

    First you would create the packet class itself, and set the data. Currently it only supports integers, floats, shorts, strings, and longs. However, you can have it support arrays of these objects in the packet class when reading and writing the data. Simply send the length of the array and then loop through the contents, then in the receive function take in an int, then loop and take in a number of ints based on the received array size. It can also be modified to support other data types, and byte arrays quite easily.

    Here is the code to create an instance of a packet and send it. Any class that subscribes to packet 1 (Code example in ExampleManager) will receive the packet.

    Code (CSharp):
    1.             //Create packet instance
    2.             ExamplePacket_1 packet = new ExamplePacket_1();
    3.             //Assign packet data
    4.             packet.index = 243756;
    5.             packet.index2 = 1002;
    6.  
    7.             //Send packet
    8.             connectionManager.sendPacketToServer(packet);
     
    Joe-Censored likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yeah I think supporting byte arrays is important, as it is a nice catch all for sending any data type. In my network solution I send most data as custom C# classes which I serialize to byte arrays. The network solution gets the byte array to the other end and I deserialize it back to the original class instance for use with whatever I needed it for.

    I also created methods for serializing strings, Vector3's, and Quaternions, to and from byte arrays. BitConverter can handle all the basic types.
     
  5. AustinRichards

    AustinRichards

    Joined:
    Apr 4, 2013
    Posts:
    321
    Definitely. I'm going to add support for it now :).
     
    Joe-Censored likes this.