changeset 25:52cb63c37218

update ChristieDaemon
author riono <e165729@ie.u-ryukyu.ac.jp>
date Fri, 18 Dec 2020 01:06:47 +0900
parents 58858657a0e0
children 45ff08d59fda
files daemon/AcceptThread.cs daemon/ChristieDaemon.cs
diffstat 2 files changed, 49 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/daemon/AcceptThread.cs	Tue Dec 15 22:30:06 2020 +0900
+++ b/daemon/AcceptThread.cs	Fri Dec 18 01:06:47 2020 +0900
@@ -1,6 +1,7 @@
 using System;
 using System.Net;
 using System.Net.Sockets;
+using System.Threading.Tasks;
 using Christie_net.codegear;
 
 namespace Christie_net.daemon {
@@ -17,13 +18,23 @@
     public void Run() {
         while (true) {
             try {
-                Socket socket = null;
-                socket = soc.Accept();
+                Socket socket = soc;
+                socket.Listen((int)SocketOptionName.MaxConnections);
+                socket.NoDelay = true;
                 Console.WriteLine("Accept " + socket.LocalEndPoint + ":" + ((IPEndPoint)socket.LocalEndPoint).Port);
+                
+                Connection connection = new Connection(socket, cgm);
                 string key = "accept" + counter;
                 
+                IncomingTcpConnection inComing = new IncomingTcpConnection(connection);
+                Task.Run(() => inComing.Run());
+                //cgm.SetAccept(key, in);
+
+                OutboundTcpConnection outBound = new OutboundTcpConnection(connection);
+                Task.Run(() => outBound.Run());
+                counter++;
             } catch (Exception e) {
-                
+                Console.WriteLine(e.StackTrace);
             }
         }
     }
--- a/daemon/ChristieDaemon.cs	Tue Dec 15 22:30:06 2020 +0900
+++ b/daemon/ChristieDaemon.cs	Fri Dec 18 01:06:47 2020 +0900
@@ -1,7 +1,40 @@
+using System;
+using System.IO;
+using System.Net;
+using System.Net.Sockets;
+using System.Threading.Tasks;
+using Christie_net.codegear;
+
 namespace Christie_net.daemon {
 public class ChristieDaemon {
     private int localPort;
-    //private
-    
+    private AcceptThread acceptThread;
+    public CodeGearManager cgm;
+
+    public ChristieDaemon(int localPort, CodeGearManager cgm) {
+        this.localPort = localPort;
+        this.cgm = cgm;
+    }
+
+    public void Listen() {
+        try {
+            // listen on any address ipv4/ipv6
+            IPHostEntry host = Dns.GetHostEntry("::");
+            IPAddress ipAddress = host.AddressList[0];
+            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, localPort);
+
+            Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
+            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
+            Console.WriteLine("ChristieDaemon, listen: bind to " + localEndPoint);
+            socket.Bind(localEndPoint);
+
+            acceptThread = new AcceptThread(socket, cgm);
+            Task.Run(() => acceptThread.Run());
+        } catch (IOException e) {
+            Console.WriteLine(e.StackTrace);
+        }
+        
+    }
+
 }
 }
\ No newline at end of file