# HG changeset patch # User riono # Date 1606155110 -32400 # Node ID 4a3115ba746d346c5b9af9d476daed8f3c2aab96 # Parent 7352793b5dbe079c4221cb7b1e9df95cb1dadc9d fix CommandType enum diff -r 7352793b5dbe -r 4a3115ba746d Christie_net.csproj --- a/Christie_net.csproj Sun Nov 22 01:52:22 2020 +0900 +++ b/Christie_net.csproj Tue Nov 24 03:11:50 2020 +0900 @@ -3,7 +3,7 @@ Exe netcoreapp3.1 - SocketIPPortCheck + EnumInit diff -r 7352793b5dbe -r 4a3115ba746d Test/RewritingTest/EnumInit.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Test/RewritingTest/EnumInit.cs Tue Nov 24 03:11:50 2020 +0900 @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using Christie_net.datagear.command; + + +public class EnumInit { + private static int nextId = 0; + public readonly int id = ++nextId; + public static readonly Dictionary hash = new Dictionary(); + + private EnumInit() { + hash.Add(id, this); + } + + public static EnumInit GetEnumInit(int nid) { + return hash[nid]; + } + + public static readonly EnumInit Val1 = new EnumInit(); + public static readonly EnumInit Val2 = new EnumInit(); + + public static void Main() { + Console.WriteLine(Val1.id); + Console.WriteLine(GetEnumInit(1).id); + + } +} diff -r 7352793b5dbe -r 4a3115ba746d datagear/DataGears.cs --- a/datagear/DataGears.cs Sun Nov 22 01:52:22 2020 +0900 +++ b/datagear/DataGears.cs Tue Nov 24 03:11:50 2020 +0900 @@ -1,6 +1,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using Christie_net.annotation; using Christie_net.datagear.command; using Christie_net.datagear.dg; @@ -71,15 +72,12 @@ /// public object GetData(Command cm) { lock (syncObject) { - switch (cm.type.commandType) { - case CommandType.TAKE: - case CommandType.REMOTETAKE: - return Take(cm.key); - case CommandType.PEEK: - case CommandType.REMOTEPEEK: - return Peek(cm.key); + CommandType type = cm.type; + if (type == CommandType.TAKE || type == CommandType.REMOTETAKE) { + return Take(cm.key); + } else if (type == CommandType.PEEK || type == CommandType.REMOTEPEEK){ + return Peek(cm.key); } - return null; } } diff -r 7352793b5dbe -r 4a3115ba746d datagear/command/Command.cs --- a/datagear/command/Command.cs Sun Nov 22 01:52:22 2020 +0900 +++ b/datagear/command/Command.cs Tue Nov 24 03:11:50 2020 +0900 @@ -6,8 +6,7 @@ namespace Christie_net.datagear.command { public abstract class Command { - private CommandType _type; - public CommandTypeEtx type; + public CommandType type; public string key; public string toDgmName; // for take public string fromDgmName = "local"; // for remotetake/reply @@ -18,8 +17,7 @@ public Connection connection = null; // for reply public Command(CommandBuilder cb) { - this._type = cb.type.commandType; - this.type = new CommandTypeEtx(_type); + this.type = cb.type; this.key = cb.key; this.toDgmName = cb.toDgmName; this.fromDgmName = cb.fromDgmname; @@ -47,7 +45,7 @@ } public override string ToString() { - return "Command : type = " + type.commandType + ", key = " + key + "toDgmName = " + toDgmName + + return "Command : type = " + type + ", key = " + key + "toDgmName = " + toDgmName + " fromDgmName = " + fromDgmName + " cgmID = " + cgmID + " cg = " + cg + " dg = " + dg + " clazz = " + clazz + "connection = " + connection; } diff -r 7352793b5dbe -r 4a3115ba746d datagear/command/CommandBuilder.cs --- a/datagear/command/CommandBuilder.cs Sun Nov 22 01:52:22 2020 +0900 +++ b/datagear/command/CommandBuilder.cs Tue Nov 24 03:11:50 2020 +0900 @@ -5,8 +5,7 @@ namespace Christie_net.datagear.command { public class CommandBuilder { - private CommandType _type; - protected internal CommandTypeEtx type; + protected internal CommandType type; protected internal string key = null; protected internal string toDgmName = null; // for take protected internal string fromDgmname = "local"; // for remotetake/reply @@ -19,8 +18,7 @@ //private CommandFactory factory = new CommandFactory(); public CommandBuilder init(CommandType type) { - this._type = type; - this.type = new CommandTypeEtx(_type); + this.type = type; this.key = null; this.toDgmName = null; this.fromDgmname = "local"; diff -r 7352793b5dbe -r 4a3115ba746d datagear/command/CommandType.cs --- a/datagear/command/CommandType.cs Sun Nov 22 01:52:22 2020 +0900 +++ b/datagear/command/CommandType.cs Tue Nov 24 03:11:50 2020 +0900 @@ -2,43 +2,31 @@ using System.Collections.Generic; namespace Christie_net.datagear.command { -public enum CommandType { - PUT, - TAKE, - PEEK, - REMOTETAKE, - REMOTEPEEK, - REPLY, - CLOSE, - FINISH -} +public class CommandType { + private static int lastId = 0; // コマンドの総数 + public readonly int id = ++lastId; // コマンドのid コンストラクタが呼ばれるたびにlastIdが++される + public static readonly Dictionary hash = new Dictionary(); -// C#ではenumに関数は生やせないためヘルパークラスを実装 -public class CommandTypeEtx { - public CommandType commandType; - public int id; // command ID - public static Dictionary hash = new Dictionary(); // コマンド対応表 - private static int lastID = 0; // total command number - - // コマンドごとにコンストラクタ内の処理をする必要がある - public CommandTypeEtx(CommandType type) { - commandType = type; - StoreValues(); - id = IncrementLastID(); + private CommandType() { + hash.Add(this.id, this); } - private static int IncrementLastID() { - return ++lastID; - } - - public static CommandType GetCommandTypeFromID(int id) { + /// + /// idよりCommandTypeを返す + /// + /// + /// + public static CommandType GetCommandTypeFromId(int id) { return hash[id]; } - public static void StoreValues() { - foreach (CommandType value in Enum.GetValues(typeof(CommandType))) { - hash.Add(Convert.ToInt32(value), value); - } - } + public static readonly CommandType PUT = new CommandType(); + public static readonly CommandType TAKE = new CommandType(); + public static readonly CommandType PEEK = new CommandType(); + public static readonly CommandType REMOTETAKE = new CommandType(); + public static readonly CommandType REMOTEPEEK = new CommandType(); + public static readonly CommandType REPLY = new CommandType(); + public static readonly CommandType CLOSE = new CommandType(); + public static readonly CommandType FINISH = new CommandType(); } } \ No newline at end of file diff -r 7352793b5dbe -r 4a3115ba746d datagear/command/RemoteTakeCommand.cs --- a/datagear/command/RemoteTakeCommand.cs Sun Nov 22 01:52:22 2020 +0900 +++ b/datagear/command/RemoteTakeCommand.cs Tue Nov 24 03:11:50 2020 +0900 @@ -8,7 +8,7 @@ } public override void Execute() { - new CommandBuilder().init(CommandType.REPLY). + //new CommandBuilder().init(CommandType.REPLY). } public override MemoryStream Convert() {