Created
November 26, 2010 06:53
-
-
Save noqisofon/716365 to your computer and use it in GitHub Desktop.
へにょへにょな C# リモートテスト。
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Runtime.Remoting.Lifetime; | |
namespace halfmoon.transport.ipc { | |
/// <summary> | |
/// | |
/// </summary> | |
/// <summary> | |
/// 共有オブジェクト。 | |
/// </summary> | |
public class RemoteObject : MarshalByRefObject { | |
/// <summary> | |
/// 情報を引き渡すイベント引数クラス。 | |
/// </summary> | |
public class SendMessageEventArg : EventArgs { | |
/// <summary> | |
/// メッセージのタイトル。 | |
/// </summary> | |
private string title_ = ""; | |
/// <summary> | |
/// メッセージの本文。 | |
/// </summary> | |
private string fulltext_ = ""; | |
/// <summary> | |
/// | |
/// </summary> | |
public string Title { | |
get { return title_; } | |
set { title_ = value; } | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
public string FullText { | |
get { return fulltext_; } | |
set { fulltext_ = value; } | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="tmpMode"></param> | |
/// <param name="tmpfName"></param> | |
public SendMessageEventArg(string tmp_title, string tmp_fulltext) { | |
title_ = tmp_title; | |
fulltext_ = tmp_fulltext; | |
} | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="e"></param> | |
public delegate void SendMessageEventHandler(SendMessageEventArg e); | |
/// <summary> | |
/// | |
/// </summary> | |
public event SendMessageEventHandler onMessageSend; | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="tmpmode"></param> | |
/// <param name="tmpfname"></param> | |
public void send(string tmp_title, string tmp_fulltext) { | |
if ( onMessageSend != null ) { | |
onMessageSend( new SendMessageEventArg( tmp_title, tmp_fulltext ) ); | |
} | |
} | |
/// <summary> | |
/// 対象のインスタンスの有効期間ポリシーを制御する、有効期間サービス オブジェクトを取得します。 | |
/// </summary> | |
/// <returns></returns> | |
public override object InitializeLifetimeService() { | |
ILease base_lease = (ILease)base.InitializeLifetimeService(); | |
if ( base_lease != null && base_lease.CurrentState == LeaseState.Initial ) { | |
base_lease.InitialLeaseTime = TimeSpan.FromSeconds( 15 ); | |
base_lease.RenewOnCallTime = TimeSpan.FromSeconds( 15 ); | |
base_lease.SponsorshipTimeout = TimeSpan.FromMinutes( 2 ); | |
} | |
return base_lease; | |
} | |
} | |
} |
This file contains hidden or 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
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Text; | |
using System.Security.Principal; | |
using System.Runtime.Remoting; | |
using System.Runtime.Remoting.Channels; | |
using System.Runtime.Remoting.Channels.Ipc; | |
using System.Windows.Forms; | |
namespace halfmoon.transport.ipc.server { | |
/// <summary> | |
/// | |
/// </summary> | |
public partial class RemoteServerForm : Form { | |
/// <summary> | |
/// | |
/// </summary> | |
private RemoteObject remote_; | |
/// <summary> | |
/// | |
/// </summary> | |
public RemoteServerForm() { | |
InitializeComponent(); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="ergs"></param> | |
private void RemoteServerForm_Load(object sender, EventArgs ergs) { | |
IDictionary channel_config = new Hashtable(); | |
// チャンネルの名前を指定します。。 | |
channel_config["name"] = string.Empty; | |
// ポート名を指定します。 | |
channel_config["portName"] = "remote"; | |
// セキュリティ偽装レベルを指定します。 | |
channel_config["tokenImpersonationLevel"] = TokenImpersonationLevel.Impersonation; | |
// 偽装します。 | |
channel_config["impersonation"] = true; | |
// セキュア | |
channel_config["secure"] = true; | |
// IPC チャンネルを作成します。 | |
IpcServerChannel channel = new IpcServerChannel( channel_config, null ); | |
// チャンネルを登録します。 | |
ChannelServices.RegisterChannel( channel, true ); | |
// リモートオブジェクトの型を登録します。 | |
RemotingConfiguration.RegisterWellKnownServiceType( typeof( RemoteObject ), | |
"ipc://remote/message", | |
WellKnownObjectMode.Singleton | |
); | |
lblUri.Text = channel.GetChannelUri(); | |
this.remote_ = new RemoteObject(); | |
this.remote_.onMessageSend += new RemoteObject.SendMessageEventHandler( remote_onMessageSend ); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="smergs"></param> | |
void remote_onMessageSend(RemoteObject.SendMessageEventArg smergs) { | |
lbResult.Items.Add( smergs.Title ); | |
} | |
} | |
} |
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Text; | |
using System.Runtime.Remoting; | |
using System.Runtime.Remoting.Channels; | |
using System.Runtime.Remoting.Channels.Ipc; | |
using System.Runtime.Remoting.Lifetime; | |
using System.Windows.Forms; | |
namespace halfmoon.transport.ipc.client { | |
using halfmoon.transport.ipc; | |
/// <summary> | |
/// | |
/// </summary> | |
public partial class TransportClientForm : Form { | |
/// <summary> | |
/// | |
/// </summary> | |
RemoteObject remote_ = null; | |
/// <summary> | |
/// | |
/// </summary> | |
public TransportClientForm() { | |
InitializeComponent(); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="ergs"></param> | |
private void TransportClientForm_Load(object sender, EventArgs ergs) { | |
// IPC チャンネルを作成します。 | |
IpcClientChannel channel = new IpcClientChannel(); | |
// チャンネルを登録します。 | |
ChannelServices.RegisterChannel( channel, true ); | |
// リモートオブジェクトの型を登録します。 | |
RemotingConfiguration.RegisterWellKnownClientType( typeof( RemoteObject ), "ipc://remote/message" ); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="ergs"></param> | |
private void TransportClientForm_Shown(object sender, EventArgs ergs) { | |
// | |
this.remote_ = (RemoteObject)Activator.GetObject( typeof( RemoteObject ), "ipc://remote/message" ); | |
// 有効期間を 5 秒にします。 | |
ILease lease = (ILease)RemotingServices.GetLifetimeService( this.remote_ ); | |
if ( lease != null ) | |
lease.Register( new ClientSponsor( TimeSpan.FromSeconds( 5 ) ) ); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="ergs"></param> | |
private void btnSend_Click(object sender, EventArgs ergs) { | |
if ( tbTitle.Text == string.Empty && tbFullText.Text == string.Empty ) | |
return; | |
this.remote_.send( tbTitle.Text, tbFullText.Text ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment