本文整理汇总了C#中Channel.CloseAfterEnqueue方法的典型用法代码示例。如果您正苦于以下问题:C# Channel.CloseAfterEnqueue方法的具体用法?C# Channel.CloseAfterEnqueue怎么用?C# Channel.CloseAfterEnqueue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Channel
的用法示例。
在下文中一共展示了Channel.CloseAfterEnqueue方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendRpcMessage
protected void SendRpcMessage(Address addr, string method,
string query, bool secure) {
System.DateTime sent = System.DateTime.Now;
#if !SVPN_NUNIT
string meth_call = RPCID + "." + method;
Channel q = new Channel();
q.CloseAfterEnqueue();
q.CloseEvent += delegate(object obj, EventArgs eargs) {
try {
RpcResult res = (RpcResult) q.Dequeue();
string result = (string) res.Result;
if(method == "Ping") {
System.DateTime recv = System.DateTime.Now;
_times = _times.InsertIntoNew(result, recv);
TimeSpan rtt = recv - sent;
_ssm.UpdateLatency(result, rtt.TotalMilliseconds);
}
ProtocolLog.WriteIf(SocialLog.SVPNLog,
String.Format("RPC {0} {1} {2}", addr.ToString(), method,
query));
} catch(Exception e) {
ProtocolLog.WriteIf(SocialLog.SVPNLog, e.ToString());
}
};
ISender sender;
if(!secure) {
sender = new AHExactSender(_node.Node, addr);
}
else {
sender = _node.Bso.GetSecureSender(addr);
}
_rpc.Invoke(sender, q, meth_call, _node.Address, query);
#endif
}
示例2: Main
//.........这里部分代码省略.........
int count = 0;
//
// Send a large number of packets as exact packets to random destinations
// and make sure exact routing is perfect.
//
for (int i = 0; i < network_size; i++) {
for (int j = 0; j < network_size; j++) {
int src_idx = i;
int dest_idx = j;
Node src_node = (Node) _sorted_node_list.GetByIndex(src_idx);
Node dest_node = (Node) _sorted_node_list.GetByIndex(dest_idx);
//Console.WriteLine("{0} -> {1}", src_idx, dest_idx);
Address dest_address = (Address) dest_node.Address;
ISender s = new AHExactSender(src_node, dest_address);
MemBlock p = dest_address.ToMemBlock();
s.Send(new CopyList(new PType(routing_test), p));
_sent++;
//System.Threading.Thread.Sleep(10);
s.Send(new CopyList(new PType(routing_test), p));
_sent++;
//System.Threading.Thread.Sleep(10);
}
}
//wait for 10 more seconds
Console.WriteLine("Going to sleep for 10 seconds.");
System.Threading.Thread.Sleep(10000);
Console.WriteLine("Final statistics");
lock(_class_lock) {
Console.WriteLine("Sent: {0}, Received: {1}, Wrongly routed: {2}",
_sent, _received, _wrongly_routed);
}
int missing_rpcs = 0;
int correct_rpcs = 0;
int incorrect_rpcs = 0;
Hashtable queue_to_address = new Hashtable();
for (int i = 0; i < network_size; i++) {
for (int j = 0; j < network_size; j++) {
int src_idx = i;
int dest_idx = j;
Node src_node = (Node) _sorted_node_list.GetByIndex(src_idx);
Node dest_node = (Node) _sorted_node_list.GetByIndex(dest_idx);
//Console.WriteLine("{0} -> {1}", src_idx, dest_idx);
Address dest_address = (Address) dest_node.Address;
ISender s = new AHExactSender(src_node, dest_address);
RpcManager rpc_man = src_node.Rpc;
Channel q = new Channel();
lock (_class_lock) {
queue_to_address[q] = dest_address;
}
q.CloseAfterEnqueue();
q.CloseEvent += delegate(object o, EventArgs cargs) {
lock(_class_lock) {
Channel qu = (Channel) o;
if (qu.Count == 0) {
missing_rpcs++;
}
queue_to_address.Remove(qu);
}
};
q.EnqueueEvent += delegate(object o, EventArgs cargs) {
lock(_class_lock) {
Channel qu = (Channel) o;
RpcResult rpc_reply = (RpcResult) qu.Peek();
byte []result = (byte[]) rpc_reply.Result;
Address target = new AHAddress(result);
if (target.Equals(queue_to_address[qu])) {
correct_rpcs++;
} else {
incorrect_rpcs++;
}
}
};
rpc_man.Invoke(s, q, "rpc_routing_test.GetIdentification", new object[]{});
}
}
//wait for 10 more seconds
while (true) {
int c = -1;
lock(_class_lock) {
c = incorrect_rpcs + missing_rpcs + correct_rpcs;
}
if (c < network_size*network_size) {
Console.WriteLine("Going to sleep for 10 seconds.");
System.Threading.Thread.Sleep(10000);
} else {
break;
}
}
Console.WriteLine("Final statistics");
Console.WriteLine("correct rpcs: {0}, incorrect rpcs: {1}, missing rpcs: {2}",
correct_rpcs, incorrect_rpcs, missing_rpcs);
System.Environment.Exit(1);
}
示例3: NextTransfer
/**
<summary>This is called by all completed transfers. It checks to see if
there is another value to transfer, transfers it if there is. Otherwise
it calls Done.</summary>
<param name="o">The Channel where the result of the previous transfer is
stored.</param>
<param name="eargs">Null</param>
*/
protected void NextTransfer(Object o, EventArgs eargs) {
Channel queue = (Channel) o;
queue.CloseEvent -= this.NextTransfer;
/* No point in dequeueing, if we've been interrupted, we most likely
* will get an exception!
*/
if(_interrupted) {
return;
}
try {
queue.Dequeue();
}
catch (Exception){
if(_con.Edge.IsClosed) {
_interrupted = true;
Done();
return;
}
}
/* An exception could be thrown if Done is called in another thread or
there are no more entries available. */
Entry ent = null;
try {
lock(_sync) {
if(_entry_enumerator.MoveNext()) {
ent = (Entry) _entry_enumerator.Current;
}
}
}
catch{}
if(ent != null) {
queue = new Channel();
queue.CloseAfterEnqueue();
queue.CloseEvent += this.NextTransfer;
int ttl = (int) (ent.EndTime - DateTime.UtcNow).TotalSeconds;
try {
_ts._rpc.Invoke(_con.Edge, queue, "dht.PutHandler", ent.Key, ent.Value, ttl, false);
}
catch {
if(_con.Edge.IsClosed) {
_interrupted = true;
}
}
}
else {
Done();
if(Dht.DhtLog.Enabled) {
ProtocolLog.Write(Dht.DhtLog, String.Format(
"Successfully complete transfer from {0} to {1}",
_ts._node.Address, _con.Address));
}
}
}
示例4: TransferState
/**
<summary>Begins a new transfer state to the neighbor connected via con.
</summary>
<param name="con">The connection to the neigbhor we will be transferring
data to.</param>
<param name="ts">The table server we're providing the transfer for. C#
does not allow sub-class objects to have access to their parent objects
member variables, so we pass it in like this.</param>
<remarks>
Step 1:
Get all the keys between me and my new neighbor.
Step 2:
Get all values for those keys, we copy so that we don't worry about
changes to the dht during this interaction. This is only a pointer
copy and since we let the OS deal with removing the contents of an
entry, we don't need to make copies of the actual entry.
Step 3:
Generate another list of keys of up to max parallel transfers and begin
transferring, that way we do not need to lock access to the entry
enumerator until non-constructor puts.
Step 4:
End constructor, results from puts, cause the next entry to be sent.
*/
public TransferState(Brunet.Connections.Connection con, TableServer ts) {
this._ts = ts;
this._con = con;
// Get all keys between me and my new neighbor
LinkedList<MemBlock> keys;
lock(_ts._sync) {
keys = _ts._data.GetKeysBetween((AHAddress) _ts._node.Address,
(AHAddress) _con.Address);
}
if(Dht.DhtLog.Enabled) {
ProtocolLog.Write(Dht.DhtLog, String.Format(
"Starting transfer from {0} to {1}",
_ts._node.Address, _con.Address));
}
int total_entries = 0;
/* Get all values for those keys, we copy so that we don't worry about
* changes to the dht during this interaction. This is only a pointer
* copy and since we let the OS deal with removing the contents of an
* entry, we don't need to make copies of the actual entry.
*/
foreach(MemBlock key in keys) {
Entry[] entries;
lock(_ts._sync) {
LinkedList<Entry> llentries = _ts._data.GetEntries(key);
if(llentries == null) {
continue;
}
entries = new Entry[llentries.Count];
total_entries += llentries.Count;
llentries.CopyTo(entries, 0);
}
key_entries.AddLast(entries);
}
if(Dht.DhtLog.Enabled) {
ProtocolLog.Write(Dht.DhtLog, String.Format(
"Total keys: {0}, total entries: {1}.",
key_entries.Count, total_entries));
}
_entry_enumerator = GetEntryEnumerator();
/* Here we generate another list of keys that we would like to
* this is done here, so that we can lock up the _entry_enumerator
* only during this stage and not during the RpcManager.Invoke
*/
LinkedList<Entry> local_entries = new LinkedList<Entry>();
for(int i = 0; i < MAX_PARALLEL_TRANSFERS && _entry_enumerator.MoveNext(); i++) {
local_entries.AddLast((Entry) _entry_enumerator.Current);
}
foreach(Entry ent in local_entries) {
Channel queue = new Channel();
queue.CloseAfterEnqueue();
queue.CloseEvent += this.NextTransfer;
int ttl = (int) (ent.EndTime - DateTime.UtcNow).TotalSeconds;
try {
_ts._rpc.Invoke(_con.Edge, queue, "dht.PutHandler", ent.Key, ent.Value, ttl, false);
}
catch {
if(_con.Edge.IsClosed) {
_interrupted = true;
Done();
break;
}
}
}
}
示例5: Put
/**
<summary>Called by a Dht client to store data here, this supports both Puts
and Creates by using the unique parameter.</summary>
<remarks>Puts will store the value no matter what, Creates will only store
the value if they are the first ones to store data on that key. This is
the first part of a Put operation. This calls PutHandler on itself and
the neighbor nearest to the key, which actually places the data into the
store. The result is returned to the client upon completion of the call
to the neighbor, if that fails the data is removed locally and an exception
is sent to the client indicating failure.</remarks>
<param name="key">The index to store the data at.</param>
<param name="value">Data to store at the key.</param>
<param name="ttl">Dht lease time in seconds</param>
<param name="unique">True if this should perform a create, false otherwise.
</param>
<param name="rs">The return state sent back to the RpcManager so that it
knows who to return the result to.</param>
<returns>True on success, thrown exception on failure</returns>
<exception cref="Exception">Data is too large, unresolved remote issues,
or the create is no successful</exception>
*/
public bool Put(MemBlock key, MemBlock value, int ttl, bool unique, object rs) {
if(value.Length > MAX_BYTES) {
throw new Exception(String.Format(
"Dht only supports storing data smaller than {0} bytes.", MAX_BYTES));
}
PutHandler(key, value, ttl, unique);
Channel remote_put = new Channel();
remote_put.CloseAfterEnqueue();
remote_put.CloseEvent += delegate(Object o, EventArgs eargs) {
object result = false;
try {
result = remote_put.Dequeue();
RpcResult rpcResult = (RpcResult) result;
result = rpcResult.Result;
if(result.GetType() != typeof(bool)) {
throw new Exception("Incompatible return value.");
}
else if(!(bool) result) {
throw new Exception("Unknown error!");
}
}
catch (Exception e) {
lock(_sync) {
_data.RemoveEntry(key, value);
}
result = new AdrException(-32602, e);
}
_rpc.SendResult(rs, result);
};
try {
Address key_address = new AHAddress(key);
ISender s = null;
var structs =
_node.ConnectionTable.GetConnections(ConnectionType.Structured);
// We need to forward this to the appropriate node!
if(((AHAddress)_node.Address).IsLeftOf((AHAddress) key_address)) {
var con = structs.GetRightNeighborOf(_node.Address);
s = con.Edge;
}
else {
var con = structs.GetLeftNeighborOf(_node.Address);
s = con.Edge;
}
_rpc.Invoke(s, remote_put, "dht.PutHandler", key, value, ttl, unique);
}
catch (Exception) {
lock(_sync) {
_data.RemoveEntry(key, value);
}
throw;
}
return true;
}
示例6: ComputePathLatencyTo
/**
* Recursive function to compute the latency of an Rpc call
* by accumulating measured latencies of individual hops.
* @param target address of the target
*/
public void ComputePathLatencyTo(AHAddress a, object req_state) {
/*
* First find the Connection pointing to the node closest to dest, if
* there is one closer than us
*/
ConnectionTable tab = _node.ConnectionTable;
ConnectionList structs = tab.GetConnections(ConnectionType.Structured);
Connection next_closest = structs.GetNearestTo((AHAddress) _node.Address, a);
//Okay, we have the next closest:
ListDictionary my_entry = new ListDictionary();
my_entry["node"] = _node.Address.ToString();
if( next_closest != null ) {
my_entry["next_latency"] = GetMeasuredLatency(next_closest.Address);
my_entry["next_contype"] = next_closest.ConType;
Channel result = new Channel();
//We only want one result, so close the queue after we get the first
result.CloseAfterEnqueue();
result.CloseEvent += delegate(object o, EventArgs args) {
Channel q = (Channel)o;
if( q.Count > 0 ) {
try {
RpcResult rres = (RpcResult)q.Dequeue();
IList l = (IList) rres.Result;
ArrayList results = new ArrayList( l.Count + 1);
results.Add(my_entry);
results.AddRange(l);
_rpc.SendResult(req_state, results);
}
catch(Exception x) {
string m = String.Format("<node>{0}</node> trying <connection>{1}</connection> got <exception>{2}</exception>", _node.Address, next_closest, x);
Exception nx = new Exception(m);
_rpc.SendResult(req_state, nx);
}
}
else {
//We got no results.
IList l = new ArrayList(1);
l.Add( my_entry );
_rpc.SendResult(req_state, l);
}
};
_rpc.Invoke(next_closest.Edge, result, "ncserver.ComputePathLatencyTo", a.ToString());
}
else {
//We are the end of the line, send the result:
ArrayList l = new ArrayList();
l.Add(my_entry);
_rpc.SendResult(req_state, l);
}
}