PubSub is working now
This commit is contained in:
1 parent
3fa37397d9
commit
7bb7f1a5e2
27 files changed
+1108
-1124
No files matched your search
@@ -0,0 +1,123 @@
|
||||
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;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
|
||||
public class SimpleTCPServerAsync : ITCPConnectionHandler, ITCPMessageHandler
|
||||
{
|
||||
int _n_msgs_received = 0;
|
||||
SingleServerToClientManagedTCPConnectionAcceptor _acceptor = new SingleServerToClientManagedTCPConnectionAcceptor();
|
||||
IDictionary<RemoteConnectionInfo, ITCPConnection> _conn_map = new Dictionary<RemoteConnectionInfo, ITCPConnection>();
|
||||
|
||||
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)
|
||||
{
|
||||
AsyncTCPConnection2 new_conn = connection as AsyncTCPConnection2;
|
||||
RemoteConnectionInfo rci = new_conn.get_peer_address();
|
||||
_conn_map.Add(rci, new_conn);
|
||||
Console.WriteLine($"SimpleTCPServerAsync.onConnection new client connected conn: {rci}");
|
||||
new_conn.register(this);
|
||||
}
|
||||
|
||||
|
||||
public void onData(RemoteMessage msg)
|
||||
{
|
||||
Console.WriteLine($"SimpleTCPServerAsync.onData msg: {msg}");
|
||||
RemoteConnectionInfo rci = msg.remoteConnectionInfo;
|
||||
ITCPConnection this_conn;
|
||||
bool found = _conn_map.TryGetValue(rci, out this_conn);
|
||||
if (!found)
|
||||
{
|
||||
Console.WriteLine("Could not find connection !!!!!!");
|
||||
return;
|
||||
}
|
||||
_n_msgs_received++;
|
||||
Console.WriteLine($"SimpleTCPServerAsync.onData msgs_received: {_n_msgs_received} msg: {msg} ci: {rci} echoing it back");
|
||||
if (msg.messageType == RemoteMessageType.Data)
|
||||
{
|
||||
|
||||
this_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);
|
||||
// _conn.send(msg.message);
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
|
||||
// public bool isDisposed { get { return false; } }
|
||||
|
||||
// public void Dispose() { }
|
||||
// }
|
||||
}
|
||||
Reference in new issue
Block a user