# HG changeset patch # User riono # Date 1608037900 -32400 # Node ID 46cfeb0609c5a3e9c8f4d918df55867c9fd1a4a5 # Parent 970c7f587126f05840a6cf4a7b24cb2c7548ced3 Add TcpConnections diff -r 970c7f587126 -r 46cfeb0609c5 Test/RewritingTest/EnumToInt.cs --- /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 diff -r 970c7f587126 -r 46cfeb0609c5 Test/RewritingTest/SocketListenerTask.cs --- 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() { diff -r 970c7f587126 -r 46cfeb0609c5 daemon/Connection.cs --- 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 sendQueue = new BlockingCollection(); 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(){} diff -r 970c7f587126 -r 46cfeb0609c5 daemon/IncomingTcpConnection.cs --- /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(deserializeData); + CommandType type = CommandTypeExt.GetCommandTypeFormId(msg.type); + byte[] data; + + switch (type) { + case CommandType.PUT: + data = new byte[MessagePackSerializer.Deserialize(deserializeData)]; + connection.socket.Receive(data); + + try { + MessagePackDataGear dg = + new MessagePackDataGear(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(deserializeData)]; + connection.socket.Receive(data); + + try { + MessagePackDataGear dg = + new MessagePackDataGear(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 diff -r 970c7f587126 -r 46cfeb0609c5 daemon/OutboundTcpConnection.cs --- /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 diff -r 970c7f587126 -r 46cfeb0609c5 datagear/command/CommandType.cs --- 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 hash = new Dictionary(); + + 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が++される diff -r 970c7f587126 -r 46cfeb0609c5 datagear/dg/MessagePackDataGear.cs --- 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;