How to use C# to connect to Ouster Gemini TCP output
C# Implementation of the TCP Client
The following C# code creates a TCP connection to the server, reads frames of data, decodes them as JSON, and processes them with a callback function.
using System;
using System.Net.Sockets;
using System.Text;
using System.Security.Authentication;
using System.Net.Security;
using System.IO;
using Newtonsoft.Json.Linq;
class TCPClient
{
private const string HOST = "localhost";
private const int PORT = 3302;
private const int FRAME_SIZE_B = 4;
private const string ENDIAN_TYPE = "big"; // Not used in C#, assuming big-endian
static void Main(string[] args)
{
try
{
// Create the SSL/TLS context
SslStream sslStream = CreateSslStream(HOST, PORT);
// Start reading frames from the server
ReadFrames(sslStream, ProcessData);
// Close the SSL stream and TCP connection
sslStream.Close();
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
private static SslStream CreateSslStream(string host, int port)
{
TcpClient tcpClient = new TcpClient(host, port);
SslStream sslStream = new SslStream(tcpClient.GetStream(), false, new RemoteCertificateValidationCallback((sender, certificate, chain, sslPolicyErrors) => true));
sslStream.AuthenticateAsClient(host, null, SslProtocols.Tls12, false);
return sslStream;
}
private static void ReadFrames(SslStream sslStream, Action<JObject> callbackFunction)
{
while (true)
{
// Get the size of the frame
byte[] frameSizeBuffer = new byte[FRAME_SIZE_B];
int bytesRead = sslStream.Read(frameSizeBuffer, 0, FRAME_SIZE_B);
if (bytesRead == 0)
{
return;
}
// Convert the byte data to an integer, representing the number of bytes
int frameSize = BitConverter.ToInt32(frameSizeBuffer, 0);
if (BitConverter.IsLittleEndian)
{
frameSize = System.Net.IPAddress.NetworkToHostOrder(frameSize);
}
// Read the data frame
byte[] dataBuffer = new byte[frameSize];
bytesRead = sslStream.Read(dataBuffer, 0, frameSize);
if (bytesRead == 0)
{
return;
}
// Convert the byte array to a JSON string and process it
string jsonString = Encoding.UTF8.GetString(dataBuffer, 0, bytesRead);
JObject data = JObject.Parse(jsonString);
// If the message is a heartbeat, continue to the next message
if (data.ContainsKey("heartbeat"))
{
continue;
}
// Call the callback function with the JSON data
callbackFunction(data);
}
}
private static void ProcessData(JObject data)
{
// Example callback function to print the data
Console.WriteLine(data.ToString());
}
}
Explanation of the Code
CreateSslStream Method:
This method establishes an SSL/TLS connection to the server using
TcpClientandSslStream.
ReadFrames Method:
Continuously reads data from the stream.
First, it reads the frame size (4 bytes).
Then, it reads the frame of that size and processes it as JSON.
ProcessData Method:
This is an example callback function that simply prints the JSON data. Customer can replace it with their custom logic.
Setting Up and Running the Code on Ubuntu 22.04
Step 1: Install .NET SDK
sudo apt update
sudo apt install dotnet-sdk-8.0
Step 2: Create a New C# Project
Create a new directory for your project:
mkdir -p ~/tcp_client
cd ~/tcp_clientInitialize a new C# console project:
dotnet new console -n TCPClient
cd TCPClient
Step 3: Add Newtonsoft.Json Library
Install the Newtonsoft.Json library via NuGet:
dotnet add package Newtonsoft.Json
Step 4: Replace the Default Code
Open the Program.cs file:
nano Program.csReplace the contents with the C# code provided above.
Save and close the file (CTRL + S, then Enter, and CTRL + X to exit).
Step 5: Build and Run the Application
Build the project:
dotnet buildRun the application:
dotnet run
You will see the output similar like this: