0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

unity > udpEmu (udp emulator) > 登録した応答をランダムに返す

Last updated at Posted at 2015-09-19
実装環境
Unity 5.1.3-f1 on MacOS X 10.8.5

udpMonitor @ QiitaというUDPの通信をモニターできるものを作成した。
その通信した情報を元に、コマンド応答をエミュレーションするものを作りたかった。

code (v0.5)

udpEcho @ Qiitaを元に作成した。

v0.5 @ github

要となる処理(udpEchoから変更や追加したもの)は以下の2つ

  • Assets/MyResponseDictionaryUtil.cs : 辞書登録関連
  • Assets/udpEmu.cs : udp通信関連

使い方

モード変更

EMULATOR と REGISTER の2つのモードがある。RESIGTERで応答登録をして、それをEMULATORで使う。

それぞれのモード変更はudpで以下のコマンドを使う

  1. EMULATOR -> REGISTER : SOT,dfae271
  2. REGISTER -> EMULATOR : EOT,dfae271

dfae271はudpEmuのv0.1のハッシュタグ。udpコマンドの誤検知を回避するため。

SOTやEOTとしているのは udpMonior (改良予定)の出力を取り入れやすくするよう考慮している。

REGISTERでのコマンド

  1. コマンド文字列登録 : ,tx,hello
  2. 応答文字列登録 : ,rx,hi,Mike-neko

1列目は無視する(日時情報など入っていてもOK).
2列目は"tx"か"rx"を入れる
3列目以降(","区切りもおそらく可能)の文字を登録する。

使用例

udpEmu側

udpEmu自体は起動するだけでいい。
起動直後は EMULATOR モードになっている。

Scene_unity_-150917-udpEmu-PC__Mac___Linux_Standalone__Personal.jpg

相手側

udpEmuに対してコマンドを送信するものからの処理。

今回は以下を使用

動作環境
IP: 192.168.10.8
OS: CentOS 6.5 (32bit)

CentOSからは以下のbashスクリプトを実行 $bash testUdpEmu-exe

# !/bin/env bash

for data in \
		"SOT,dfae271" \
		",tx,hello" \
		",rx,hi,Mike-neko" \
		",tx,hello" \
		",rx,hi,Matatabi" \
		",tx,hello" \
		",rx,hi,Kagemusha jya" \
		",tx,hello" \
		",rx,hi,Honnninn jya" \
		",tx,hello" \
		",rx,hi,Who are you?" \
		"EOT,dfae271" \
		; do
	sleep 1
	echo $data | nc -w 1 -u 192.168.10.3 6000
done

for loop in $(seq 1 7);do
   echo "hello" | nc -w 1 -u 192.168.10.3 6000
   usleep 125000 # arbitrary (every 125msec)
done

上記において

  • 最初のfor : 辞書登録
  • 2つ目のfor : 7回 hello を送信

実行結果

登録が終わって、ランダムに応答が返っている様子が見られる。

[udpEmu]$ bash testUdpEmu-exec 
emulator >> register mode
register >> emulator mode
hi,Mike-neko
hi,Honnninn jya
hi,Mike-neko
hi,Kagemusha jya
hi,Matatabi
hi,Who are you?
hi,Matatabi
[udpEmu]$ 

手動実行

スクリプトでなく、1つ1つudp通信する場合は、以下を順次送るといい。

SOT,dfae271
,tx,hello
,rx,hi,Mike-neko
,tx,hello
,rx,hi,Matatabi
,tx,hello
,rx,hi,Kagemusha jya
,tx,hello
,rx,hi,Honnninn jya
,tx,hello
,rx,hi,Who are you?
EOT,dfae271

上記にて登録が終わってからは以下を実行するたびに、異なる応答が受けられる。

hello
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?