Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Tmds.DBus.Protocol/IMessageStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface IMessageStream

void ReceiveMessages<T>(MessageReceivedHandler<T> handler, T state);

ValueTask<bool> TrySendMessageAsync(MessageBuffer message);
bool TrySendMessage(MessageBuffer message);

void BecomeMonitor();

Expand Down
46 changes: 15 additions & 31 deletions src/Tmds.DBus.Protocol/InnerConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
{
if (_state != DBusConnectionState.Connecting)
{
throw new DisconnectedException(DisconnectReason);

Check warning on line 173 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 173 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 173 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 173 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'
}
_messageStream = stream = new MessageStream(socket);
}
Expand All @@ -185,7 +185,7 @@
{
if (_state != DBusConnectionState.Connecting)
{
throw new DisconnectedException(DisconnectReason);

Check warning on line 188 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 188 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 188 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 188 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'
}
_state = DBusConnectionState.Connected;
}
Expand Down Expand Up @@ -228,7 +228,7 @@
{
if (_state != DBusConnectionState.Connecting)
{
throw new DisconnectedException(DisconnectReason);

Check warning on line 231 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 231 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 231 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 231 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'
}
_state = DBusConnectionState.Connected;
}
Expand All @@ -247,7 +247,7 @@
{
MyValueTaskSource<string?> vts = new();

await CallMethodAsync(
CallMethod(
message: CreateHelloMessage(),
static (Exception? exception, Message message, object? state) =>
{
Expand All @@ -265,7 +265,7 @@
{
vtsState.SetResult(null);
}
}, vts).ConfigureAwait(false);
}, vts);

return await new ValueTask<string?>(vts, token: 0).ConfigureAwait(false);

Expand Down Expand Up @@ -467,7 +467,7 @@
{
foreach (var pendingCall in _pendingCalls.Values)
{
pendingCall.Invoke(new DisconnectedException(disconnectReason), null!);

Check warning on line 470 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 470 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 470 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 470 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'
}
_pendingCalls.Clear();
}
Expand All @@ -478,7 +478,7 @@
{
bool emitException = !object.ReferenceEquals(disconnectReason, DBusConnection.DisposedException) ||
observer.EmitOnConnectionDispose;
Exception? exception = emitException ? new DisconnectedException(disconnectReason) : null;

Check warning on line 481 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 481 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 481 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 481 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'
observer.Dispose(exception, removeObserver: false);
}
}
Expand All @@ -489,25 +489,25 @@
lock (monitor)
{
_monitorHandler = null;
monitor(new DisconnectedException(disconnectReason), new DisposableMessage(null));

Check warning on line 492 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 492 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 492 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 492 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'
}
}

_disconnectedTcs?.SetResult(GetWaitForDisconnectException());
}

private ValueTask CallMethodAsync(MessageBuffer message, MessageReceivedHandler returnHandler, object? state)
private void CallMethod(MessageBuffer message, MessageReceivedHandler returnHandler, object? state)
{
MessageHandlerDelegate fn = static (Exception? exception, Message message, object? state1, object? state2, object? state3) =>
{
((MessageReceivedHandler)state1!)(exception, message, state2);
};
MessageHandler handler = new(fn, returnHandler, state);

return CallMethodAsync(message, handler);
CallMethod(message, handler);
}

private async ValueTask CallMethodAsync(MessageBuffer message, MessageHandler handler)
private void CallMethod(MessageBuffer message, MessageHandler handler)
{
bool messageSent = false;
try
Expand All @@ -516,7 +516,7 @@
{
if (_state != DBusConnectionState.Connected)
{
throw new DisconnectedException(DisconnectReason!);

Check warning on line 519 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 519 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 519 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'

Check warning on line 519 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DisconnectedException' is obsolete: 'Use DBusConnectionClosedException instead.'
}
if (_isMonitor)
{
Expand All @@ -528,7 +528,7 @@
}
}

messageSent = await _messageStream!.TrySendMessageAsync(message).ConfigureAwait(false);
messageSent = _messageStream!.TrySendMessage(message);
}
finally
{
Expand Down Expand Up @@ -574,7 +574,7 @@
MyValueTaskSource<T> vts = new();
MessageHandler handler = new(fn, valueReader, vts, state);

await CallMethodAsync(message, handler).ConfigureAwait(false);
CallMethod(message, handler);

return await new ValueTask<T>(vts, 0).ConfigureAwait(false);
}
Expand All @@ -583,8 +583,7 @@
{
MyValueTaskSource<object?> vts = new();

await CallMethodAsync(message,
static (Exception? exception, Message message, object? state) => CompleteCallValueTaskSource(exception, message, state), vts).ConfigureAwait(false);
CallMethod(message, static (Exception? exception, Message message, object? state) => CompleteCallValueTaskSource(exception, message, state), vts);

await new ValueTask(vts, 0).ConfigureAwait(false);
}
Expand All @@ -611,7 +610,7 @@
}
}

private static DBusException CreateDBusExceptionForErrorMessage(Message message)

Check warning on line 613 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DBusException' is obsolete: 'Use DBusErrorReplyException instead.'

Check warning on line 613 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DBusException' is obsolete: 'Use DBusErrorReplyException instead.'

Check warning on line 613 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DBusException' is obsolete: 'Use DBusErrorReplyException instead.'

Check warning on line 613 in src/Tmds.DBus.Protocol/InnerConnection.cs

View workflow job for this annotation

GitHub Actions / build

'DBusException' is obsolete: 'Use DBusErrorReplyException instead.'
{
string errorName = message.ErrorNameAsString ?? "<<No ErrorName>>.";
string errMessage = errorName;
Expand Down Expand Up @@ -750,19 +749,13 @@
};

_pendingCalls.Add(addMatchMessage.Serial, new(fn, matchMaker));

_messageStream!.TrySendMessage(addMatchMessage);
}
}

if (subscribe)
{
if (addMatchMessage is not null)
{
if (!await _messageStream!.TrySendMessageAsync(addMatchMessage).ConfigureAwait(false))
{
addMatchMessage.ReturnToPool();
}
}

try
{
await matchMaker.AddMatchTask!.ConfigureAwait(false);
Expand Down Expand Up @@ -995,7 +988,6 @@
private async void RemoveObserver(MatchMaker matchMaker, Observer observer)
{
string ruleString = matchMaker.RuleString;
bool sendMessage = false;

lock (_gate)
{
Expand All @@ -1007,23 +999,16 @@
if (_matchMakers.TryGetValue(ruleString, out _))
{
matchMaker.Observers.Remove(observer);
sendMessage = matchMaker.AddMatchTcs is not null && matchMaker.HasSubscribers;
bool sendMessage = matchMaker.AddMatchTcs is not null && matchMaker.HasSubscribers;
if (sendMessage)
{
_matchMakers.Remove(ruleString);
var message = CreateRemoveMatchMessage();
_messageStream!.TrySendMessage(message);
}
}
}

if (sendMessage)
{
var message = CreateRemoveMatchMessage();
if (!await _messageStream!.TrySendMessageAsync(message).ConfigureAwait(false))
{
message.ReturnToPool();
}
}

MessageBuffer CreateRemoveMatchMessage()
{
using var writer = GetMessageWriter();
Expand Down Expand Up @@ -1261,10 +1246,9 @@

public MessageWriter GetMessageWriter() => _parentDBusConnection.GetMessageWriter();

public async void SendMessage(MessageBuffer message)
public void SendMessage(MessageBuffer message)
{
bool messageSent = await _messageStream!.TrySendMessageAsync(message).ConfigureAwait(false);
if (!messageSent)
if (!_messageStream!.TrySendMessage(message))
{
message.ReturnToPool();
}
Expand Down
12 changes: 2 additions & 10 deletions src/Tmds.DBus.Protocol/MessageStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
{
if (guid != authenticationResult.Guid)
{
throw new ConnectException("Authentication failure: Unexpected GUID");

Check warning on line 212 in src/Tmds.DBus.Protocol/MessageStream.cs

View workflow job for this annotation

GitHub Actions / build

'ConnectException' is obsolete: 'Use DBusConnectFailedException instead.'

Check warning on line 212 in src/Tmds.DBus.Protocol/MessageStream.cs

View workflow job for this annotation

GitHub Actions / build

'ConnectException' is obsolete: 'Use DBusConnectFailedException instead.'

Check warning on line 212 in src/Tmds.DBus.Protocol/MessageStream.cs

View workflow job for this annotation

GitHub Actions / build

'ConnectException' is obsolete: 'Use DBusConnectFailedException instead.'

Check warning on line 212 in src/Tmds.DBus.Protocol/MessageStream.cs

View workflow job for this annotation

GitHub Actions / build

'ConnectException' is obsolete: 'Use DBusConnectFailedException instead.'
}
}

Expand Down Expand Up @@ -237,7 +237,7 @@
return result;
}

throw new ConnectException("Authentication failure");

Check warning on line 240 in src/Tmds.DBus.Protocol/MessageStream.cs

View workflow job for this annotation

GitHub Actions / build

'ConnectException' is obsolete: 'Use DBusConnectFailedException instead.'

Check warning on line 240 in src/Tmds.DBus.Protocol/MessageStream.cs

View workflow job for this annotation

GitHub Actions / build

'ConnectException' is obsolete: 'Use DBusConnectFailedException instead.'

Check warning on line 240 in src/Tmds.DBus.Protocol/MessageStream.cs

View workflow job for this annotation

GitHub Actions / build

'ConnectException' is obsolete: 'Use DBusConnectFailedException instead.'

Check warning on line 240 in src/Tmds.DBus.Protocol/MessageStream.cs

View workflow job for this annotation

GitHub Actions / build

'ConnectException' is obsolete: 'Use DBusConnectFailedException instead.'
}

private static string CreateAuthExternalCommand(string userId)
Expand Down Expand Up @@ -409,16 +409,8 @@
}
}

public async ValueTask<bool> TrySendMessageAsync(MessageBuffer message)
{
while (await _messageWriter.WaitToWriteAsync().ConfigureAwait(false))
{
if (_messageWriter.TryWrite(message))
return true;
}

return false;
}
public bool TrySendMessage(MessageBuffer message)
=> _messageWriter.TryWrite(message);

public void Close(Exception closeReason) => CloseCore(closeReason);

Expand Down
6 changes: 3 additions & 3 deletions test/Tmds.DBus.Protocol.Tests/PairedConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ public async void ReceiveMessages<T>(IMessageStream.MessageReceivedHandler<T> ha
}
}

public ValueTask<bool> TrySendMessageAsync(MessageBuffer message)
public bool TrySendMessage(MessageBuffer message)
{
_writeQueue.Enqueue(message);
_writeSemaphore.Release();
return ValueTask.FromResult(true);
return true;
}

public void Close(Exception? closeReason = null)
{
TrySendMessageAsync(null!); // Use null as EOF.
TrySendMessage(null!); // Use null as EOF.
}

public void BecomeMonitor()
Expand Down
Loading