changeset 33:7575980bffc9

update
author riono <e165729@ie.u-ryukyu.ac.jp>
date Tue, 20 Apr 2021 18:42:17 +0900
parents 3a7a71ee8738
children 1236da135f79
files Christie_net.csproj Test/Example/FizzBuzz/Counter.cs Test/Example/FizzBuzz/FizzBuzz.cs Test/Example/FizzBuzz/StartFizzBuzz.cs Test/RewritingTest/SocketListenerThread.cs Test/RewritingTest/TCPClient.cs Test/RewritingTest/TCPClientTest.cs Test/RewritingTest/TCPListener.cs Test/RewritingTest/TCPListenerTest.cs codegear/CodeGearManager.cs daemon/AcceptThread.cs daemon/ChristieDaemon.cs daemon/Connection.cs datagear/RemoteDataGearManager.cs
diffstat 14 files changed, 188 insertions(+), 142 deletions(-) [+]
line wrap: on
line diff
--- 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 @@
     <PropertyGroup>
         <OutputType>Exe</OutputType>
         <TargetFramework>netcoreapp3.1</TargetFramework>
-        <StartupObject>TCPListener</StartupObject>
+        <StartupObject>Christie_net.Test.Example.FizzBuzz.StartFizzBuzz</StartupObject>
     </PropertyGroup>
 
     <ItemGroup>
--- 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());
--- 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) {
--- 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;
 
--- 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();
         
     }
--- 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
--- /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
--- 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
--- /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
--- 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;
--- 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
--- 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);
         }
         
--- 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 @@
     /// </summary>
     /// <returns></returns>
     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;
+       
     }
 
     /// <summary>
--- 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<object> dg) {