Shared Memory API

Discussion in 'Community Workshop' started by Mikael Hermansson, Jun 7, 2015.

  1. Gil Ritter

    Gil Ritter Well-Known Member

    Joined:
    Feb 26, 2015
    Ratings:
    +91 / 0 / -0
    It looks better now. We will check it for our result Import.
     
    • Like Like x 1
    • Agree Agree x 1
  2. Stefan Mizzi

    Stefan Mizzi Well-Known Member

    Joined:
    Feb 6, 2015
    Ratings:
    +625 / 0 / -0
    Hi Mikael,

    I managed to find some time and cooked up a quick example with the latest struct. Example is for reference only as I did not have time to test everything and check for null objects etc but should give the user a good idea how to do stuff.

    I have also gone a step further and made a class to read the JSON data. The JSON object in the link above is not really in a good format and I had to change it a bit to be more user friendly.

    Download link for R3ESharedMemoryApiExample http://www.mediafire.com/download/p27dap35we0b2kq

    Here is some info on the project classes:
    api.PNG

    1. R3E.cs is the class where it contains the latest API struct
    2. GameData.cs is the class which maps to the new (which I had to reformat) JSON data object
    3. R3E-GameData.json is the new formatted json object
    4. Constant.cs is for the constants used by R3E.cs
    5. MapMemory.cs is the class which reads the new data from memory
    6. RaceroomData.cs is the class used to get the game data (car, track etc) from the JSON object

    To use, it should be simple:

    1. Call Start() to initialize the timer which eventually starts memory reading and also load the JSON data in memory

    start.PNG

    2. When timer is elapsed:
    a. data is retrieved from memory and stored in object sharedResult
    b. To get track and car data, one has to call something like this. I just implemented a few methods only for example.

    //this is an example how to get the track data
    var trackData = raceroomData.GetTrackData(sharedResult.Data.TrackInfo.TrackId);

    //yet another example how to get the car class and team from the json object
    var classData = raceroomData.GetCarClass(driver.DriverInfo.ClassId).Name;
    var teamData = raceroomData.GeTeamData(driver.DriverInfo.TeamId).Name;

    //get driver name
    var driverName = System.Text.Encoding.Default.GetString(driver.DriverInfo.Name).Replace("/0", string.Empty)

    timer.PNG

    Did not give much thought to stufff so if anyone sees any issues please shout :)

    Enjoy!

    Cheers
    Stefan
     
  3. tobias.schlottbohm

    tobias.schlottbohm Member

    Joined:
    Jun 24, 2015
    Ratings:
    +17 / 0 / -0
    How do you cope with the name of the shared memory when running multiple instances on the same machine?
    '$Race$' cannot be used, because it won't differentiate between the several instances of the dedicated servers...

    @Stefan:
    Thanks for sharing the code :)

    Btw, concerning the dedicated server, I'd suggest to just add some info to the already existing kinda RESTful API of it to grab some more info. You're already exposing the following info when calling 'http://localhost:8088/dedi/?api_key=special-key'
    :
    Code:
    [
       {
          "GameSetting":{
             "Id":4924,
             "Name":"virtualracing.org 1",
             "Password":"vr",
             "Experience":2481,
             "TrackLayouts":[
                {
                   "LayoutId":1684,
                   "Sessions":{
                      "Qualify":{
                         "Duration":30,
                         "TimeOfDay":1,
                         "SessionType":0
                      },
                      "Practice":{
                         "Duration":60,
                         "TimeOfDay":3,
                         "SessionType":1
                      },
                      "Race1":{
                         "Duration":23,
                         "TimeOfDay":2,
                         "SessionType":1
                      }
                   }
                }
             ],
             "Cars":[
                4046,
                3899,
                4197,
                4067
             ],
             "Liveries":[
                3901,
                3902,
                3903,
                3904,
                4048,
                4049,
                4051,
                4053,
                4054,
                4056,
                4069,
                4070,
                4071,
                4199,
                4200,
                4202,
                4201
             ],
             "Difficulty":2,
             "MaxNumOfPlayers":24,
             "FuelUsage":1,
             "TireWear":1,
             "VisualDamage":2,
             "MechanicalDamage":1,
             "WarmupDuration":0,
             "RaceFinishDuration":100,
             "FlagRules":1,
             "CutRules":0,
             "RaceSeriesFormat":0,
             "StateReportInterval":3000,
             "KickNonReadyClientsTime":0,
             "CompressionLevel":3,
             "WreckerPrevention":0,
             "MandatoryPitstop":0
          },
          "ProcessState":{
             "Running":true,
             "Port":60000,
             "TimePort":60002,
             "PlayersOnServer":0,
             "TimeLeft":3599998
          }
       },
       {
          "GameSetting":{
             "Id":4934,
             "Name":"virtualracing.org 2",
             "Password":"",
             "Experience":2481,
             "TrackLayouts":[
                {
                   "LayoutId":1684,
                   "Sessions":{
                      "Qualify":{
                         "Duration":20,
                         "TimeOfDay":1,
                         "SessionType":0
                      },
                      "Practice":{
                         "Duration":20,
                         "TimeOfDay":3,
                         "SessionType":1
                      },
                      "Race1":{
                         "Duration":20,
                         "TimeOfDay":2,
                         "SessionType":1
                      }
                   }
                }
             ],
             "Cars":[
                4046,
                3899,
                4197,
                4067
             ],
             "Liveries":[
                3901,
                3902,
                3903,
                3904,
                4048,
                4049,
                4051,
                4053,
                4054,
                4056,
                4069,
                4070,
                4071,
                4199,
                4200,
                4202,
                4201
             ],
             "Difficulty":2,
             "MaxNumOfPlayers":24,
             "FuelUsage":1,
             "TireWear":1,
             "VisualDamage":2,
             "MechanicalDamage":1,
             "WarmupDuration":0,
             "RaceFinishDuration":100,
             "FlagRules":1,
             "CutRules":0,
             "RaceSeriesFormat":0,
             "StateReportInterval":3000,
             "KickNonReadyClientsTime":0,
             "CompressionLevel":3,
             "WreckerPrevention":0,
             "MandatoryPitstop":0
          },
          "ProcessState":{
             "Running":true,
             "Port":60003,
             "TimePort":60005,
             "PlayersOnServer":0,
             "TimeLeft":1199998
          }
       },
       {
          "GameSetting":{
             "Id":4935,
             "Name":"virtualracing.org 3",
             "Password":"vr",
             "Experience":2481,
             "TrackLayouts":[
                {
                   "LayoutId":1693,
                   "Sessions":{
                      "Qualify":{
                         "Duration":30,
                         "TimeOfDay":2,
                         "SessionType":0
                      },
                      "Practice":{
                         "Duration":60,
                         "TimeOfDay":1,
                         "SessionType":1
                      },
                      "Race1":{
                         "Duration":45,
                         "TimeOfDay":3,
                         "SessionType":1
                      }
                   }
                }
             ],
             "Cars":[
                3662,
                3549,
                3842,
                3539,
                3874
             ],
             "Liveries":[
                3541,
                3542,
                3543,
                3544,
                3545,
                3546,
                3547,
                3548,
                3557,
                3558,
                3559,
                3560,
                3561,
                3562,
                3563,
                3564,
                3565,
                3566,
                3664,
                3665,
                3666,
                3667,
                3846,
                3847,
                3876
             ],
             "Difficulty":2,
             "MaxNumOfPlayers":24,
             "FuelUsage":1,
             "TireWear":1,
             "VisualDamage":2,
             "MechanicalDamage":1,
             "WarmupDuration":0,
             "RaceFinishDuration":100,
             "FlagRules":1,
             "CutRules":0,
             "RaceSeriesFormat":0,
             "StateReportInterval":3000,
             "KickNonReadyClientsTime":0,
             "CompressionLevel":3,
             "WreckerPrevention":0,
             "MandatoryPitstop":0
          },
          "ProcessState":{
             "Running":true,
             "Port":60006,
             "TimePort":60008,
             "PlayersOnServer":0,
             "TimeLeft":3599997
          }
       },
       {
          "GameSetting":{
             "Id":4936,
             "Name":"virtualracing.org 4",
             "Password":"",
             "Experience":2481,
             "TrackLayouts":[
                {
                   "LayoutId":1693,
                   "Sessions":{
                      "Qualify":{
                         "Duration":15,
                         "TimeOfDay":2,
                         "SessionType":0
                      },
                      "Practice":{
                         "Duration":30,
                         "TimeOfDay":1,
                         "SessionType":1
                      },
                      "Race1":{
                         "Duration":25,
                         "TimeOfDay":3,
                         "SessionType":1
                      }
                   }
                }
             ],
             "Cars":[
                3662,
                3549,
                3842,
                3539,
                3874
             ],
             "Liveries":[
                3541,
                3542,
                3543,
                3544,
                3545,
                3546,
                3547,
                3548,
                3557,
                3558,
                3559,
                3560,
                3561,
                3562,
                3563,
                3564,
                3565,
                3566,
                3664,
                3665,
                3666,
                3667,
                3846,
                3847,
                3876
             ],
             "Difficulty":2,
             "MaxNumOfPlayers":24,
             "FuelUsage":1,
             "TireWear":1,
             "VisualDamage":2,
             "MechanicalDamage":1,
             "WarmupDuration":0,
             "RaceFinishDuration":100,
             "FlagRules":1,
             "CutRules":0,
             "RaceSeriesFormat":0,
             "StateReportInterval":3000,
             "KickNonReadyClientsTime":0,
             "CompressionLevel":3,
             "WreckerPrevention":0,
             "MandatoryPitstop":0
          },
          "ProcessState":{
             "Running":true,
             "Port":60015,
             "TimePort":60017,
             "PlayersOnServer":0,
             "TimeLeft":1799997
          }
       },
       {
          "GameSetting":{
             "Id":5607,
             "Name":"virtualracing.org 5",
             "Password":"",
             "Experience":2481,
             "TrackLayouts":[
                {
                   "LayoutId":2468,
                   "Sessions":{
                      "Qualify":{
                         "Duration":15,
                         "TimeOfDay":2,
                         "SessionType":1
                      },
                      "Practice":{
                         "Duration":30,
                         "TimeOfDay":3,
                         "SessionType":1
                      },
                      "Race1":{
                         "Duration":25,
                         "TimeOfDay":3,
                         "SessionType":1
                      }
                   }
                }
             ],
             "Cars":[
                4261,
                4267,
                4264
             ],
             "Liveries":[
                4263,
                4329,
                4325,
                4326,
                4327,
                4328,
                4330,
                4331,
                4378,
                4383,
                4381,
                4385,
                4382,
                4384,
                4379,
                4380,
                4320,
                4321,
                4269,
                4316,
                4317,
                4337,
                4318,
                4319
             ],
             "Difficulty":2,
             "MaxNumOfPlayers":24,
             "FuelUsage":1,
             "TireWear":1,
             "VisualDamage":2,
             "MechanicalDamage":1,
             "WarmupDuration":0,
             "RaceFinishDuration":100,
             "FlagRules":2,
             "CutRules":0,
             "RaceSeriesFormat":0,
             "StateReportInterval":3000,
             "KickNonReadyClientsTime":0,
             "CompressionLevel":3,
             "WreckerPrevention":0,
             "MandatoryPitstop":0
          },
          "ProcessState":{
             "Running":true,
             "Port":60012,
             "TimePort":60014,
             "PlayersOnServer":1,
             "TimeLeft":194840
          }
       }
    ]
    If you'd just include a bit more info into the 'ProcessState'-node, we'd be really happy :)
     
    Last edited: Oct 31, 2015
  4. pixeljetstream

    pixeljetstream Well-Known Member Beta tester

    Joined:
    Jan 29, 2015
    Ratings:
    +412 / 0 / -0
    Some feedback:

    Given the size of the struct I really hope we get those read/write counters, otherwise I'd think we never know if a frame is consistent (one counter increments on begin.edit, the other on end-edit, and compiler-flags ensure operations are kept in order).

    information how many drivers started the race (how much data we actually need to copy for the grid).
     
    Last edited: Nov 14, 2015
  5. pixeljetstream

    pixeljetstream Well-Known Member Beta tester

    Joined:
    Jan 29, 2015
    Ratings:
    +412 / 0 / -0
    was mentioned by @Racki before, suspension height per wheel would be nice to get :)
     
    • Agree Agree x 1
    Last edited: Nov 19, 2015
  6. Robert Holm

    Robert Holm KW Studios Developer

    Joined:
    Jan 15, 2015
    Ratings:
    +162 / 0 / -0
    Forgot to add here in time, sorry. Added new struct:

    typedef struct
    {
    r3e_int32 available;
    r3e_int32 engaged;
    r3e_int32 amount_left;
    r3e_float32 engaged_time_left;
    r3e_float32 wait_time_left;
    } r3e_push_to_pass;

    in this place:
    .
    .
    .
    // Info about track and layout
    r3e_track_info track_info;

    // Pust to pass data
    r3e_push_to_pass push_to_pass;


    // Contains name and vehicle info for all drivers in place order
    r3e_driver_data_1 all_drivers_data_1[128];
    .
    .
    .
     
    • Like Like x 1
  7. pixeljetstream

    pixeljetstream Well-Known Member Beta tester

    Joined:
    Jan 29, 2015
    Ratings:
    +412 / 0 / -0
    that will mean a compatibility break (first of its kind so far)
     
  8. Robert Holm

    Robert Holm KW Studios Developer

    Joined:
    Jan 15, 2015
    Ratings:
    +162 / 0 / -0
    Last thing we wanna do which makes me wonder. Double checking here, and PTP has been part of shared mem since Oct 1st (big shared mem patch was Oct 20th), but perhaps it didn't get incl in that patch for some reason.
     
  9. mr_belowski

    mr_belowski Well-Known Member Beta tester

    Joined:
    Apr 23, 2015
    Ratings:
    +1,307 / 0 / -0
    It was in the API docs but not in the block. I originally had it in my code but commented it out and remember having some issues at the time because it never made it into the October patch.

    Perhaps it should have been added after, rather than before the drivers array?
     
  10. Robert Holm

    Robert Holm KW Studios Developer

    Joined:
    Jan 15, 2015
    Ratings:
    +162 / 0 / -0
    It was added to the block a day later than all the new stuff, but way before Oct 20th patch. So its placement shouldn't matter in that case, except that for some reason it didn't make it into the public game until now, and cause of that the placement turned into a problem. Nothing to do about it now, though.
     
  11. Stefan Mizzi

    Stefan Mizzi Well-Known Member

    Joined:
    Feb 6, 2015
    Ratings:
    +625 / 0 / -0
    I understand that it breaks the code, but it's really a 2 minutes update to your class and recompile...no biggy for me....
     
  12. pixeljetstream

    pixeljetstream Well-Known Member Beta tester

    Joined:
    Jan 29, 2015
    Ratings:
    +412 / 0 / -0
    of course it's trivial to fix, if you are around :) what about software that is not updated as quickly (thinking third party hardware type of stuff).

    I actually like the alldrivers section at the end of the block, so maybe instead of a version number we could put the byte offset to the all drivers section somewhere. This way compatibility can be preserved even if things slide in front of alldrivers.
     
  13. rbn

    rbn Well-Known Member

    Joined:
    May 31, 2015
    Ratings:
    +69 / 0 / -0
    Is there an easy way to get you own best/previous sector times?
    Or is it only with the AllDriversData?

    If I add this to my C# app it instantly crashes:
    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 128)]
    public DriverData[] AllDriversData;
     
  14. mr_belowski

    mr_belowski Well-Known Member Beta tester

    Joined:
    Apr 23, 2015
    Ratings:
    +1,307 / 0 / -0
  15. rbn

    rbn Well-Known Member

    Joined:
    May 31, 2015
    Ratings:
    +69 / 0 / -0
    I still use the C# (old) sample code :X
    Everything works with that except the AllDriversData;

    I will have a look at your code.
    Its the first time I am trying C#, its pretty doable but these things make it a bit harder.
    The S3 sample was pretty easy to understand.
     
  16. mr_belowski

    mr_belowski Well-Known Member Beta tester

    Joined:
    Apr 23, 2015
    Ratings:
    +1,307 / 0 / -0
    because the new data contain these array types, the old method of reading doesn't work
     
  17. Stefan Mizzi

    Stefan Mizzi Well-Known Member

    Joined:
    Feb 6, 2015
    Ratings:
    +625 / 0 / -0
    Hi guys,

    I cannot find a way to get the server name I am connected to. Would it be possible that the server name is added to the shared memory? :)

    Thanks!
     
  18. pixeljetstream

    pixeljetstream Well-Known Member Beta tester

    Joined:
    Jan 29, 2015
    Ratings:
    +412 / 0 / -0
    If the shared memory is used for high frequency data in simulation (say simvibe type usage) then maybe we should split it up?
    the default keeps containing the important "player telemetry", and a new memory block, which is updated less frequent with the "all drivers" and other less "sim" information?
     
  19. Stefan Mizzi

    Stefan Mizzi Well-Known Member

    Joined:
    Feb 6, 2015
    Ratings:
    +625 / 0 / -0
    There seems to be some bugs regarding the best lap time and driver positions in the shared memory. As you can see in the image, I am capturing all laps, but twice I did best laps that were not recorded in the BestLap field. Also the result log file is showing the wrong best lap too (shown in image at the bottom).

    Also, I finished the race in 4th, but the final lap after I crossed the checkered flag I was tagged as 2nd in the shared memory. This is totally messing up end results and would appreciate if you have a look at it.

    Thanks!

    session_laps.PNG
     
  20. Stefan Mizzi

    Stefan Mizzi Well-Known Member

    Joined:
    Feb 6, 2015
    Ratings:
    +625 / 0 / -0
    I can't seem to find a way to tag a driver as AI or Real. If I knew that the session is Single Player or MP, then I can know at least but that is not available either as far as I know.

    Anyone knows if its possible to know if a driver is AI or not please? :)