Skip to content

Commit d46b6f0

Browse files
committed
Improve EDSM journal sync by using MultipartFormDataContent with serialized json for the message.
1 parent f22e383 commit d46b6f0

1 file changed

Lines changed: 12 additions & 17 deletions

File tree

StarMapService/EdsmJournalSync.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Net;
77
using System.Net.Http;
8+
using System.Text;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using Utilities;
@@ -24,9 +25,6 @@ public partial class StarMapService
2425
// The minimum interval between EDSM responder event syncs
2526
private const int syncIntervalMilliSeconds = 5000; // 5 seconds
2627

27-
// The maximum number of events sent in each batch to EDSM
28-
private const int MaxEventsPerBatch = 25; // Adjust as needed
29-
3028
private IEdsmHttpClient edsmJournalHttpClient { get; set; }
3129

3230
public void StartJournalSync ()
@@ -71,11 +69,11 @@ await Task.Run( async () =>
7169
holdingQueue.Add( pendingEvent );
7270

7371
// If we've reached the batch size or the queue is empty, send the batch
74-
if ( holdingQueue.Count >= MaxEventsPerBatch || queuedEvents.Count == 0 )
72+
if ( queuedEvents.Count == 0 )
7573
{
7674
// Once we hit zero queued events, wait a couple more seconds for any concurrent events to register
7775
await Task.Delay( 2000, syncCancellationTS.Token ).ConfigureAwait( false );
78-
if ( queuedEvents.Count > 0 && holdingQueue.Count < MaxEventsPerBatch )
76+
if ( queuedEvents.Count > 0 )
7977
{
8078
continue;
8179
}
@@ -160,23 +158,20 @@ private async Task SendEventBatchAsync ( List<IDictionary<string, object>> event
160158
if ( eventData.Count == 0 ) { return; }
161159

162160
var url = $"{baseUrl}api-journal-v1";
163-
var parameters = new Dictionary<string, string>
164-
{
165-
{ "commanderName", commanderName },
166-
{ "apiKey", apiKey },
167-
{ "fromSoftware", Constants.EDDI_NAME },
168-
{ "fromSoftwareVersion", Constants.EDDI_VERSION.ToString() },
169-
{ "fromGameVersion", gameVersion },
170-
{ "fromGameBuild", gameBuild },
171-
{ "message", JsonConvert.SerializeObject( eventData ).Normalize() }
172-
};
173-
174161
var maxRetries = 3;
175162
var delay = syncIntervalMilliSeconds; // Initial delay in milliseconds
176163
for ( var retry = 0; retry < maxRetries; retry++ )
177164
{
178-
using ( var httpContent = new FormUrlEncodedContent( parameters ) )
165+
using ( var httpContent = new MultipartFormDataContent() )
179166
{
167+
httpContent.Add( new StringContent( commanderName ), "commanderName" );
168+
httpContent.Add( new StringContent( apiKey ), "apiKey" );
169+
httpContent.Add( new StringContent( Constants.EDDI_NAME ), "fromSoftware" );
170+
httpContent.Add( new StringContent( Constants.EDDI_VERSION.ToString() ), "fromSoftwareVersion" );
171+
httpContent.Add( new StringContent( gameVersion ), "fromGameVersion" );
172+
httpContent.Add( new StringContent( gameBuild ), "fromGameBuild" );
173+
httpContent.Add( new StringContent( JsonConvert.SerializeObject( eventData ), Encoding.UTF8, "application/json" ), "message" );
174+
180175
try
181176
{
182177
Logging.Debug( "Sending message to EDSM: " + url, httpContent );

0 commit comments

Comments
 (0)