Compare commits

...

2 commits

Author SHA1 Message Date
b2646e7340 NOP and ACK shit 2023-12-10 15:21:13 -05:00
3716ba482f add ACK 2023-12-10 13:55:12 -05:00
3 changed files with 53 additions and 3 deletions

View file

@ -17,6 +17,7 @@ namespace CollabVMAgent
static readonly string UploadsFolder = Environment.GetEnvironmentVariable("USERPROFILE") + @"\Desktop";
static MessagePackSerializer serializer = MessagePackSerializer.Get<ProtocolMessage>();
static VirtIOSerial vio;
static void Main(string[] args)
{
if (!IsAdmin())
@ -31,8 +32,16 @@ namespace CollabVMAgent
#if DEBUG
Console.WriteLine("Starting CollabVM Agent in Debug mode");
#endif
VirtIOSerial vio = new VirtIOSerial();
vio = new VirtIOSerial();
vio.Data += OnData;
if (vio.WriteMsg(new ProtocolMessage{
Operation = ProtocolOperation.NOP
}))
{
#if DEBUG
Console.WriteLine("Wrote NOP");
#endif
}
Thread.Sleep(-1);
}
@ -79,6 +88,20 @@ namespace CollabVMAgent
}
break;
}
case ProtocolOperation.NOP:
{
if (vio.WriteMsg(new ProtocolMessage
{
Operation = ProtocolOperation.NOP
}))
{
#if DEBUG
Console.WriteLine("Wrote NOP");
#endif
}
break;
}
}
}

View file

@ -10,6 +10,8 @@ namespace CollabVMAgent.Protocol
}
public enum ProtocolOperation : ushort
{
UploadFile = 0
UploadFile = 0,
ACK = 1,
NOP = 2,
}
}

View file

@ -1,4 +1,6 @@
using System;
using CollabVMAgent.Protocol;
using MsgPack.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@ -12,6 +14,7 @@ namespace CollabVMAgent
{
public class VirtIOSerial : IDisposable
{
MessagePackSerializer serializer = MessagePackSerializer.Get<ProtocolMessage>();
const int READ_INTERVAL = 3000;
const int COPY_BUFFER_SIZE = 4096;
static Guid GUID_VIOSERIAL_PORT = new Guid(0x6fde7521, 0x1b65, 0x48ae, 0xb6, 0x28, 0x80, 0xbe, 0x62, 0x1, 0x60, 0x26);
@ -41,6 +44,19 @@ namespace CollabVMAgent
uint wrote;
return WriteFile(viohnd, data, (uint)data.Length, out wrote, IntPtr.Zero);
}
public bool WriteMsg(ProtocolMessage msg)
{
byte[] message;
using (var ms = new MemoryStream())
{
serializer.Pack(ms, msg);
message = ms.ToArray();
}
byte[] header = BitConverter.GetBytes((UInt32)message.Length);
if (!Write(header)) return false;
return Write(message);
}
void ReadLoop()
{
@ -73,6 +89,10 @@ namespace CollabVMAgent
#if DEBUG
Console.WriteLine($"Reported payload size: {size}");
#endif
if (size == 0)
{
continue;
}
// Read the message
uint position = 0;
byte[] buf = new byte[COPY_BUFFER_SIZE];
@ -93,6 +113,11 @@ namespace CollabVMAgent
position += read;
}
byte[] payload = ms.ToArray();
if (this.WriteMsg(new ProtocolMessage
{
Operation = ProtocolOperation.ACK
}))
Console.WriteLine("Wrote ACK to serial.");
ms.Dispose();
Data.Invoke(this, new TypedEventArgs<byte[]>(payload));
}