-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
77 lines (67 loc) · 2.33 KB
/
Copy pathProgram.cs
File metadata and controls
77 lines (67 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Threading.Tasks;
using CycloneDDS.Core;
using CycloneDDS.Runtime;
using CycloneDDS.Schema;
namespace HelloWorld
{
[DdsTopic("HelloWorldTopic")]
public partial struct HelloWorldMessage
{
[DdsKey]
public int Id;
[DdsManaged]
public string Message;
}
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Starting CycloneDDS.NET Hello World...");
// Create a participant
using var participant = new DdsParticipant();
// Create a wrapper for topic registration handled internally by Writer/Reader
// Create a writer - topic name "HelloWorldTopic" automatically used from [DdsTopic] attribute
using var writer = new DdsWriter<HelloWorldMessage>(participant);
// Create a reader - topic name "HelloWorldTopic" automatically used from [DdsTopic] attribute
using var reader = new DdsReader<HelloWorldMessage>(participant);
// Local helper to read synchronously
void ReadSamples()
{
using var samples = reader.Read();
foreach (var sample in samples)
{
Console.WriteLine($"Received: [{sample.Data.Id}] {sample.Data.Message}");
}
}
// Task to write data
var writeTask = Task.Run(async () =>
{
for (int i = 0; i < 10; i++)
{
await Task.Delay(500);
var msg = new HelloWorldMessage { Id = i, Message = $"Hello World {i}" };
Console.WriteLine($"Writing: {msg.Message}");
writer.Write(msg);
}
});
// Read data
Console.WriteLine("Waiting for data...");
for (int i = 0; i < 20; i++)
{
try
{
// Simple reading loop
await Task.Delay(250);
ReadSamples();
}
catch (Exception ex)
{
Console.WriteLine($"Read error: {ex.Message}");
}
}
await writeTask;
Console.WriteLine("Done.");
}
}
}