forked from eventmachine/eventmachine
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module EventMachine | ||
module Protocols | ||
# ObjectProtocol allows for easy communication using marshaled ruby objects | ||
# | ||
# module RubyServer | ||
# include EM::P::ObjectProtocol | ||
# | ||
# def receive_object obj | ||
# send_object({'you said' => obj}) | ||
# end | ||
# end | ||
# | ||
module ObjectProtocol | ||
def receive_data data # :nodoc: | ||
(@buf ||= '') << data | ||
|
||
while @buf.size >= 4 | ||
if @buf.size >= 4+(size=@buf.unpack('N').first) | ||
@buf.slice!(0,4) | ||
receive_object Marshal.load(@buf.slice!(0,size)) | ||
else | ||
break | ||
end | ||
end | ||
end | ||
|
||
# Invoked with ruby objects received over the network | ||
def receive_object obj | ||
# stub | ||
end | ||
|
||
# Sends a ruby object over the network | ||
def send_object obj | ||
data = Marshal.dump(obj) | ||
send_data [data.respond_to?(:bytesize) ? data.bytesize : data.size, data].pack('Na*') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
$:.unshift "../lib" | ||
require 'eventmachine' | ||
require 'test/unit' | ||
|
||
class TestObjectProtocol < Test::Unit::TestCase | ||
Host = "127.0.0.1" | ||
Port = 9550 | ||
|
||
module Server | ||
include EM::P::ObjectProtocol | ||
def post_init | ||
send_object :hello=>'world' | ||
end | ||
def receive_object obj | ||
$server = obj | ||
EM.stop | ||
end | ||
end | ||
|
||
module Client | ||
include EM::P::ObjectProtocol | ||
def receive_object obj | ||
$client = obj | ||
send_object 'you_said'=>obj | ||
end | ||
end | ||
|
||
def test_send_receive | ||
EM.run{ | ||
EM.start_server Host, Port, Server | ||
EM.connect Host, Port, Client | ||
} | ||
|
||
assert($client == {:hello=>'world'}) | ||
assert($server == {'you_said'=>{:hello=>'world'}}) | ||
end | ||
end |