92 lines
3.1 KiB
C#
92 lines
3.1 KiB
C#
|
using System.Net;
|
||
|
using System.Net.Sockets;
|
||
|
using InnovEnergy.Lib.Protocols.DBus.Protocol;
|
||
|
using InnovEnergy.Lib.Protocols.DBus.Utils;
|
||
|
using InnovEnergy.Lib.Protocols.DBus.WireFormat;
|
||
|
|
||
|
namespace InnovEnergy.Lib.Protocols.DBus.Transport;
|
||
|
|
||
|
public class DBusSocket : DisposableStack
|
||
|
{
|
||
|
public Bus Bus { get; }
|
||
|
public Guid Id { get; }
|
||
|
|
||
|
private Socket Socket { get; }
|
||
|
private BufferedSocketReader Reader { get; }
|
||
|
|
||
|
public DBusSocket(Bus bus)
|
||
|
{
|
||
|
Bus = bus;
|
||
|
Socket = bus.EndPoint switch
|
||
|
{
|
||
|
UnixDomainSocketEndPoint => new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified),
|
||
|
IPEndPoint ipEndPoint => new Socket(ipEndPoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp),
|
||
|
|
||
|
_ => throw new ArgumentException($"Unsupported type of {nameof(EndPoint)}", nameof(bus.EndPoint))
|
||
|
};
|
||
|
|
||
|
try
|
||
|
{
|
||
|
Socket.Connect(bus.EndPoint); // Note: blocking (no async constructors)
|
||
|
Id = Bus.Auth.Authenticate(Socket);
|
||
|
}
|
||
|
catch
|
||
|
{
|
||
|
Socket.Dispose();
|
||
|
throw;
|
||
|
}
|
||
|
|
||
|
AddDisposable(Socket);
|
||
|
|
||
|
Reader = new BufferedSocketReader(Socket);
|
||
|
}
|
||
|
|
||
|
|
||
|
protected Boolean Connected => Socket.Connected;
|
||
|
|
||
|
protected Message ReadMessage()
|
||
|
{
|
||
|
var reader = new DBusSocketReader(Reader); // TODO: unclass?
|
||
|
return Message.Read(reader);
|
||
|
}
|
||
|
|
||
|
protected void SendMessage(Message message)
|
||
|
{
|
||
|
var writer = new DBusBufferWriter();
|
||
|
message.Write(writer);
|
||
|
|
||
|
// DumpMsg(message);
|
||
|
// Console.WriteLine(writer.Data.ToHexView2());
|
||
|
|
||
|
var data = writer.Data;
|
||
|
|
||
|
// DEBUG: test readback
|
||
|
// var reader = new DBusBufferReader(data, false);
|
||
|
// var msg = Message.Read(reader);
|
||
|
// var msgWriter = new DBusBufferWriter();
|
||
|
// msg.Write(msgWriter);
|
||
|
// var msgData = msgWriter.Data;
|
||
|
// var bol = msgData.SequenceEqual(data);
|
||
|
|
||
|
Socket.Send(data, SocketFlags.None);
|
||
|
}
|
||
|
|
||
|
public static void DumpMsg(Message msg)
|
||
|
{
|
||
|
Console.WriteLine("Type" + " " + msg.Type);
|
||
|
Console.WriteLine("Serial" + " " + msg.Serial);
|
||
|
Console.WriteLine("Signature" + " " + (msg.Signature?.ToString() ?? "None"));
|
||
|
Console.WriteLine("Interface" + " " + msg.Interface);
|
||
|
Console.WriteLine("Member" + " " + msg.Member);
|
||
|
Console.WriteLine("Destination" + " " + msg.Destination);
|
||
|
// Console.WriteLine("Sender " + " " + msg.Sender);
|
||
|
Console.WriteLine("ErrorName" + " " + msg.ErrorName);
|
||
|
Console.WriteLine("ReplySerial" + " " + msg.ReplySerial);
|
||
|
Console.WriteLine("PayloadLength" + " " + msg.PayloadLength);
|
||
|
Console.WriteLine("ObjectPath" + " " + msg.ObjectPath);
|
||
|
Console.WriteLine("Endian" + " " + msg.Endian);
|
||
|
Console.WriteLine("HasPayload" + " " + msg.HasPayload);
|
||
|
Console.WriteLine("IsError" + " " + msg.IsError);
|
||
|
Console.WriteLine("==================================================");
|
||
|
}
|
||
|
}
|