Merging, and adding test project

This commit is contained in:
Taha authored and John Colvin committed 2023-09-30 12:52:37 -04:00
1 parent 9311c27f47
commit 404698152e
13 files changed
+717 -110

No files matched your search

@@ -0,0 +1,103 @@
using BCG;
using Google.Protobuf;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using BCG;
using System.Xml.Serialization;
using System.Reflection;
using System.Data;
namespace BCG {
public class SimpleTCPServerAsync : ITCPConnectionHandler, ITCPMessageHandler {
private AsyncTCPConnection2 _conn = null;
int _n_msgs_received = 0;
SingleServerToClientManagedTCPConnectionAcceptor _acceptor = new SingleServerToClientManagedTCPConnectionAcceptor();
public static async Task start() {
Console.WriteLine("SimpleTCPServer.start accepting socket connections");
SimpleTCPServerAsync s = new SimpleTCPServerAsync();
//await AsyncTCPConnection2.accept_connections("127.0.0.1", 12345, s, TimeSpan.FromMilliseconds(100));
s.accept();
}
// public static async Task init()
// {
// }
public void accept() {
_acceptor.accept_on_port(this, "127.0.0.1", 12345, TimeSpan.Zero);
}
public void onConnection(ITCPConnection connection) {
Console.WriteLine("SimpleTCPServerAsync.onConnection new client connected");
_conn = connection as AsyncTCPConnection2;
connection.register(this);
}
public void onData(RemoteMessage msg) {
_n_msgs_received++;
Console.WriteLine($"SimpleTCPServerAsync.onData msgs_received: {_n_msgs_received} msg: {msg} , echoing it back");
if (msg.messageType == RemoteMessageType.data && _conn != null) {
//_conn.send(msg.message);
Task.Run(async () => {
Console.WriteLine($"SimpleTCPServerAsync.onData sending msg: {msg} to client");
await _conn.send_async(msg.message);
});
}
}
public bool isDisposed { get { return false; } }
public void Dispose() { }
}
public class SimpleTCPServer : ITCPConnectionHandler, ITCPMessageHandler {
private AsyncTCPConnection2 _conn = null;
private int _n_msgs_received = 0;
SingleServerToClientManagedTCPConnectionAcceptor _acceptor = new SingleServerToClientManagedTCPConnectionAcceptor();
public static async Task start() {
Console.WriteLine("SimpleTCPServer.start accepting socket connections");
SimpleTCPServer s = new SimpleTCPServer();
s.accept();
//await AsyncTCPConnection2.accept_connections("127.0.0.1", 12345, s, TimeSpan.FromMilliseconds(100));
}
public void accept() {
_acceptor.accept_on_port(this, "127.0.0.1", 12345, TimeSpan.Zero);
}
public void onConnection(ITCPConnection connection) {
Console.WriteLine("SimpleTCPServerAsync.onConnection new client connected");
_conn = connection as AsyncTCPConnection2;
connection.register(this);
}
public async void onData(RemoteMessage msg) {
_n_msgs_received++;
Console.WriteLine($"SimpleTCPServerAsync.onData msg: {msg} , msgs received: {_n_msgs_received} echoing it back");
if (msg.messageType == RemoteMessageType.data && _conn != null) {
Task.Run(async () => {
//Console.WriteLine($"SimpleTCPServerAsync.onData sending msg: {msg} to client");
await _conn.send_async(msg.message);
});
}
}
public bool isDisposed { get { return false; } }
public void Dispose() { }
}
}