Created
April 14, 2010 03:20
-
-
Save takeshik/365415 to your computer and use it in GitHub Desktop.
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
// License: MIT License | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Runtime.Remoting; | |
using System.Runtime.Remoting.Channels; | |
using System.Runtime.Remoting.Channels.Tcp; | |
using System.Runtime.Serialization.Formatters; | |
public class TransparentEnumerable<T> | |
: MarshalByRefObject, | |
IEnumerable<T> | |
{ | |
private readonly IEnumerable<T> _enumerable; | |
public TransparentEnumerable(IEnumerable<T> iterator) | |
{ | |
this._enumerable = iterator; | |
} | |
public IEnumerator<T> GetEnumerator() | |
{ | |
return new TransparentEnumerator<T>(this._enumerable.GetEnumerator()); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
} | |
public class TransparentEnumerator<T> | |
: MarshalByRefObject, | |
IEnumerator<T> | |
{ | |
private readonly IEnumerator<T> _enumerator; | |
public TransparentEnumerator(IEnumerator<T> iterator) | |
{ | |
this._enumerator = iterator; | |
} | |
public void Dispose() | |
{ | |
this._enumerator.Dispose(); | |
} | |
public Boolean MoveNext() | |
{ | |
return this._enumerator.MoveNext(); | |
} | |
public void Reset() | |
{ | |
this._enumerator.Reset(); | |
} | |
public T Current | |
{ | |
get | |
{ | |
return this._enumerator.Current; | |
} | |
} | |
Object IEnumerator.Current | |
{ | |
get | |
{ | |
return this.Current; | |
} | |
} | |
} | |
// BELOW IS SAMPLE: | |
public class Server | |
{ | |
private static TcpServerChannel _channel; | |
private static readonly TestObject _obj = new TestObject(); | |
public static void Main() | |
{ | |
_channel = new TcpServerChannel(new Dictionary<Object, Object>() | |
{ | |
{"port", 12345}, | |
{"useIpAddress", false}, | |
}, new BinaryServerFormatterSinkProvider() | |
{ | |
TypeFilterLevel = TypeFilterLevel.Full, | |
}); | |
ChannelServices.RegisterChannel(_channel, false); | |
RemotingServices.Marshal(_obj, "test", typeof(TestObject)); | |
Console.ReadLine(); | |
} | |
} | |
class Client | |
{ | |
static void Main(string[] args) | |
{ | |
TcpClientChannel c = new TcpClientChannel(); | |
ChannelServices.RegisterChannel(c, false); | |
RemotingConfiguration.RegisterWellKnownClientType(typeof(TestObject), "tcp://localhost:12345/test"); | |
var x = Activator.CreateInstance<TestObject>(); | |
try | |
{ | |
foreach (var i in x.Iterate()) | |
Console.WriteLine(i); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
foreach (var i in x.TransparentIterate()) | |
Console.WriteLine(i); | |
} | |
} | |
public class TestObject | |
: MarshalByRefObject | |
{ | |
public IEnumerable<Int32> Iterate() | |
{ | |
int i = 0; | |
while (true) | |
{ | |
yield return unchecked(++i); | |
} | |
} | |
public IEnumerable<Int32> TransparentIterate() | |
{ | |
return new TransparentEnumerable<Int32>(this.Iterate()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment