this.queuedApplicationData.Clear();
}
- /// <inheritdoc />
- protected override void WriteBytesToConnection(byte[] bytes, int length)
+ /// <summary>
+ /// Request from the application to write data to the DTLS
+ /// stream. If appropriate, returns a byte span to send to
+ /// the wire.
+ /// </summary>
+ /// <param name="bytes">Plaintext bytes to write</param>
+ /// <param name="length">Length of the bytes to write</param>
+ /// <returns>
+ /// Encrypted data to put on the wire if appropriate,
+ /// otherwise an empty span
+ /// </returns>
+ private ByteSpan WriteBytesToConnectionInternal(byte[] bytes, int length)
{
lock (this.syncRoot)
{
new ByteSpan(bytes, 0, length).CopyTo(copyOfSpan);
this.queuedApplicationData.Add(copyOfSpan);
- return;
+ return ByteSpan.Empty;
}
// Send any queued application data now
, ref outgoinRecord
);
- base.WriteBytesToConnection(packet.GetUnderlyingArray(), packet.Length);
+ return packet;
+ }
+ }
+
+ /// <inheritdoc />
+ protected override void WriteBytesToConnection(byte[] bytes, int length)
+ {
+ ByteSpan wireData = this.WriteBytesToConnectionInternal(bytes, length);
+ if (wireData.Length > 0)
+ {
+ Debug.Assert(wireData.Offset == 0, "Got a non-zero write data offset");
+ base.WriteBytesToConnection(wireData.GetUnderlyingArray(), wireData.Length);
+ }
+ }
+
+ /// <inheritdoc />
+ protected override void WriteBytesToConnectionSync(byte[] bytes, int length)
+ {
+ ByteSpan wireData = this.WriteBytesToConnectionInternal(bytes, length);
+ if (wireData.Length > 0)
+ {
+ Debug.Assert(wireData.Offset == 0, "Got a non-zero write data offset");
+ base.WriteBytesToConnectionSync(wireData.GetUnderlyingArray(), wireData.Length);
}
}
public class UnityUdpClientConnection : UdpConnection
{
private Socket socket;
- private bool sendSynchronously = false;
public UnityUdpClientConnection(IPEndPoint remoteEndPoint, IPMode ipMode = IPMode.IPv4)
: base()
{
try
{
- if (this.sendSynchronously)
- {
- socket.SendTo(
- bytes,
- 0,
- length,
- SocketFlags.None,
- EndPoint);
- }
- else
- {
- socket.BeginSendTo(
- bytes,
- 0,
- length,
- SocketFlags.None,
- EndPoint,
- HandleSendTo,
- null);
- }
+ socket.BeginSendTo(
+ bytes,
+ 0,
+ length,
+ SocketFlags.None,
+ EndPoint,
+ HandleSendTo,
+ null);
+ }
+ catch (NullReferenceException) { }
+ catch (ObjectDisposedException)
+ {
+ // Already disposed and disconnected...
+ }
+ catch (SocketException ex)
+ {
+ DisconnectInternal(HazelInternalErrors.SocketExceptionSend, "Could not send data as a SocketException occurred: " + ex.Message);
+ }
+ }
+
+ /// <summary>
+ /// Synchronously writes the given bytes to the connection.
+ /// </summary>
+ /// <param name="bytes">The bytes to write.</param>
+ protected virtual void WriteBytesToConnectionSync(byte[] bytes, int length)
+ {
+ try
+ {
+ socket.SendTo(
+ bytes,
+ 0,
+ length,
+ SocketFlags.None,
+ EndPoint);
}
catch (NullReferenceException) { }
catch (ObjectDisposedException)
try
{
- this.WriteBytesToConnection(bytes, bytes.Length);
+ this.WriteBytesToConnectionSync(bytes, bytes.Length);
}
catch { }
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
- this.sendSynchronously = true;
-
if (disposing)
{
SendDisconnect();