ease_engine 0.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +25 -0
  3. data/.travis.yml +3 -0
  4. data/Gemfile +4 -0
  5. data/README.md +33 -0
  6. data/Rakefile +1 -0
  7. data/benchmark/echo/echo_client.rb +90 -0
  8. data/benchmark/echo/echo_packet.rb +9 -0
  9. data/benchmark/echo/echo_server.rb +48 -0
  10. data/benchmark/fps.rb +23 -0
  11. data/benchmark/http.rb +9 -0
  12. data/benchmark/loop/Makefile +39 -0
  13. data/benchmark/loop/Makefile.rb +11 -0
  14. data/benchmark/loop/Rakefile +42 -0
  15. data/benchmark/loop/loop.c +38 -0
  16. data/benchmark/loop/loop.cs +33 -0
  17. data/benchmark/loop/loop.go +33 -0
  18. data/benchmark/loop/loop.js +23 -0
  19. data/benchmark/loop/loop.php +22 -0
  20. data/benchmark/loop/loop.rb +19 -0
  21. data/benchmark/loop/loop.scala +30 -0
  22. data/benchmark/loop/loop_c +0 -0
  23. data/benchmark/loop/loop_cs +0 -0
  24. data/benchmark/loop/loop_go +0 -0
  25. data/benchmark/measure/measure_client.rb +76 -0
  26. data/benchmark/measure/measure_server.rb +47 -0
  27. data/benchmark/process.rb +3 -0
  28. data/benchmark/tcp/tcp_client.rb +45 -0
  29. data/benchmark/tcp/tcp_server.rb +40 -0
  30. data/benchmark/udp/udp_client.rb +84 -0
  31. data/benchmark/udp/udp_packet.rb +40 -0
  32. data/benchmark/udp/udp_server.rb +33 -0
  33. data/bin/console +14 -0
  34. data/bin/setup +7 -0
  35. data/ease_engine.gemspec +27 -0
  36. data/lib/ease_engine.rb +35 -0
  37. data/lib/ease_engine/application.rb +269 -0
  38. data/lib/ease_engine/buffer.rb +25 -0
  39. data/lib/ease_engine/data.rb +158 -0
  40. data/lib/ease_engine/frame.rb +38 -0
  41. data/lib/ease_engine/http.rb +42 -0
  42. data/lib/ease_engine/log.rb +109 -0
  43. data/lib/ease_engine/measure.rb +25 -0
  44. data/lib/ease_engine/packet.rb +150 -0
  45. data/lib/ease_engine/platform.rb +19 -0
  46. data/lib/ease_engine/process.rb +31 -0
  47. data/lib/ease_engine/socket.rb +174 -0
  48. data/lib/ease_engine/time.rb +22 -0
  49. data/lib/ease_engine/timer.rb +72 -0
  50. data/lib/ease_engine/version.rb +3 -0
  51. data/lib/ease_engine/watcher.rb +95 -0
  52. data/types/Rakefile +83 -0
  53. data/types/csharp/EaseEngine.cs +39 -0
  54. data/types/csharp/EaseEngine/Buffer.cs +49 -0
  55. data/types/csharp/EaseEngine/Measure.cs +47 -0
  56. data/types/csharp/EaseEngine/Time.cs +41 -0
  57. data/types/csharp/EaseEngine/Version.cs +7 -0
  58. data/types/csharp/Unity/Assets/Menu.cs +56 -0
  59. data/types/csharp/Unity/Assets/Menu.unity +0 -0
  60. metadata +158 -0
