annotate Christie/for.NET.md @ 27:459b9fa6f047

backup 2021-01-20
author autobackup
date Wed, 20 Jan 2021 00:10:03 +0900
parents 6f9bfbfc54c9
children 0bd2a99a349e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
1 # ChristieのC#への書き換えログ
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
3 ## メソッド等の置換一覧
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
4 java ConcurrentHashMap<> → C# ConcurrentDictionary<>
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
5 java map<> → C# Dictionary<>
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
6 java getClass() → C# GetType()
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
7 java getGenericSuperclass() → C# type.BaseType 補足あり
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
8 java ClassCastException → C# InvalidCastException
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
9 java ByteBuffer → C# MemoryStream
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
10 ```
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
11 java
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
12 T data = null;
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
13 Class<T> clazz = null;
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
14 byte[] messsagePack = null;
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
15 MessagePack packer = new MessagePack();
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
16 // write read
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
17 messagePack = packer.write(data);
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
18 packer.read(messagePack, clazz);
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
19
0
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
21 C#
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
22 T data;
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
23 Type clazz = null;
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
24 byte[] messagePack = null;
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
25 // write read
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
26 messagePack = MessagePackSerializer.Serialize(data);
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
27 MessagePackSerializer.Deserialize<T>(messagePack);
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
28 ```
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
29 java TreeMap&lt;&gt; → C# SortedDictionary&lt;&gt;
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
30 java HashMap&lt;&gt; → C# Dictionary&lt;&gt;
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
31 java Class clazz → C# Type clazz
15
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
32 java LinkedBlockingQueue&lt;&gt; → C# ~~ConcurrentQueue&lt;&gt;~~ → BlockingCollection&lt;&gt;
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
33 java socket.getInetAddress().getHostName() → C# Dns.GetHostEntry(((IPEndPoint) socket.RemoteEndPoint).Address.ToString()).HostName
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
34 java clazz.getName() → C# `type.Name`
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
35 java socket.getInputStream().read() → C# socket.Receive([格納する配列]) (intの長さが返る)
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
36 java ClassNotFoundException → C# TypeLoadException
0
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
37
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
38 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
39
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
40 ## MessagePackが使用されていたクラス
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
41 * MessagePackDataGear
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
42 * RemoteMessage
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
43
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
44
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
45
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
46 ---
14
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
47 ## 実装を飛ばしている物(仮置きしているもの)
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
48 IncomingTcpConnetion 送られてくるデータを格納する配列のサイズ
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
49 PriorityThreadPoolExecutor ThreadPoolの優先度実装
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
50
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
51 ---
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
52 ## Tips
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
53 C#
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
54 object → 全ての継承元クラス
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
55 Type → 型情報
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
56 T → ジェネリクス
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
57
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
58 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
59 javaのatomicintagerはc#に存在しない
0
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 intを読む, 代入するくらいは原始的が保証されているらしい
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 一度実装してみてダメなら
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 volatile修飾子をつける→読み書きがスレッドセーフになるが、コンパイラで最適化されない
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 https://ufcpp.net/study/csharp/misc_synchronize.html
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 intのインクリメントとかはこのメソッドを使ったほうがいいかも
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 https://docs.microsoft.com/en-us/dotnet/api/system.threading.interlocked?view=netcore-3.1
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
69
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
70 ---
0
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
71
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 java
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
74 ```public static <T> Class<T> [関数名] () { }```
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
75 → Class&lt;T&gt; は返り値の型
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
76 →その前の&lt;T&gt;はジェネリスクメソッドの型を表す
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
77
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
78 C#
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
79 ```public static Type [関数名]<T> () { }```
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
80 →Typeは返り値の型
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
81 →関数名の直後の&lt;T&gt;はジェネックメソッドの型を表す
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
82
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
83
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
84 java
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
85 int.class vs Intager.class
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
86 javaは独自でそれぞれの型についてプリミティブ型とクラス型が用意されているらしい…
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
87 https://stackoverflow.com/questions/22470985/integer-class-vs-int-class/22471247
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
88
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
89
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
90 java側のdatagear/WrapperClassいらないかも...
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
91 もうちょい調べる
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
92
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
93 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
94 javaでは
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
95 ```T typeJava = null;```
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
96 はできるがC#では初期化でT型にnullを代入できない
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
97
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
98 T型の初期化は
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
99 ```T typeCs = default(T);```
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
100 とする。(T)は省略可能
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
101
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
102
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
103 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
104 java getGenericSuperclass() → type.BaseTypeについて
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
105 どちらも直接のスーパクラス、直接の継承元のTypeを返す
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
106
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
107
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
108 javaの場合
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
109 Object クラス、インタフェース、プリミティブ型、または void を表す場合、null が返され、配列の場合はClassが帰ってくる
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
110
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
111
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
112 C#の場合
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
113 直接の継承しているTypeを返す、もし現在のTypeがobjectかインターフェースならnullが返る
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
114
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
115 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
116 javaの場合
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
117 ジェネリスククラスの宣言時にジェネリスクの型宣言は省略できる
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
118 ex:``` Datagear dg;```
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
119
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
120
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
121 C#の場合
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
122 ジェネリッククラス宣言時にジェネリックの型宣言は省略できない
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
123 ex: ```Datagear<Type> dg; ```
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
124
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
125 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
126
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
127 ~~MessagePackはjavaではread,writeにはジェネリックを使用できるが、
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
128 C#ではジェネリスクを使用することができない
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
129 →ラッパーか何かを挟んで型判断してread,writeするしかない?~~
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
130 C#で関数名が全然異なっていた...
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
131
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
132 javaリファレンス
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
133 http://msgpack.org/javadoc/current/org/msgpack/MessagePack.html#read(byte[],%20java.lang.Class)
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
134
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
135
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
136 C#README
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
137 https://github.com/neuecc/MessagePack-CSharp#quick-start
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
138
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
139 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
140 C#のenumの扱いについて
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
141
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
142 C#のenumではstringは許容されていない
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
143 また、enum内でフィールド変数や関数を含めることはできないので、ヘルパークラスを実装してそちらを利用する必要がある
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
144
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
145 enum内でfor文などは使えないので、今回は別途関数を作成した
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
146 コンストラクタで使用するとか...?
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
147
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
148 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
149 java HashMapとC# Dictionaryの違いについて
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
150 https://stackoverflow.com/questions/1273139/c-sharp-java-hashmap-equivalent
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
151 null周りなど違いがあるので注意
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
152
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
153 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
154 ~~C#のBlockingCollectionは既定ではConcurrentQueue、つまりFIFOになっているが、特に使う理由はないのでConcurrentQueueを最初から利用することに~~
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
155 →いやブロッキングどうするねん
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
156 →やっぱり使う
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
157 https://www.atmarkit.co.jp/ait/articles/1802/07/news019.html
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
158 →使用後にDisposeしないといけない
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
159 →無理...?
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
160 →もしかしたらずっとBlockingCollection利用してるかも
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
161 →Getするところでlengthが0になったもしくは
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
162 破棄をしていたらdisposeする
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
163
15
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
164 #### BlockingCollection使用箇所(Dispoes処理)
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
165 * Connection
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
166 * DataGears → fin
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
167 * WaitList → fin
0
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
168
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
169
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
170 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
171 C#だとsynchronized修飾子がないので
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
172 ```
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
173 private object syncObject = new object();
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
174
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
175 public void Function(){
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
176 lock (syncObject) {
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
177
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
178 }
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
179 }
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
180 ```
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
181 でロックをかける必要がある
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
182
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
183 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
184 javaだとジェネリッククラスを型として変数宣言する際にはジェネリックの型宣言は必要ないが、
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
185 c#では変数宣言する際に型宣言が必須なので、今回はとりあえずobject型で宣言している。
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
186
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
187 DataGearの宣言は基本的にDataGear&lt;object&gt;で
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
188
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
189 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
190 java側で出力されるsocket.getInetAddress().getHostName()
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
191 localhost:3333
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
192
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
193 ~~これと同様の結果をC#でも欲しい感じ~~
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
194
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
195 javaはSocketクラスが割となんでもしてくれる感じがあるが、
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
196 C#はSocket, Dns, IPAdress, IPEndPointとかなり役割が分散されているので注意
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
197
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
198 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
199 daemon/Connection
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
200
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
201 Close()について
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
202 socket.shutdownでSocketShutdown.Bothで本当にあっているか
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
203
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
204 ---
0
e12992dca4a0 init from Growi
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
205 java
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
206 socket.getChannel().write(buffer);について
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
207
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
208 javaのChannelではwrite(), read()などをByteBufferなどと一緒に使ってデータの送受信ができる。
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
209 Channelの開き方は、socketとは別にopenする必要があるので注意。
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
210
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
211 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
212 christleに導入されているMessagePackのバージョンは0.6以前のもの
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
213 そのため、シリアライズするクラスには@Messageが必要になる
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
214 https://github.com/msgpack/msgpack-java/wiki/QuickStart-for-msgpack-java-0.6.x-(obsolete)
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
215
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
216 java版は0.7.xから仕様がかなり変わっているため注意が必要
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
217
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
218
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
219
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
220 C#版のMassagePackには日本人が作成したものを利用している(Unityとかでもサポートされているらしい)
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
221 https://github.com/neuecc/MessagePack-CSharp#quick-start
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
222
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
223
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
224 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
225 javaとC#のenumの仕様はかなり異なっており
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
226 C#ではenumにメソッドを生やすことができない
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
227
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
228 ~~最初はヘルパークラスを実装することで対応しようとしたが、挙動的にenum各要素を初期化する必要があるとわかったため、独自にenumの挙動をするクラスを作成した~~
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
229
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
230 https://qiita.com/lilacs/items/167a73fbbfedf83eb51a
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
231 https://bleis-tift.hatenablog.com/entry/20080808/1218133517
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
232
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
233 必要なのはenumをintにする変換なので、C#ではenumをintにcastできるので廃止
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
234
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
235 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
236 protected internalについて
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
237
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
238 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
239 LinkedBlockingQueueとConcurrentQueueの特徴について
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
240
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
241 * LinkedBlockingQueue
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
242 * FIFO
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
243 * boolean contains(Object o): 要素の確認
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
244 * peek(): 先頭取得、削除しない
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
245 * poll(): 先頭取得、削除
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
246 * put(): 末尾に追加 完了するまでブロック
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
247 * add(): 末尾に追加 失敗時例外
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
248 * put, addの違いについては:https://docs.oracle.com/javase/jp/8/docs/api/java/util/concurrent/BlockingQueue.html#add-E-
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
249
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
250 * ConcurrentQueue
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
251 * FIFO
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
252 * スレッドセーフ
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
253 * Enqueue(): 末尾に追加
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
254 *
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
255
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
256 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
257 Socketはaccept()するとその返り値にacceptされたsocketが返る
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
258 よってそれを変数に入れて利用する
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
259
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
260
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
261 ---
5
8f5664d57cfd backup 2020-12-19
autobackup
parents: 2
diff changeset
262 javaのThreadはThreadを継承して作成をする。
8f5664d57cfd backup 2020-12-19
autobackup
parents: 2
diff changeset
263 Threadの名前をつける必要あり
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
264
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
265
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
266 c#は
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
267 `Task task = Task.Run(() => MethodThread());
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
268 task.Run();`
5
8f5664d57cfd backup 2020-12-19
autobackup
parents: 2
diff changeset
269 (名前をつける必要なし)
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
270
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
271 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
272 IncommingTcpConnectionなどのスレッドはAcceptThreadで一括でTask.Runする
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
273
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
274 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
275 IncomingTcpConnetion
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
276
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
277 送られてくるデータ長がわからないので
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
278 とりあえず4096と置いている
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
279
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
280 ---
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
281 TCPで送受信するデータは、Remotemessage(Command), length, dataの順で入っている
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
282
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
283 PutCommand
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
284 Command
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
285 IncomingTcpConnection
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
286 を参照
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
287
5
8f5664d57cfd backup 2020-12-19
autobackup
parents: 2
diff changeset
288 ---
8f5664d57cfd backup 2020-12-19
autobackup
parents: 2
diff changeset
289 javaではserverSocketクラスがあり、listenerはこれを使えばいいが、c# 側にはない。
8f5664d57cfd backup 2020-12-19
autobackup
parents: 2
diff changeset
290
23
6f9bfbfc54c9 backup 2021-01-15
autobackup
parents: 21
diff changeset
291 ここで注意するのが、javaではacceptでlistenを開始するが、C#ではListenでlistenを開始することに注意
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
292
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
293
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
294 ---
14
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
295 優先度付きThreadPoolについて
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
296 javaのほうでは実装されているが、現状処理が複雑なため一旦実装を飛ばす
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
297 優先度が実装されている理由についてはheartbeatを実装したかったと考えられる
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
298
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
299 Taskは内部でThreadPoolを作成しているらしい
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
300 https://oita.oika.me/2016/02/18/task-and-threadpool/
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
301
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
302 優先度をつけるならThreadPoolはTaskを利用できないかも
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
303
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
304 ---
15
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
305 Task.RunとTask.Factory.Startnewについて
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
306
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
307 無限ループさせるような、すぐには終了しないTaskについては
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
308 LongRunオプションをつける必要がある。
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
309
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
310 これは、Task.Runは裏でThreadPoolを作るが、この数がTask.Runの場合規定数(通常だと16(cpuに依存))に制限されており、無限ループのTaskも個の規定数にカウントされてしまうため
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
311 https://oita.oika.me/2016/02/18/task-and-threadpool/
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
312
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
313 それを避けるためにStartnewでTaskを作成する
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
314
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
315 https://qiita.com/chocolamint/items/3e2e4951ea0fa2ccd19e
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
316
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
317 ---
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
318 C#のTaskでのThreadPoolについて
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
319
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
320 https://stackoverflow.com/questions/35734051/c-sharp-task-thread-pool-running-100-tasks-across-only-10-threads
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
321
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
322 ---
27
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
323 lockのwaitとnotifyについて
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
324 Javaでは排他制御として`synchronized(lock)`(lockはobject型)を使用することで排他制御ができる。
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
325 解除にはlock.notifyなどを使う
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
326 https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#notify()
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
327
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
328 C#ではMoniterクラスの中にwaitやpulse関数があり、同じように使えるっぽい
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
329 https://docs.microsoft.com/en-us/dotnet/api/system.threading.monitor.pulse?view=net-5.0
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
330
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
331 ```java
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
332 Object lock = new Object;
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
333 synchronized(lock) {
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
334 lock.notify(); // lockの解除
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
335 }
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
336 ```
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
337
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
338 ```c#
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
339 objecy lockObj = new object;
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
340 lock(lockObj) {
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
341 Moniter.Pulse(lock);
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
342 }
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
343 ```
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
344
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
345 ---
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
346 ## TODO
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
347 * annotation → fin
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
348 * daemon
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
349 * connection → cgm要実装
5
8f5664d57cfd backup 2020-12-19
autobackup
parents: 2
diff changeset
350 * ChristieDaemon → fin
8f5664d57cfd backup 2020-12-19
autobackup
parents: 2
diff changeset
351 * AcceptThread → cgm要実装
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
352 * IncomingTcpConnection → CodeGearManager要実装
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
353 * OutboundTcpConnection → fin
27
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
354 * ThreadPoolExecutors 実装
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
355 * codegear
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
356 * CodeGear → InputDataGear要実装
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
357 * InputDataGear
14
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
358 * cgm要実装
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
359 * CodeGearManager
27
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
360 * cg要実装
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
361 * ThreadPoolExcuter要実装
14
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
362 * CodeGearExecutor
9404c0e15e39 backup 2021-01-03
autobackup
parents: 5
diff changeset
363 * cgm要実装
15
7c64665b26bf backup 2021-01-04
autobackup
parents: 14
diff changeset
364 * cg要実装
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
365 * datagear
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
366 * command
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
367 * Command → fin
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
368 * CommandType → fin
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
369 * CommandBuilder → fin
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
370 * TakeCommand → CodeGear要実装
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
371 * その他のcommand → fin
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
372 * dg → fin
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
373 * DataGearManager → fin
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
374 * DataGears → fin
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
375 * WaitList → fin
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
376 * LocalDataGearManager → fin
27
459b9fa6f047 backup 2021-01-20
autobackup
parents: 23
diff changeset
377 * RemoteDataGearManager → fin
2
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
378
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
379
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
380
b6c284fd5ae4 backup 2020-12-16
autobackup
parents: 0
diff changeset
381