Merge commit 'b3bb7d2f34eda7dfdd0aa629dfef7d725fa6241a' as 'Runtime/HumanoidControl/Scripts/Networking/Roboid/ControlCore'

This commit is contained in:
Pascal Serrarens 2024-12-06 13:59:28 +01:00
commit 712560f610
11 changed files with 600 additions and 0 deletions

View File

@ -0,0 +1,26 @@
# You can override the included template(s) by including variable overrides
# SAST customization: https://docs.gitlab.com/ee/user/application_security/sast/#customizing-the-sast-settings
# Secret Detection customization: https://docs.gitlab.com/ee/user/application_security/secret_detection/pipeline/#customization
# Dependency Scanning customization: https://docs.gitlab.com/ee/user/application_security/dependency_scanning/#customizing-the-dependency-scanning-settings
# Container Scanning customization: https://docs.gitlab.com/ee/user/application_security/container_scanning/#customizing-the-container-scanning-settings
# Note that environment variables can be set in several places
# See https://docs.gitlab.com/ee/ci/variables/#cicd-variable-precedence
stages:
- build
- test
- deploy
- review
- dast
- staging
- canary
- production
- incremental rollout 10%
- incremental rollout 25%
- incremental rollout 50%
- incremental rollout 100%
- performance
- cleanup
sast:
stage: test
include:
- template: Auto-DevOps.gitlab-ci.yml

View File