@@ -0,0 +1,22 @@
1
+ module EaseEngine
2
+ class Time < Time
3
+ def to_s
4
+ sprintf( "%04d/%02d/%02d %02d:%02d:%02d.%06d", year, month, day, hour, min, sec, usec )
5
+ end
6
+
7
+ def to_unix_epoch_time_usec
8
+ to_i * 1000000 + usec
9
+ end
10
+
11
+ def -( time )
12
+ ( to_i - time.to_i ) * 1000000 + ( usec - time.usec )
13
+ end
14
+
15
+ def is_same_day( time )
16
+ return false if day != time.day
17
+ return false if month != time.month
18
+ return false if year != time.year
19
+ return true
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,72 @@
1
+ require "ease_engine/data"
2
+
3
+ module EaseEngine
4
+ class Timer
5
+ class Info
6
+ attr_accessor :end_time_usec, :arg, :callback
7
+
8
+ def initialize( end_time_usec, arg, callback )
9
+ @end_time_usec = end_time_usec
10
+ @arg = arg
11
+ @callback = callback
12
+ end
13
+ end
14
+
15
+ class Record < EaseEngine::Data::Record
16
+ def sort!
17
+ @ary.sort_by!{|info| info.data.end_time_usec}
18
+
19
+ super
20
+ end
21
+ end
22
+
23
+ attr_accessor :update_time_usec
24
+
25
+ def initialize( options = {} )
26
+ options[ :update_time_usec ] = 100000 if ! options.key?( :update_time_usec )
27
+
28
+ @update_time_usec = options[ :update_time_usec ]
29
+ @check_time_usec = 0
30
+
31
+ @timer = Record.new( 1, 0xFFFFFFFF )
32
+ @group = EaseEngine::Data::Group.new
33
+ end
34
+
35
+ def add( group_id, timeout_usec, arg, callback )
36
+ timer_id = @timer.add( Info.new( EaseEngine::Time.new.to_unix_epoch_time_usec + timeout_usec, arg, callback ) )
37
+ return 0 if 0 == timer_id
38
+
39
+ @group.add( group_id, timer_id ) if 0 < group_id
40
+
41
+ timer_id
42
+ end
43
+
44
+ def remove( group_id, timer_id = 0 )
45
+ @group.group( group_id ).each{|timer_id, group_id|
46
+ @timer.remove( timer_id )
47
+ }
48
+ @group.remove( group_id, timer_id )
49
+ @timer.remove( timer_id )
50
+ end
51
+
52
+ def update
53
+ return if 0 == @timer.size
54
+
55
+ usec = EaseEngine::Time.new.to_unix_epoch_time_usec
56
+ return if usec < @check_time_usec
57
+
58
+ @timer.each{|id, index, info|
59
+ if info.end_time_usec < usec
60
+ remove( 0, id )
61
+
62
+ info.callback.call( info.arg )
63
+ else
64
+ # 終了時間の早い順に昇順ソートしているので、後のデータをチェックする必要なし
65
+ break
66
+ end
67
+ }
68
+
69
+ @check_time_usec = usec + @update_time_usec
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ module EaseEngine
2
+ VERSION = "0.0.19"
3
+ end
@@ -0,0 +1,95 @@
1
+ require "cool.io"
2
+ require "ease_engine/platform"
3
+
4
+ module EaseEngine
5
+ class Watcher
6
+ class Info < Cool.io::IOWatcher
7
+ attr_accessor :io, :callbacks
8
+
9
+ def initialize( io, flags, callbacks )
10
+ @io = io
11
+ @callbacks = callbacks
12
+
13
+ super( io.kind_of?( EaseEngine::Socket ) ? io.socket : io, flags )
14
+ end
15
+
16
+ def on_readable
17
+ @callbacks[ :on_read ].call( @io ) if @callbacks.key?( :on_read )
18
+ end
19
+
20
+ def on_writable
21
+ @callbacks[ :on_write ].call( @io ) if @callbacks.key?( :on_write )
22
+ end
23
+
24
+ def on_remove
25
+ @callbacks[ :on_remove ].call( @io ) if @callbacks.key?( :on_remove )
26
+ end
27
+ end
28
+
29
+ def initialize( options = {} )
30
+ # 環境に合わせて適切なものを選択
31
+ if ! options.key?( :backend )
32
+ backend = :select
33
+ if EaseEngine::Platform.mac?
34
+ backend = :kqueue
35
+ elsif EaseEngine::Platform.linux?
36
+ backend = :epoll
37
+ end
38
+
39
+ options[ :backend ] = backend
40
+ end
41
+
42
+ EaseEngine::Log.inf( "Watcher #{options}" )
43
+ @loop = Cool.io::Loop.new( options )
44
+ @watches = {}
45
+ end
46
+
47
+ def watch( timeout = 0 )
48
+ if ! @watches.empty?
49
+ @loop.run_once( timeout )
50
+ elsif 0 < timeout
51
+ sleep timeout
52
+ end
53
+ end
54
+
55
+ def add( io, callbacks )
56
+ id = io.to_i
57
+ if @watches.key?( id )
58
+ info = @watches[ id ]
59
+ @watches.delete( id )
60
+ info.detach
61
+ end
62
+
63
+ flags = ""
64
+ flags = "r" if callbacks.key?( :on_read )
65
+ flags = "#{flags}w" if callbacks.key?( :on_write )
66
+
67
+ @watches[ id ] = Info.new( io, flags, callbacks )
68
+ @watches[ id ].attach @loop
69
+ end
70
+
71
+ def remove( io )
72
+ id = io.to_i
73
+ return if ! @watches.key?( id )
74
+
75
+ info = @watches[ id ]
76
+ @watches.delete( id )
77
+ info.detach
78
+ info.on_remove
79
+ end
80
+
81
+ def size
82
+ @watches.size
83
+ end
84
+
85
+ def each
86
+ @watches.each{|id, info|
87
+ yield info
88
+ }
89
+ end
90
+
91
+ def []( id )
92
+ @watches.key?( id ) ? @watches[ id ].io : nil
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,83 @@
1
+ require "fileutils"
2
+
3
+ class String
4
+ def camelize
5
+ list = []
6
+ self.split( "_" ).each{|name|
7
+ list.push name.capitalize
8
+ }
9
+ list.join
10
+ end
11
+ end
12
+
13
+ type = nil
14
+ build_cmds = []
15
+ clean_cmds = []
16
+
17
+ desc "Setup"
18
+ task :setup do
19
+ puts "setup #{type}"
20
+
21
+ case type
22
+ when :csharp
23
+ create_file_path = Proc.new{|names|
24
+ names.each_with_index{|name, index|
25
+ names[ index ] = name.camelize
26
+ }
27
+ "./csharp/#{names.join( '/' )}.cs"
28
+ }
29
+ else
30
+ STDERR.puts "Unknown type=#{type}"
31
+ return
32
+ end
33
+
34
+ Dir::glob( "../lib/**/*.rb" ).each{|src_path|
35
+ dst_path = src_path.sub( /^\.\.\/lib\/(.+)\.rb$/, "\\1" )
36
+ dst_path = create_file_path.call( dst_path.split( "/" ) )
37
+ # p [ src_path, dst_path ]
38
+
39
+ dir = File.dirname( dst_path )
40
+ FileUtils.mkdir_p( dir ) if ! Dir.exist?( dir )
41
+ FileUtils.cp( src_path, dst_path ) if ! File.exist?( dst_path )
42
+ }
43
+ end
44
+
45
+ desc "Build"
46
+ task :build do
47
+ build_cmds.each{|cmd|
48
+ sh cmd
49
+ }
50
+ end
51
+
52
+ desc "Clean"
53
+ task :clean do
54
+ clean_cmds.each{|cmd|
55
+ sh cmd
56
+ }
57
+ end
58
+
59
+ desc "CSharp"
60
+ task :csharp do
61
+ type = :csharp
62
+ srcs = [
63
+ "./csharp/EaseEngine.cs",
64
+ "./csharp/EaseEngine/Version.cs",
65
+ "./csharp/EaseEngine/Time.cs",
66
+ "./csharp/EaseEngine/Buffer.cs",
67
+ "./csharp/EaseEngine/Measure.cs",
68
+ ]
69
+ build_cmds = [
70
+ "mcs -target:library -out:./csharp/EaseEngine.dll #{srcs.join( ' ' )}",
71
+ "cp ./csharp/EaseEngine.dll ./csharp/Unity/Assets/."
72
+ ]
73
+ clean_cmds = [
74
+ "rm -f ./csharp/EaseEngine.dll"
75
+ ]
76
+ end
77
+
78
+ desc "Show"
79
+ task :show do
80
+ puts "type=#{type}"
81
+ puts "build_cmds=#{build_cmds}"
82
+ puts "clean_cmds=#{clean_cmds}"
83
+ end
@@ -0,0 +1,39 @@
1
+ using System;
2
+ using System.Diagnostics;
3
+
4
+ namespace EaseEngine {
5
+ public class Common {
6
+ static public UInt32 Rotate( UInt32 min, UInt32 max, UInt32 value, UInt32 add ){
7
+ if ( value < min || max < value ) return min;
8
+
9
+ value += add;
10
+ return ( value < min || max < value ) ? min : value;
11
+ }
12
+
13
+ static public string TraceInfo( StackFrame trace, ref string file, ref int line, ref string method ){
14
+ file = trace.GetFileName();
15
+ line = trace.GetFileLineNumber();
16
+ method = trace.GetMethod().Name;
17
+ return string.Format( "{0}:{1} {2}", file, line, method );
18
+ }
19
+
20
+ static public string TraceInfo( StackFrame trace ){
21
+ string file = "";
22
+ int line = 0;
23
+ string method = "";
24
+ return TraceInfo( trace, ref file, ref line, ref method );
25
+ }
26
+
27
+ static public string TraceInfo( int index, ref string file, ref int line, ref string method ){
28
+ return TraceInfo( new StackFrame( index + 1, true ), ref file, ref line, ref method );
29
+ }
30
+
31
+ static public string TraceInfo( int index ){
32
+ return TraceInfo( new StackFrame( index + 1, true ) );
33
+ }
34
+
35
+ static public string TraceInfo(){
36
+ return TraceInfo( 1 );
37
+ }
38
+ }
39
+ }
@@ -0,0 +1,49 @@
1
+ using System;
2
+ using System.Text;
3
+
4
+ namespace EaseEngine {
5
+ public class Buffer {
6
+ protected string m_value;
7
+ public string Value {
8
+ get { return m_value; }
9
+ set { m_value = value; }
10
+ }
11
+
12
+ protected int m_size;
13
+ public int Size {
14
+ get { return m_size; }
15
+ set { m_size = value; }
16
+ }
17
+
18
+ public Buffer( string value ){
19
+ m_value = value;
20
+ m_size = value.Length;
21
+ }
22
+
23
+ public Buffer() : this( "" ){
24
+ }
25
+
26
+ public override string ToString(){
27
+ return m_value;
28
+ }
29
+
30
+ public string Add( string value ){
31
+ if ( 0 < value.Length ){
32
+ StringBuilder sb = new StringBuilder( m_size + value.Length );
33
+ sb.Append( value );
34
+ m_value = sb.ToString();
35
+ m_size = m_value.Length;
36
+ }
37
+ return m_value;
38
+ }
39
+
40
+ public string Remove( int size ){
41
+ if ( size <= 0 ) return "";
42
+
43
+ m_size -= size;
44
+ string removed_str = m_value.Substring( 0, size );
45
+ m_value = m_value.Substring( size );
46
+ return removed_str;
47
+ }
48
+ }
49
+ }
@@ -0,0 +1,47 @@
1
+ using System;
2
+
3
+ namespace EaseEngine {
4
+ public class Measure {
5
+ protected Time m_start_time;
6
+ public Time StartTime {
7
+ get { return m_start_time; }
8
+ set { m_start_time = value; }
9
+ }
10
+
11
+ protected Time m_end_time;
12
+ public Time EndTime {
13
+ get { return m_end_time; }
14
+ set { m_end_time = value; }
15
+ }
16
+
17
+ protected UInt64 m_update_usec;
18
+ public UInt64 UpdateUSec {
19
+ get { return m_update_usec; }
20
+ set { m_update_usec = value; }
21
+ }
22
+
23
+ protected UInt64 m_count;
24
+ public UInt64 Count {
25
+ get { return m_count; }
26
+ set { m_count = value; }
27
+ }
28
+
29
+ public Measure(){
30
+ Start();
31
+ }
32
+
33
+ public void Start(){
34
+ m_start_time = m_end_time = new Time();
35
+ m_update_usec = 0;
36
+ m_count = 0;
37
+ }
38
+
39
+ public UInt64 Check(){
40
+ m_end_time = new Time();
41
+ m_update_usec += ( m_end_time - m_start_time );
42
+ m_count += 1;
43
+ m_start_time = m_end_time;
44
+ return m_update_usec;
45
+ }
46
+ }
47
+ }
@@ -0,0 +1,41 @@
1
+ using System;
2
+
3
+ namespace EaseEngine {
4
+ public class Time {
5
+ protected static DateTime m_unix_epoch_base_time = new DateTime( 1970, 1, 1 );
6
+
7
+ protected DateTime m_value;
8
+ protected DateTime m_value_unix_epoch_time;
9
+ public DateTime Value {
10
+ get { return m_value; }
11
+ set {
12
+ m_value = value;
13
+ m_value_unix_epoch_time = value.ToUniversalTime();
14
+ }
15
+ }
16
+
17
+ public Time(){
18
+ Value = DateTime.Now;
19
+ }
20
+
21
+ public UInt64 ToUnixEpochTimeUSec(){
22
+ return (UInt64)( ( m_value_unix_epoch_time - m_unix_epoch_base_time ).TotalSeconds * 1000000 );
23
+ }
24
+
25
+ public override string ToString(){
26
+ return string.Format( "{0:0000}/{1:00}/{2:00} {3:00}:{4:00}:{5:00}.{6:000000}",
27
+ m_value.Year, m_value.Month, m_value.Day, m_value.Hour, m_value.Minute, m_value.Second, m_value.Millisecond * 1000 );
28
+ }
29
+
30
+ public static UInt64 operator-( Time a, Time b ){
31
+ return a.ToUnixEpochTimeUSec() - b.ToUnixEpochTimeUSec();
32
+ }
33
+
34
+ public bool IsSameDay( Time time ){
35
+ if ( m_value.Day != time.Value.Day ) return false;
36
+ if ( m_value.Month != time.Value.Month ) return false;
37
+ if ( m_value.Year != time.Value.Year ) return false;
38
+ return true;
39
+ }
40
+ }
41
+ }