# HG changeset patch # User riono # Date 1618911737 -32400 # Node ID 7575980bffc9f6c026d1c0c4675db5a5ceeac057 # Parent 3a7a71ee8738adcd9ab3f9e94ff5c57d7dbdbaed update diff -r 3a7a71ee8738 -r 7575980bffc9 Christie_net.csproj --- a/Christie_net.csproj Fri Apr 16 01:32:08 2021 +0900 +++ b/Christie_net.csproj Tue Apr 20 18:42:17 2021 +0900 @@ -3,7 +3,7 @@ Exe netcoreapp3.1 - TCPListener + Christie_net.Test.Example.FizzBuzz.StartFizzBuzz diff -r 3a7a71ee8738 -r 7575980bffc9 Test/Example/FizzBuzz/Counter.cs --- a/Test/Example/FizzBuzz/Counter.cs Fri Apr 16 01:32:08 2021 +0900 +++ b/Test/Example/FizzBuzz/Counter.cs Tue Apr 20 18:42:17 2021 +0900 @@ -1,3 +1,4 @@ +using System; using Christie_net.annotation; using Christie_net.codegear; @@ -7,6 +8,7 @@ public override void Run(CodeGearManager cgm) { if (num <= 100) { + Console.WriteLine("call"); GetDgm("FizzBuzz").Put("num", num); cgm.GetLocalDGM().Put("num", num+1); cgm.Setup(new Counter()); diff -r 3a7a71ee8738 -r 7575980bffc9 Test/Example/FizzBuzz/FizzBuzz.cs --- a/Test/Example/FizzBuzz/FizzBuzz.cs Fri Apr 16 01:32:08 2021 +0900 +++ b/Test/Example/FizzBuzz/FizzBuzz.cs Tue Apr 20 18:42:17 2021 +0900 @@ -7,6 +7,7 @@ [Take] private int num; public override void Run(CodeGearManager cgm) { + Console.WriteLine("call fizi"); if (num % 3 == 0 && num % 5 == 0) { Console.WriteLine(num + ":FizzBuzz"); }else if (num % 3 == 0) { diff -r 3a7a71ee8738 -r 7575980bffc9 Test/Example/FizzBuzz/StartFizzBuzz.cs --- a/Test/Example/FizzBuzz/StartFizzBuzz.cs Fri Apr 16 01:32:08 2021 +0900 +++ b/Test/Example/FizzBuzz/StartFizzBuzz.cs Tue Apr 20 18:42:17 2021 +0900 @@ -1,3 +1,4 @@ +using System; using Christie_net.annotation; using Christie_net.codegear; diff -r 3a7a71ee8738 -r 7575980bffc9 Test/RewritingTest/SocketListenerThread.cs --- a/Test/RewritingTest/SocketListenerThread.cs Fri Apr 16 01:32:08 2021 +0900 +++ b/Test/RewritingTest/SocketListenerThread.cs Tue Apr 20 18:42:17 2021 +0900 @@ -12,7 +12,7 @@ } public void Run () { - Thread thread = new Thread (new ThreadStart (MethodThread)); + Thread thread = new Thread (MethodThread); thread.Start(); } diff -r 3a7a71ee8738 -r 7575980bffc9 Test/RewritingTest/TCPClient.cs --- a/Test/RewritingTest/TCPClient.cs Fri Apr 16 01:32:08 2021 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ -using System; -using System.Net.Sockets; -using System.Text; - -namespace Christie_net { -public class TCPClient { - public static void Main() { - TcpClient client = null; - - client = new TcpClient("127.0.0.1", 11000); - - Console.WriteLine("connect!"); - - NetworkStream stream = client.GetStream(); - - Byte[] data = Encoding.ASCII.GetBytes("HelloWorld"); - stream.Write(data, 0, data.Length); - Console.WriteLine("Sent"); - - data = new Byte[256]; - - Int32 bytes = stream.Read(data, 0, data.Length); - string receiveData = Encoding.ASCII.GetString(data, 0, bytes); - Console.WriteLine("received:" + receiveData); - - stream.Close(); - client.Close(); - - } -} -} \ No newline at end of file diff -r 3a7a71ee8738 -r 7575980bffc9 Test/RewritingTest/TCPClientTest.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test/RewritingTest/TCPClientTest.cs Tue Apr 20 18:42:17 2021 +0900 @@ -0,0 +1,31 @@ +using System; +using System.Net.Sockets; +using System.Text; + +namespace Christie_net { +public class TCPClientTest { + public static void Main() { + TcpClient client = null; + + client = new TcpClient("127.0.0.1", 11000); + + Console.WriteLine("connect!"); + + NetworkStream stream = client.GetStream(); + + Byte[] data = Encoding.ASCII.GetBytes("HelloWorld"); + stream.Write(data, 0, data.Length); + Console.WriteLine("Sent"); + + data = new Byte[256]; + + Int32 bytes = stream.Read(data, 0, data.Length); + string receiveData = Encoding.ASCII.GetString(data, 0, bytes); + Console.WriteLine("received:" + receiveData); + + stream.Close(); + client.Close(); + + } +} +} \ No newline at end of file diff -r 3a7a71ee8738 -r 7575980bffc9 Test/RewritingTest/TCPListener.cs --- a/Test/RewritingTest/TCPListener.cs Fri Apr 16 01:32:08 2021 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,58 +0,0 @@ -using System; -using System.Net; -using System.Net.Sockets; - -public class TCPListener { - - public static void Main() { - TcpListener server = null; - try { - IPAddress localAddress = IPAddress.Parse("127.0.0.1"); - - server = new TcpListener(IPAddress.IPv6Any, 11000); - // ipv4/v6対応 - server.Server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0); - - // 開始 - server.Start(); - - string data = null; - Byte[] bytes = new byte[256]; - - while (true) { - Console.Write("Waiting for a connection... "); - - TcpClient client = server.AcceptTcpClient(); - - data = null; - - NetworkStream stream = client.GetStream(); - - int i; - - // Loop to receive all the data sent by the client. - while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { - // Translate data bytes to a ASCII string. - data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); - Console.WriteLine("Received: {0}", data); - - // Process the data sent by the client. - data = data.ToUpper(); - - byte[] msg = System.Text.Encoding.ASCII.GetBytes(data); - - // Send back a response. - stream.Write(msg, 0, msg.Length); - Console.WriteLine("Sent: {0}", data); - } - - // Shutdown and end connection - client.Close(); - } - } catch (SocketException e) { - Console.WriteLine("SocketException: {0}", e); - } finally { - server.Stop(); - } - } -} \ No newline at end of file diff -r 3a7a71ee8738 -r 7575980bffc9 Test/RewritingTest/TCPListenerTest.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test/RewritingTest/TCPListenerTest.cs Tue Apr 20 18:42:17 2021 +0900 @@ -0,0 +1,86 @@ +using System; +using System.Net; +using System.Net.Sockets; +using System.Threading; +using System.Threading.Tasks; + +public class TCPListenerTest { + + public static void Main() { + TCPListenerTest listenerTest = new TCPListenerTest(); + + TcpListener listener = new TcpListener(IPAddress.IPv6Any, 11000); + listener.Server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0); + + // 開始 + listener.Start(); + + Thread thread = new Thread ( () => listenerTest.TCPListenerFunc(listener)); + thread.Name = "test"; + thread.Start(); + } + + void SocketInTask(TCPListenerTest listenerTest) { + // Thread thread = new Thread (listenerTest.TCPListenerFunc); + // thread.Start(); + } + + + public void TCPListenerFunc(TcpListener server) { + //TcpListener server = null; + try { + // IPAddress localAddress = IPAddress.Parse("127.0.0.1"); + // + // server = new TcpListener(IPAddress.IPv6Any, 11000); + // // ipv4/v6対応 + // server.Server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0); + // + // // 開始 + // server.Start(); + + string data = null; + Byte[] bytes = new byte[256]; + + while (true) { + Console.Write("Waiting for a connection... "); + + TcpClient client = server.AcceptTcpClient(); + client.NoDelay = true; + + IPEndPoint endPoint = (IPEndPoint) client.Client.RemoteEndPoint; + IPAddress ipAddress = endPoint.Address; + IPHostEntry hostEntry = Dns.GetHostEntry(ipAddress); + Console.WriteLine("host:" + hostEntry.HostName + " port:" + endPoint.Port); + + data = null; + + NetworkStream stream = client.GetStream(); + + int i; + + // Loop to receive all the data sent by the client. + while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { + // Translate data bytes to a ASCII string. + data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); + Console.WriteLine("Received: {0}", data); + + // Process the data sent by the client. + data = data.ToUpper(); + + byte[] msg = System.Text.Encoding.ASCII.GetBytes(data); + + // Send back a response. + stream.Write(msg, 0, msg.Length); + Console.WriteLine("Sent: {0}", data); + } + + // Shutdown and end connection + client.Close(); + } + } catch (SocketException e) { + Console.WriteLine("SocketException: {0}", e); + } finally { + server.Stop(); + } + } +} \ No newline at end of file diff -r 3a7a71ee8738 -r 7575980bffc9 codegear/CodeGearManager.cs --- a/codegear/CodeGearManager.cs Fri Apr 16 01:32:08 2021 +0900 +++ b/codegear/CodeGearManager.cs Tue Apr 20 18:42:17 2021 +0900 @@ -40,7 +40,7 @@ } public RemoteDataGearManager CreateRemoteDGM(string dgmName, string address, int port) { - RemoteDataGearManager remote = new RemoteDataGearManager(address, port, this); + RemoteDataGearManager remote = new RemoteDataGearManager(dgmName ,address, port, this); dgmList.TryAdd(dgmName, remote); return remote; diff -r 3a7a71ee8738 -r 7575980bffc9 daemon/AcceptThread.cs --- a/daemon/AcceptThread.cs Fri Apr 16 01:32:08 2021 +0900 +++ b/daemon/AcceptThread.cs Tue Apr 20 18:42:17 2021 +0900 @@ -1,47 +1,52 @@ using System; using System.Net; using System.Net.Sockets; -using System.Threading.Tasks; +using System.Threading; using Christie_net.codegear; namespace Christie_net.daemon { public class AcceptThread { - private Socket soc; + private TcpListener listener; public int counter = 0; public CodeGearManager cgm; - public AcceptThread(Socket soc, CodeGearManager cgm) { - this.soc = soc; + public AcceptThread(TcpListener listener, CodeGearManager cgm) { + this.listener = listener; this.cgm = cgm; } public void Run() { - while (true) { + //while (true) { try { - Socket socket = null; - socket = soc.Accept(); - socket.NoDelay = true; - Console.WriteLine("Accept " + socket.LocalEndPoint + ":" + ((IPEndPoint)socket.LocalEndPoint).Port); - Connection connection = new Connection(socket, cgm); + TcpClient client = null; + client = listener.AcceptTcpClient(); + client.NoDelay = true; + + IPEndPoint endPoint = (IPEndPoint) client.Client.RemoteEndPoint; + IPAddress ipAddress = endPoint.Address; + IPHostEntry hostEntry = Dns.GetHostEntry(ipAddress); + Console.WriteLine("Accept " + hostEntry.HostName + ":" + endPoint.Port); + + Connection connection = new Connection(client.Client, cgm); + Console.WriteLine("connect@" + connection.GetInfoString()); string key = "accept" + counter; IncomingTcpConnection incoming = new IncomingTcpConnection(connection); - Task.Factory.StartNew( - () => incoming.Run(), - TaskCreationOptions.LongRunning); + Thread incomingThread = new Thread(incoming.Run); + incomingThread.Name = connection.GetInfoString() + "-IncomingTcp"; + incomingThread.Start(); + cgm.SetAccept(key, incoming); OutboundTcpConnection outbound = new OutboundTcpConnection(connection); - Task.Factory.StartNew( - () => outbound.Run(), - TaskCreationOptions.LongRunning); + Thread outboundThread = new Thread(outbound.Run); + outboundThread.Name = connection.GetInfoString() + "-OutboundTcp"; + outboundThread.Start(); counter++; - - Console.WriteLine("fin"); } catch (Exception e) { Console.WriteLine(e.StackTrace); } - } + //} } } } \ No newline at end of file diff -r 3a7a71ee8738 -r 7575980bffc9 daemon/ChristieDaemon.cs --- a/daemon/ChristieDaemon.cs Fri Apr 16 01:32:08 2021 +0900 +++ b/daemon/ChristieDaemon.cs Tue Apr 20 18:42:17 2021 +0900 @@ -2,6 +2,7 @@ using System.IO; using System.Net; using System.Net.Sockets; +using System.Threading; using System.Threading.Tasks; using Christie_net.codegear; @@ -18,27 +19,19 @@ public void Listen() { try { + TcpListener listener = new TcpListener(IPAddress.IPv6Any, localPort); // listen on any address ipv4/ipv6 - - IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, localPort); - - Socket socket = new Socket(AddressFamily.InterNetwork , SocketType.Stream, ProtocolType.Tcp); - socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); - Console.WriteLine("ChristieDaemon, listen: bind to " + localEndPoint); - socket.Bind(localEndPoint); - socket.Listen((int)SocketOptionName.MaxConnections); - - // Socket ss = null; - // ss = socket.Accept(); - // ss.NoDelay = true; - // Console.WriteLine("Accept " + ss.LocalEndPoint + ":" + ((IPEndPoint)ss.LocalEndPoint).Port); + listener.Server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0); + listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); + + Console.WriteLine("ChristieDaemon, listen: bind to " + localPort); + listener.Start(); - - acceptThread = new AcceptThread(socket, cgm); - Task.Factory.StartNew( - () => acceptThread.Run(), - TaskCreationOptions.LongRunning); - } catch (IOException e) { + acceptThread = new AcceptThread(listener, cgm); + Thread thread = new Thread(acceptThread.Run); + thread.Name = ("ACCEPT" + localPort); + thread.Start(); + } catch (Exception e) { Console.WriteLine(e.StackTrace); } diff -r 3a7a71ee8738 -r 7575980bffc9 daemon/Connection.cs --- a/daemon/Connection.cs Fri Apr 16 01:32:08 2021 +0900 +++ b/daemon/Connection.cs Tue Apr 20 18:42:17 2021 +0900 @@ -29,8 +29,12 @@ /// /// public string GetInfoString() { - return Dns.GetHostEntry(((IPEndPoint) socket.LocalEndPoint).Address.ToString()).HostName + ":" + - ((IPEndPoint) socket.LocalEndPoint).Port; + IPEndPoint endPoint = (IPEndPoint) socket.RemoteEndPoint; + IPAddress ipAddress = endPoint.Address; + IPHostEntry hostEntry = Dns.GetHostEntry(ipAddress); + // Dns.GetHostEntry(((IPEndPoint) socket.RemoteEndPoint).Address.ToString()).HostName + ":" +((IPEndPoint) socket.RemoteEndPoint).Port; + return hostEntry.HostName + ":" + endPoint.Port; + } /// diff -r 3a7a71ee8738 -r 7575980bffc9 datagear/RemoteDataGearManager.cs --- a/datagear/RemoteDataGearManager.cs Fri Apr 16 01:32:08 2021 +0900 +++ b/datagear/RemoteDataGearManager.cs Tue Apr 20 18:42:17 2021 +0900 @@ -16,22 +16,22 @@ private bool connect = false; private object syncObj = new object(); - public RemoteDataGearManager (string address, int port, CodeGearManager cgm) { + public RemoteDataGearManager (string dgmname ,string address, int port, CodeGearManager cgm) { this.cgm = cgm; RemoteDataGearManager manager = this; - Task.Factory.StartNew(() => { + Thread thread = new Thread(() => { do { try { IPHostEntry host = Dns.GetHostEntry(address); IPAddress ipAddress = host.AddressList[0]; IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, port); Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); - socket.Bind(remoteEndPoint); - socket.Listen((int) SocketOptionName.MaxConnections); + socket.Connect(remoteEndPoint); socket.NoDelay = true; Socket listener = socket.Accept(); connection = new Connection(listener, cgm); + connection.name = dgmname; lock (syncObj) { connect = true; @@ -42,14 +42,23 @@ IncomingTcpConnection incoming = new IncomingTcpConnection(connection); incoming.SetManager(manager); - Task.Factory.StartNew( - () => incoming.Run(), - TaskCreationOptions.LongRunning); + Thread incomingThread = new Thread(incoming.Run); + incomingThread.Name = dgmname + "-IncomingTcp"; + incomingThread.Priority = ThreadPriority.Highest; + incomingThread.Start(); + OutboundTcpConnection outbound = new OutboundTcpConnection(connection); - Task.Factory.StartNew( - () => outbound.Run(), - TaskCreationOptions.LongRunning); + Thread outboundThread = new Thread(outbound.Run); + outboundThread.Name = dgmname + "-OutboundTcp"; + outboundThread.Priority = ThreadPriority.Highest; + outboundThread.Start(); + // Task.Factory.StartNew( + // () => outbound.Run(), + // TaskCreationOptions.LongRunning); + }); + thread.Name = "Connect-" + dgmname; + thread.Start(); } public override void Put(string key, object data) { @@ -61,6 +70,8 @@ if (!connect) { ConnectWait(); } + + connection.Write(cm); } public override void RunCommand(Command cm) { @@ -78,6 +89,7 @@ Command remoteCmd = new CommandBuilder().Init(type).FromDgmName(connection.name).Key(cm.key) .Clazz(cm.clazz).Connection(connection).Build(); + connection.Write(remoteCmd); } public override void ResolveWaitCommand(string key, DataGear dg) {