Skip to content

Commit

Permalink
Update to Twosense's fork of Named Pipe Wrapper
Browse files Browse the repository at this point in the history
- Remove unused dependencies

- Update the README to link to the updated fork of NamedPipeWrapper
  • Loading branch information
Sparronator9999 committed Aug 9, 2024
1 parent 1081b17 commit a54991e
Show file tree
Hide file tree
Showing 18 changed files with 448 additions and 526 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ details.

This project makes use of the following third-party libraries:

- [Named Pipe Wrapper](https://github.com/acdvorak/named-pipe-wrapper), as `YAMDCC.IPC`,
for communication between the service and UI program.
- A modified version of Twosense's fork of [Named Pipe Wrapper](https://github.com/twosense/named-pipe-wrapper)
for communication between the service and UI program (called `YAMDCC.IPC` in the source files).
- [WinRing0](https://github.com/QCute/WinRing0) for low-level hardware access required to
read/write the EC.
92 changes: 0 additions & 92 deletions YAMDCC.GUI/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,96 +14,6 @@
"resolved": "4.5.0",
"contentHash": "221clPs1445HkTBZPL+K9sDBdJRB8UN8rgjO3ztB0CQ26z//fmJXtlsr6whGatscsKGBrhJl5bwJuKSA8mwFOw=="
},
"MessagePack": {
"type": "Transitive",
"resolved": "2.5.171",
"contentHash": "eX0wTFtE4teu2iDWm/eEGqvVEeO9dqaRXu6MXu1MnhQ/wBkixEjh+1SlKaSh41LL5L7WUJlK04EBpz8FSaGOqw==",
"dependencies": {
"MessagePack.Annotations": "2.5.171",
"Microsoft.Bcl.AsyncInterfaces": "6.0.0",
"Microsoft.NET.StringTools": "17.6.3",
"System.Collections.Immutable": "6.0.0",
"System.Runtime.CompilerServices.Unsafe": "6.0.0",
"System.Threading.Tasks.Extensions": "4.5.4"
}
},
"MessagePack.Annotations": {
"type": "Transitive",
"resolved": "2.5.171",
"contentHash": "iS5Zb6uiPn3QJVL814+xWlWeH/rW+p2qPoECjOhL7RV71ppld4vDpej5rq4YrxGdLtR66zf+AQwW919sPVMWgw=="
},
"Microsoft.Bcl.AsyncInterfaces": {
"type": "Transitive",
"resolved": "6.0.0",
"contentHash": "UcSjPsst+DfAdJGVDsu346FX0ci0ah+lw3WRtn18NUwEqRt70HaOQ7lI72vy3+1LxtqI3T5GWwV39rQSrCzAeg==",
"dependencies": {
"System.Threading.Tasks.Extensions": "4.5.4"
}
},
"Microsoft.NET.StringTools": {
"type": "Transitive",
"resolved": "17.6.3",
"contentHash": "N0ZIanl1QCgvUumEL1laasU0a7sOE5ZwLZVTn0pAePnfhq8P7SvTjF8Axq+CnavuQkmdQpGNXQ1efZtu5kDFbA==",
"dependencies": {
"System.Memory": "4.5.5",
"System.Runtime.CompilerServices.Unsafe": "6.0.0"
}
},
"System.Buffers": {
"type": "Transitive",
"resolved": "4.5.1",
"contentHash": "Rw7ijyl1qqRS0YQD/WycNst8hUUMgrMH4FCn1nNm27M4VxchZ1js3fVjQaANHO5f3sN4isvP4a+Met9Y4YomAg=="
},
"System.CodeDom": {
"type": "Transitive",
"resolved": "8.0.0",
"contentHash": "WTlRjL6KWIMr/pAaq3rYqh0TJlzpouaQ/W1eelssHgtlwHAH25jXTkUphTYx9HaIIf7XA6qs/0+YhtLEQRkJ+Q=="
},
"System.Collections.Immutable": {
"type": "Transitive",
"resolved": "6.0.0",
"contentHash": "l4zZJ1WU2hqpQQHXz1rvC3etVZN+2DLmQMO79FhOTZHMn8tDRr+WU287sbomD0BETlmKDn0ygUgVy9k5xkkJdA==",
"dependencies": {
"System.Memory": "4.5.4",
"System.Runtime.CompilerServices.Unsafe": "6.0.0"
}
},
"System.Management": {
"type": "Transitive",
"resolved": "8.0.0",
"contentHash": "jrK22i5LRzxZCfGb+tGmke2VH7oE0DvcDlJ1HAKYU8cPmD8XnpUT0bYn2Gy98GEhGjtfbR/sxKTVb+dE770pfA==",
"dependencies": {
"System.CodeDom": "8.0.0"
}
},
"System.Memory": {
"type": "Transitive",
"resolved": "4.5.5",
"contentHash": "XIWiDvKPXaTveaB7HVganDlOCRoj03l+jrwNvcge/t8vhGYKvqV+dMv6G4SAX2NoNmN0wZfVPTAlFwZcZvVOUw==",
"dependencies": {
"System.Buffers": "4.5.1",
"System.Numerics.Vectors": "4.5.0",
"System.Runtime.CompilerServices.Unsafe": "4.5.3"
}
},
"System.Numerics.Vectors": {
"type": "Transitive",
"resolved": "4.5.0",
"contentHash": "QQTlPTl06J/iiDbJCiepZ4H//BVraReU4O4EoRw1U02H5TLUIT7xn3GnDp9AXPSlJUDyFs4uWjWafNX6WrAojQ=="
},
"System.Runtime.CompilerServices.Unsafe": {
"type": "Transitive",
"resolved": "6.0.0",
"contentHash": "/iUeP3tq1S0XdNNoMz5C9twLSrM/TH+qElHkXWaPvuNOt+99G75NrV0OS2EqHx5wMN7popYjpc8oTjC1y16DLg=="
},
"System.Threading.Tasks.Extensions": {
"type": "Transitive",
"resolved": "4.5.4",
"contentHash": "zteT+G8xuGu6mS+mzDzYXbzS7rd3K6Fjb9RiZlYlJPam2/hU7JCBZBVEcywNuR+oZ1ncTvc/cq0faRr3P01OVg==",
"dependencies": {
"System.Runtime.CompilerServices.Unsafe": "4.5.3"
}
},
"yamdcc.config": {
"type": "Project",
"dependencies": {
Expand All @@ -121,7 +31,6 @@
"yamdcc.ipc": {
"type": "Project",
"dependencies": {
"MessagePack": "[2.5.171, )",
"Microsoft.CSharp": "[4.7.0, )",
"System.Data.DataSetExtensions": "[4.5.0, )"
}
Expand All @@ -138,7 +47,6 @@
"dependencies": {
"Microsoft.CSharp": "[4.7.0, )",
"System.Data.DataSetExtensions": "[4.5.0, )",
"System.Management": "[8.0.0, )",
"YAMDCC.Config": "[1.0.0, )",
"YAMDCC.ECAccess": "[1.0.0, )",
"YAMDCC.IPC": "[1.0.0, )",
Expand Down
55 changes: 42 additions & 13 deletions YAMDCC.IPC/IO/PipeStreamReader.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
using MessagePack;
using System;
using System.IO;
using System.IO.Pipes;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace YAMDCC.IPC.IO
{
/// <summary>
/// Wraps a <see cref="PipeStream"/> object and reads from it. Deserializes
/// binary data sent by a <see cref="PipeStreamWriter{T}"/> into a .NET CLR
/// object specified by <typeparamref name="T"/>.
/// Wraps a <see cref="PipeStream"/> object and reads from it.
/// Deserializes binary data sent by a <see cref="PipeStreamWriter{T}"/>
/// into a .NET CLR object specified by <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">Reference type to deserialize data to</typeparam>
public class PipeStreamReader<T>
/// <typeparam name="T">
/// Reference type to deserialize data to
/// </typeparam>
public class PipeStreamReader<T> where T : class
{
/// <summary>
/// Gets the underlying <c>PipeStream</c> object.
/// </summary>
public PipeStream BaseStream { get; }
public PipeStream BaseStream { get; private set; }

/// <summary>
/// Gets a value indicating whether the pipe is connected or not.
/// </summary>
public bool IsConnected { get; private set; }

private readonly BinaryFormatter _binaryFormatter = new BinaryFormatter();

/// <summary>
/// Constructs a new <c>PipeStreamReader</c> object that
/// Constructs a new <see cref="PipeStreamReader{T}"/> object that
/// reads data from the given <paramref name="stream"/>.
/// </summary>
/// <param name="stream">Pipe to read from</param>
Expand All @@ -41,9 +46,16 @@ public PipeStreamReader(PipeStream stream)
/// <summary>
/// Reads the length of the next message (in bytes) from the client.
/// </summary>
/// <returns>Number of bytes of data the client will be sending.</returns>
/// <exception cref="InvalidOperationException">The pipe is disconnected, waiting to connect, or the handle has not been set.</exception>
/// <exception cref="IOException">Any I/O error occurred.</exception>
/// <returns>
/// Number of bytes of data the client will be sending.
/// </returns>
/// <exception cref="InvalidOperationException">
/// The pipe is disconnected, waiting to connect,
/// or the handle has not been set.
/// </exception>
/// <exception cref="IOException">
/// Any I/O error occurred.
/// </exception>
private int ReadLength()
{
const int lensize = sizeof(int);
Expand All @@ -59,14 +71,17 @@ private int ReadLength()
: IPAddress.NetworkToHostOrder(BitConverter.ToInt32(lenbuf, 0));
}

/// <exception cref="SerializationException">
/// An object in the graph of type parameter
/// <typeparamref name="T"/> is not marked as serializable.
/// </exception>
private T ReadObject(int len)
{
byte[] data = new byte[len];
BaseStream.Read(data, 0, len);

using (MemoryStream memoryStream = new MemoryStream(data))
{
return MessagePackSerializer.Deserialize<T>(memoryStream);
return (T) _binaryFormatter.Deserialize(memoryStream);
}
}

Expand All @@ -86,8 +101,22 @@ private T ReadObject(int len)
/// </exception>
public T ReadObject()
{
if (typeof(T) == typeof(string))
{
return (T) ReadString();
}
int len = ReadLength();
return len == 0 ? default : ReadObject(len);
}

private object ReadString()
{
const int bufferSize = 1024;
byte[] data = new byte[bufferSize];
BaseStream.Read(data, 0, bufferSize);
string message = Encoding.Unicode.GetString(data).TrimEnd('\0');

return message.Length > 0 ? message : null;
}
}
}
55 changes: 32 additions & 23 deletions YAMDCC.IPC/IO/PipeStreamWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,39 @@ namespace YAMDCC.IPC.IO
/// <summary>
/// Wraps a <see cref="PipeStream"/> object to read and write .NET CLR objects.
/// </summary>
/// <typeparam name="TRdWr">Reference type to read from and write to the pipe</typeparam>
/// <typeparam name="TRdWr">
/// Reference type to read from and write to the pipe
/// </typeparam>
public class PipeStreamWrapper<TRdWr> : PipeStreamWrapper<TRdWr, TRdWr>
where TRdWr : class
{
/// <summary>
/// Constructs a new <c>PipeStreamWrapper</c> object that reads from and writes to the given <paramref name="stream"/>.
/// Constructs a new <c>PipeStreamWrapper</c> object that
/// reads from and writes to the given <paramref name="stream"/>.
/// </summary>
/// <param name="stream">Stream to read from and write to</param>
/// <param name="stream">
/// Stream to read from and write to
/// </param>
public PipeStreamWrapper(PipeStream stream) : base(stream) { }
}

/// <summary>
/// Wraps a <see cref="PipeStream"/> object to read and write .NET CLR objects.
/// </summary>
/// <typeparam name="TRd">Reference type to <b>read</b> from the pipe</typeparam>
/// <typeparam name="TWr">Reference type to <b>write</b> to the pipe</typeparam>
/// <typeparam name="TRd">
/// Reference type to <b>read</b> from the pipe
/// </typeparam>
/// <typeparam name="TWr">
/// Reference type to <b>write</b> to the pipe
/// </typeparam>
public class PipeStreamWrapper<TRd, TWr>
where TRd : class
where TWr : class
{
/// <summary>
/// Gets the underlying <c>PipeStream</c> object.
/// </summary>
public readonly PipeStream BaseStream;
public PipeStream BaseStream { get; private set; }

/// <summary>
/// Gets a value indicating whether the
Expand Down Expand Up @@ -65,8 +76,8 @@ public class PipeStreamWrapper<TRd, TWr>
private readonly PipeStreamWriter<TWr> _writer;

/// <summary>
/// Constructs a new <c>PipeStreamWrapper</c> object that reads from
/// and writes to the given <paramref name="stream"/>.
/// Constructs a new <c>PipeStreamWrapper</c> object that reads
/// from and writes to the given <paramref name="stream"/>.
/// </summary>
/// <param name="stream">Stream to read from and write to</param>
public PipeStreamWrapper(PipeStream stream)
Expand All @@ -77,8 +88,8 @@ public PipeStreamWrapper(PipeStream stream)
}

/// <summary>
/// Reads the next object from the pipe. This method blocks until an
/// object is sent or the pipe is disconnected.
/// Reads the next object from the pipe. This method blocks
/// until an object is sent or the pipe is disconnected.
/// </summary>
/// <returns>
/// The next object read from the pipe, or
Expand All @@ -88,20 +99,20 @@ public PipeStreamWrapper(PipeStream stream)
/// An object in the graph of type parameter
/// <typeparamref name="TRd"/> is not marked as serializable.
/// </exception>
public TRd ReadObject() =>
_reader.ReadObject();
public TRd ReadObject() => _reader.ReadObject();

/// <summary>
/// Writes an object to the pipe. This
/// method blocks until all data is sent.
/// Writes an object to the pipe.
/// This method blocks until all data is sent.
/// </summary>
/// <param name="obj">Object to write to the pipe</param>
/// <param name="obj">
/// Object to write to the pipe
/// </param>
/// <exception cref="SerializationException">
/// An object in the graph of type parameter
/// <typeparamref name="TRd"/> is not marked as serializable.
/// </exception>
public void WriteObject(TWr obj) =>
_writer.WriteObject(obj);
public void WriteObject(TWr obj) => _writer.WriteObject(obj);

/// <summary>
/// Waits for the other end of the pipe to read all sent bytes.
Expand All @@ -115,14 +126,12 @@ public void WriteObject(TWr obj) =>
/// <exception cref="IOException">
/// The pipe is broken or another I/O error occurred.
/// </exception>
public void WaitForPipeDrain() =>
_writer.WaitForPipeDrain();
public void WaitForPipeDrain() => _writer.WaitForPipeDrain();

/// <summary>
/// Closes the current stream and releases any resources (such as
/// sockets and file handles) associated with the current stream.
/// Closes the current stream and releases any resources
/// (such as sockets and file handles) associated with the current stream.
/// </summary>
public void Close() =>
BaseStream.Close();
public void Close() => BaseStream.Close();
}
}
Loading

0 comments on commit a54991e

Please sign in to comment.