@ -0,0 +1,35 @@
using Passer;
public class LowLevelMessages {
public static Spherical ReceiveSpherical(byte[] data, ref uint ix) {
float horizontal = ReceiveAngle8(data, ref ix);
float vertical = ReceiveAngle8(data, ref ix);
float distance = ReceiveFloat16(data, ref ix);
Spherical v = new(distance, horizontal, vertical);
return v;
}
public static Quat32 ReceiveQuat32(byte[] data, ref uint ix) {
Quat32 q = new(
(data[ix++] - 128.0F) / 127.0F,
(data[ix++] - 128.0F) / 127.0F,
(data[ix++] - 128.0F) / 127.0F,
data[ix++] / 255.0F);
return q;
}
public static float ReceiveAngle8(byte[] data, ref uint ix) {
float value = (data[ix++] * 180) / 128.0F;
return value;
}
public static float ReceiveFloat16(byte[] data, ref uint ix) {
ushort value = (ushort)(data[ix++] << 8 | data[ix++]);
float16 f16 = new();
f16.SetBinary(value);
float f = f16.toFloat();
return f;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 157cf85b523c1f648adc007539f8b736
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,365 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace Passer.Control {
public class Client {
//public ConnectionMethod connection;
public UdpClient udpClient;
public string ipAddress;
public int port;
public byte networkId;
public readonly ConcurrentQueue<IMessage> messageQueue = new();
public static Client GetClient(string ipAddress, int port) {
foreach (Client c in clients) {
if (c.ipAddress == ipAddress && c.port == port)
return c;
}
return null;
}
static public List<Client> clients = new List<Client>();
public static Client NewClient() {
Client client = new();
clients.Add(client);
client.networkId = 0;
return client;
}
public static Client NewUDPClient(UdpClient udpClient, string ipAddress, int port) {
Client client = NewClient();
client.ipAddress = null;
client.port = port;
client.udpClient = udpClient;
return client;
}
}
public class IMessage {
public IMessage() { }
public IMessage(byte[] data) {
Deserialize(data);
}
public virtual byte[] Serialize() { return null; }
public virtual void Deserialize(byte[] data) { }
public static bool SendMsg(Client client, IMessage msg) {
return SendMsg(client, msg.Serialize());
}
public static bool SendMsg(Client client, byte[] data) {
if (client == null || client.ipAddress == null)
return false;
client.udpClient.Send(data, data.Length, client.ipAddress, client.port);
return true;
}
public static async Task<byte[]> Receive(Stream dataStream, byte packetSize) {
byte[] buffer = new byte[packetSize - 1]; // without msgId
int byteCount = dataStream.Read(buffer, 0, packetSize - 1);
while (byteCount < packetSize - 1) {
// not all bytes have been read, wait and try again
await Task.Delay(1);
byteCount += dataStream.Read(buffer, byteCount, packetSize - 1 - byteCount);
}
return buffer;
}
}
#region Client
public class ClientMsg : IMessage {
public const byte length = 2;
public byte clientId;
public ClientMsg(byte[] data) : base(data) { }
public override void Deserialize(byte[] data) {
base.Deserialize(data);
uint ix = 0;
clientId = data[ix++];
}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
if (packetSize != length)
return false;
byte[] buffer = await Receive(dataStream, packetSize);
ClientMsg msg = new(buffer);
if (client.networkId == 0) {
client.networkId = (byte)(Client.clients.Count);
NetworkIdMsg.Send(client);
//if (string.IsNullOrEmpty(sceneUrl) == false)
//SendModelUrl(client, sceneUrl);
}
else if (msg.clientId == 0) {
NetworkIdMsg.Send(client);
//if (string.IsNullOrEmpty(sceneUrl) == false)
//SendModelUrl(client, sceneUrl);
}
return true;
}
}
#endregion Client
#region Network Id
public class NetworkIdMsg : IMessage {
public const byte Id = 0xA1;
public const byte length = 2;
public static bool Send(Client client) {
byte[] data = new byte[NetworkIdMsg.length];
data[0] = NetworkIdMsg.Id;
data[1] = client.networkId;
return SendMsg(client, data);
}
}
#endregion Network Id
#region Thing
public class ThingMsg : IMessage {
public const byte length = 4;
public const byte Id = 0x80;
public byte objectId;
public byte objectType;
public byte parentId;
public ThingMsg(byte[] data) : base(data) { }
public override void Deserialize(byte[] data) {
uint ix = 0;
objectId = data[ix++];
objectType = data[ix++];
parentId = data[ix];
}
public static bool Send(Client client, byte networkId, byte thingId, byte thingType) {
byte[] data = new byte[4];
data[0] = ThingMsg.Id;
data[1] = networkId;
data[2] = thingId;
data[3] = thingType;
data[4] = 0x00; // parent not supported yet
return SendMsg(client, data);
}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
if (packetSize != length)
return false;
byte[] buffer = await Receive(dataStream, packetSize);
ThingMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion Thing
#region Name
public class NameMsg : IMessage {
public byte networkId = 0;
public byte objectId;
public byte len;
public string name;
public NameMsg(byte[] data) : base(data) { }
public override void Deserialize(byte[] data) {
uint ix = 0;
this.objectId = data[ix++];
int strlen = data[ix++];
this.name = System.Text.Encoding.UTF8.GetString(data, (int)ix, strlen);
}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
byte[] buffer = await Receive(dataStream, packetSize);
NameMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion
#region Model URL
public class ModelUrlMsg : IMessage {
public const byte Id = 0x90; // (144) Model URL
public byte objectId;
public Spherical position;
public float scale;
public string url;
public ModelUrlMsg(string url, float scale = 1) {
this.url = url;
this.scale = scale;
this.position = Spherical.zero;
}
public ModelUrlMsg(byte[] data) : base(data) { }
public override byte[] Serialize() {
byte[] data = new byte[this.url.Length + 9];
data[0] = ModelUrlMsg.Id;
data[1] = 0x00; // Thing Id
// data[2]..[5] == position 0, 0, 0
data[6] = 0x3C; // Dummy float16 value 1
data[7] = 0x00;
data[8] = (byte)url.Length;
for (int ix = 0; ix < this.url.Length; ix++)
data[9 + ix] = (byte)url[ix];
return data;
}
public override void Deserialize(byte[] data) {
uint ix = 0;
this.objectId = data[ix++];
this.position = LowLevelMessages.ReceiveSpherical(data, ref ix);
this.scale = LowLevelMessages.ReceiveFloat16(data, ref ix);
int strlen = data[ix++];
url = System.Text.Encoding.UTF8.GetString(data, (int)ix, strlen);
}
public static bool Send(Client client, string modelUrl) {
ModelUrlMsg msg = new(modelUrl);
return SendMsg(client, msg);
}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
byte[] buffer = await Receive(dataStream, packetSize);
ModelUrlMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion Model URL
#region Pose
public class PoseMsg : IMessage {
public const byte length = 3 + 4 + 4;
public byte thingId;
public byte poseType;
public Spherical position;
public Quat32 orientation;
public PoseMsg(byte[] data) : base(data) { }
public override void Deserialize(byte[] data) {
uint ix = 0;
thingId = data[ix++];
poseType = data[ix++];
//if ((poseType & Pose_Position) != 0)
position = LowLevelMessages.ReceiveSpherical(data, ref ix);
//if ((poseType & Pose_Orientation) != 0) {
orientation = LowLevelMessages.ReceiveQuat32(data, ref ix);
}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
if (packetSize != length)
return false;
byte[] buffer = await Receive(dataStream, packetSize);
PoseMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion Pose
#region Bytes
public class BytesMsg : IMessage {
public const byte Id = 0xB1;
public byte networkId;
public byte thingId;
public byte[] bytes;
public BytesMsg(byte[] data) : base(data) { }
public BytesMsg(byte networkId, byte thingId, byte[] bytes) : base() {
this.networkId = networkId;
this.thingId = thingId;
this.bytes = bytes;
}
public override byte[] Serialize() {
byte[] buffer = new byte[4 + this.bytes.Length];
int ix = 0;
buffer[ix++] = BytesMsg.Id;
buffer[ix++] = this.networkId;
buffer[ix++] = this.thingId;
buffer[ix++] = (byte)bytes.Length;
foreach (byte b in bytes)
buffer[ix++] = b;
return buffer;
}
public override void Deserialize(byte[] data) {
//this.bytes = data;
uint ix = 0;
this.thingId = data[ix++];
this.bytes = new byte[data.Length - ix];
for (uint bytesIx = 0; ix < data.Length; ix++, bytesIx++)
this.bytes[bytesIx] = data[ix];
}
public static void Send(Client client, byte thingId, byte[] bytes) {
BytesMsg msg = new(client.networkId, thingId, bytes);
SendMsg(client, msg);
}
// received bytes
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
byte[] buffer = await Receive(dataStream, packetSize);
BytesMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion Bytes
#region Destroy
public class DestroyMsg : IMessage {
public const byte length = 2;
public byte objectId;
public DestroyMsg(byte[] data) : base(data) { }
public override void Deserialize(byte[] data) {
objectId = data[0];
}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
if (packetSize != length)
return false;
byte[] buffer = await Receive(dataStream, packetSize);
DestroyMsg msg = new(buffer);
client.messageQueue.Enqueue(msg);
return true;
}
}
#endregion Destroy
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d3f26f22a5422a44997b39a1844ab2ad
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,15 @@
namespace Passer {
public class Quat32 {
public float x;
public float y;
public float z;
public float w;
public Quat32(float x, float y, float z, float w) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 96aa3cf18eaeb574d9265704d68000da
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,93 @@
# ControlCore
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin http://gitlab.passervr.com/passer/csharp/controlcore.git
git branch -M main
git push -uf origin main
```
## Integrate with your tools
- [ ] [Set up project integrations](http://gitlab.passervr.com/passer/csharp/controlcore/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 3b79b5b373e9ced4abe72eb4d2f83c6a
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,15 @@
namespace Passer {
public class Spherical {
public float distance;
public float horizontal;
public float vertical;
public static Spherical zero = new(0, 0, 0);
public Spherical(float distance, float horizontal, float vertical) {
this.distance = distance;
this.horizontal = horizontal;
this.vertical = vertical;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 33f05ee51ff6f7042b3e22723bcfc4f5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: