changeset 23:46cfeb0609c5

Add TcpConnections
author riono <e165729@ie.u-ryukyu.ac.jp>
date Tue, 15 Dec 2020 22:11:40 +0900
parents 970c7f587126
children 58858657a0e0
files Test/RewritingTest/EnumToInt.cs Test/RewritingTest/SocketListenerTask.cs daemon/Connection.cs daemon/IncomingTcpConnection.cs daemon/OutboundTcpConnection.cs datagear/command/CommandType.cs datagear/dg/MessagePackDataGear.cs
diffstat 7 files changed, 164 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Test/RewritingTest/EnumToInt.cs	Tue Dec 15 22:11:40 2020 +0900
@@ -0,0 +1,16 @@
+using System;
+
+namespace Christie_net {
+public enum EnumToInt {
+    Apple,
+    Orange,
+    Grape
+}
+
+public class EnumToIntCheck {
+    
+    public static void Main() {
+        EnumToInt elem = (EnumToInt) Enum.ToObject(typeof(EnumToInt), 1);
+    }
+}
+}
\ No newline at end of file
--- a/Test/RewritingTest/SocketListenerTask.cs	Thu Dec 03 19:53:18 2020 +0900
+++ b/Test/RewritingTest/SocketListenerTask.cs	Tue Dec 15 22:11:40 2020 +0900
@@ -15,7 +15,7 @@
     public void Run () {
         // Thread thread = new Thread (new ThreadStart (MethodThread));
         // thread.Start();
-        Task task = Task.Run((() => MethodThread()));
+        Task task = Task.Run(() => MethodThread());
     }
 
     private void MethodThread() {
--- a/daemon/Connection.cs	Thu Dec 03 19:53:18 2020 +0900
+++ b/daemon/Connection.cs	Tue Dec 15 22:11:40 2020 +0900
@@ -3,21 +3,22 @@
 using System.IO;
 using System.Net;
 using System.Net.Sockets;
+using Christie_net.codegear;
 using Christie_net.datagear.command;
 
 namespace Christie_net.daemon {
 public class Connection {
     public Socket socket;
     public string name;
-    //public CodeGearManager cgm;
+    public CodeGearManager cgm;
     public BlockingCollection<Command> sendQueue = new BlockingCollection<Command>();
     public bool sendManager = true;
     private object syncObject = new object();
     
-    // public Connection(Socket socket, CodeGearManager cgm) {
-    //     this.socket = socket;
-    //     this.cgm = cgm;
-    // }
+    public Connection(Socket socket, CodeGearManager cgm) {
+        this.socket = socket;
+        this.cgm = cgm;
+    }
     
     public Connection(){}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/daemon/IncomingTcpConnection.cs	Tue Dec 15 22:11:40 2020 +0900
@@ -0,0 +1,87 @@
+using System;
+using System.Data;
+using System.IO;
+using Christie_net.codegear;
+using Christie_net.datagear;
+using Christie_net.datagear.command;
+using Christie_net.datagear.dg;
+using MessagePack;
+using CommandType = Christie_net.datagear.command.CommandType;
+
+
+namespace Christie_net.daemon {
+public class IncomingTcpConnection {
+    private RemoteDataGearManager manager;
+    private CodeGearManager cgm;
+    private Connection connection;
+
+    public IncomingTcpConnection(Connection connection) {
+        this.connection = connection;
+        this.cgm = connection.cgm;
+    }
+
+    public void SetManager(RemoteDataGearManager manager) {
+        this.manager = manager;
+    }
+
+    public void Run() {
+        byte[] deserializeData = null;
+        
+        
+        while (true) {
+            try {
+                // データはType, length, dataの順で入っているっぽい
+                connection.socket.Receive(deserializeData);
+                RemoteMessage msg = MessagePackSerializer.Deserialize<RemoteMessage>(deserializeData);
+                CommandType type = CommandTypeExt.GetCommandTypeFormId(msg.type);
+                byte[] data;
+
+                switch (type) {
+                    case CommandType.PUT:
+                        data = new byte[MessagePackSerializer.Deserialize<int>(deserializeData)];
+                        connection.socket.Receive(data);
+
+                        try {
+                            MessagePackDataGear<object> dg =
+                                new MessagePackDataGear<object>(data, Type.GetType(msg.clazz));
+                        } catch (TypeLoadException e) {
+                            Console.WriteLine(e.StackTrace);
+                        }
+
+                        break;
+                    case CommandType.REMOTEPEEK:
+                    case CommandType.REMOTETAKE:
+                        try {
+                            Command cm = new CommandBuilder().Init(type).FromDgmName(msg.fromDmgName)
+                                .Key(msg.key)
+                                .Clazz(Type.GetType(msg.clazz))
+                                .Connection(connection).Build();
+                            //cgm.GetLocalDGM().RunCommand(cm);
+                        } catch (TypeLoadException e) {
+                            Console.WriteLine(e.StackTrace);
+                        }
+
+                        break;
+                    case CommandType.REPLY: // 待っていたwaitlistに渡してcsにセット
+                        data = new byte[MessagePackSerializer.Deserialize<int>(deserializeData)];
+                        connection.socket.Receive(data);
+
+                        try {
+                            MessagePackDataGear<object> dg =
+                                new MessagePackDataGear<object>(data, Type.GetType(msg.clazz));
+                            // cgm.GetDGM(msg.fromDgmName).ResolveWaitCommand(msg.key, dg);
+                        } catch (TypeLoadException e) {
+                            Console.WriteLine(e.StackTrace);
+                        }
+
+                        break;
+                    default:
+                        break;
+                }
+            } catch (IOException e) {
+                Console.WriteLine(e.StackTrace);
+            } 
+        }
+    }
+}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/daemon/OutboundTcpConnection.cs	Tue Dec 15 22:11:40 2020 +0900
@@ -0,0 +1,35 @@
+using System;
+using System.Threading;
+using Christie_net.datagear.command;
+
+namespace Christie_net.daemon {
+public class OutboundTcpConnection {
+
+    private Connection connection;
+
+    public OutboundTcpConnection(Connection connection) {
+        this.connection = connection;
+    }
+
+    public void Run() {
+        while (true) {
+            try {
+                Command cmd = connection.sendQueue.Take();
+                switch (cmd.type) {
+                    case CommandType.CLOSE:
+                    case CommandType.FINISH:
+                        cmd.Execute();
+                        return;
+                    default:
+                        break;
+                }
+                connection.Write(cmd);
+            } catch (ThreadInterruptedException e) {
+                Console.WriteLine(e.StackTrace);
+            }
+        }
+    }
+    
+    
+}
+}
\ No newline at end of file
--- a/datagear/command/CommandType.cs	Thu Dec 03 19:53:18 2020 +0900
+++ b/datagear/command/CommandType.cs	Tue Dec 15 22:11:40 2020 +0900
@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using Christie_net.daemon;
 
 namespace Christie_net.datagear.command {
 public enum CommandType {
@@ -12,6 +13,20 @@
     CLOSE,
     FINISH
 }
+
+public static class CommandTypeExt {
+    public static Dictionary<int, CommandType> hash = new Dictionary<int, CommandType>();
+
+    static CommandTypeExt() {
+        foreach (CommandType value in Enum.GetValues(typeof(CommandType))) {
+            hash.Add((int)value, value);
+        }
+    }
+
+    public static CommandType GetCommandTypeFormId(int id) {
+        return hash[id];
+    }
+}
 // public class CommandType {
 //     private static int lastId = 0; // コマンドの総数
 //     public readonly int id = ++lastId; // コマンドのid コンストラクタが呼ばれるたびにlastIdが++される
--- a/datagear/dg/MessagePackDataGear.cs	Thu Dec 03 19:53:18 2020 +0900
+++ b/datagear/dg/MessagePackDataGear.cs	Tue Dec 15 22:11:40 2020 +0900
@@ -13,6 +13,10 @@
 
     public MessagePackDataGear(Type clazz) : base(clazz) { }
 
+    public MessagePackDataGear(byte[] messagePack, Type clazz) : base(clazz) {
+        this.messagePack = messagePack;
+    }
+
     public byte[] GetMessagePack() {
         if (messagePack != null)
             return messagePack;