Dispose connections in SimpleTCPServer.Dispose
This commit is contained in:
1 parent
aaa3ff114b
commit
158dfdd606
1 file changed
+63
-62
@@ -1,62 +1,63 @@
|
||||
using BCG;
|
||||
|
||||
using Google.Protobuf;
|
||||
|
||||
using System;
|
||||
using BCG;
|
||||
using System.Collections.Generic;
|
||||
using bts;
|
||||
|
||||
namespace BCG {
|
||||
|
||||
public class SimpleTCPServer : ITCPConnectionHandler, ITCPMessageHandler {
|
||||
int _n_msgs_received = 0;
|
||||
AsyncTCPConnectionAcceptor_APM _acceptor;
|
||||
IDictionary<RemoteConnectionInfo, ITCPConnection> _conn_map = new Dictionary<RemoteConnectionInfo, ITCPConnection>();
|
||||
|
||||
public SimpleTCPServer(int port) {
|
||||
_acceptor = new AsyncTCPConnectionAcceptor_APM(port, this);
|
||||
}
|
||||
|
||||
public void init() {
|
||||
//_acceptor = new AsyncTCPConnectionAcceptor_APM(12345, this);
|
||||
_acceptor.init();
|
||||
}
|
||||
|
||||
|
||||
public void onConnection(ITCPConnection connection) {
|
||||
string ip; int port;
|
||||
connection.getPeerAddress(out ip, out port);
|
||||
RemoteConnectionInfo rci = new RemoteConnectionInfo(ip, port);
|
||||
_conn_map[rci] = connection;
|
||||
bts.Log.Gen.info($"SimpleTCPServer.onConnection new client connected conn: {rci}");
|
||||
connection.register(this);
|
||||
}
|
||||
|
||||
|
||||
public void onData(RemoteMessage msg) {
|
||||
Console.WriteLine($"SimpleTCPServer.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($"\n\nSimpleTCPServerAsync.onData msgs_received: {_n_msgs_received} msg: {msg} ci: {rci} NOW echoing it back\n\n");
|
||||
if (msg.messageType == RemoteMessageType.Data) {
|
||||
this_conn.send(msg.message);
|
||||
} else {
|
||||
if (msg.messageType == RemoteMessageType.Disconnection) {
|
||||
var ci = msg.remoteConnectionInfo;
|
||||
_conn_map.Remove(ci);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool isDisposed { get { return false; } }
|
||||
|
||||
public void Dispose() { }
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using bts;
|
||||
using System.Threading;
|
||||
|
||||
namespace BCG {
|
||||
|
||||
public class SimpleTCPServer : ITCPConnectionHandler, ITCPMessageHandler {
|
||||
int _n_msgs_received = 0;
|
||||
bool _isDisposed = false;
|
||||
AsyncTCPConnectionAcceptor_APM _acceptor;
|
||||
IDictionary<RemoteConnectionInfo, ITCPConnection> _conn_map = new Dictionary<RemoteConnectionInfo, ITCPConnection>();
|
||||
|
||||
public SimpleTCPServer(int port) {
|
||||
_acceptor = new AsyncTCPConnectionAcceptor_APM(port, this);
|
||||
}
|
||||
|
||||
public void init() {
|
||||
//_acceptor = new AsyncTCPConnectionAcceptor_APM(12345, this);
|
||||
_acceptor.init();
|
||||
}
|
||||
|
||||
|
||||
public void onConnection(ITCPConnection connection) {
|
||||
string ip; int port;
|
||||
connection.getPeerAddress(out ip, out port);
|
||||
RemoteConnectionInfo rci = new RemoteConnectionInfo(ip, port);
|
||||
_conn_map[rci] = connection;
|
||||
bts.Log.Gen.info($"SimpleTCPServer.onConnection new client connected conn: {rci}");
|
||||
connection.register(this);
|
||||
}
|
||||
|
||||
|
||||
public void onData(RemoteMessage msg) {
|
||||
Console.WriteLine($"SimpleTCPServer.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($"\n\nSimpleTCPServerAsync.onData msgs_received: {_n_msgs_received} msg: {msg} ci: {rci} NOW echoing it back\n\n");
|
||||
if (msg.messageType == RemoteMessageType.Data) {
|
||||
this_conn.send(msg.message);
|
||||
} else {
|
||||
if (msg.messageType == RemoteMessageType.Disconnection) {
|
||||
var ci = msg.remoteConnectionInfo;
|
||||
_conn_map.Remove(ci);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool isDisposed => _isDisposed;
|
||||
|
||||
public void Dispose() {
|
||||
_conn_map.Values.DisposeAll();
|
||||
Volatile.Write(ref _isDisposed, true);
|
||||
_conn_map.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user