protected internal static string GetNetworkAddress(IMalockSocket malock) { if (malock == null) { return(null); } do { MalockSocket socket = malock as MalockSocket; if (socket != null) { EndPoint ep = socket.Address; if (ep != null) { return(ep.ToString()); } } } while (false); do { var socket = malock as global::malock.Server.MalockSocket; if (socket != null) { return(socket.Address); } } while (false); return(null); }
private bool PostSynHostEntryMessage(IMalockSocket socket, string key, string identity, HostEntry entry) { if (entry == null || string.IsNullOrEmpty(identity) || string.IsNullOrEmpty(key)) { return(false); } MalockNnsMessage message = new MalockNnsMessage(); message.Key = key; message.Identity = identity; message.Sequence = MalockMessage.NewId(); message.Command = MalockNnsMessage.SERVER_NNS_COMMAND_SYN_HOSTENTRYINFO; using (MemoryStream stream = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(stream)) { message.Serialize(bw); if (entry != null) { entry.Serialize(bw); } MalockMessage.TrySendMessage(socket, stream); } } return(true); }
protected internal static string GetEtherAddress(IMalockSocket malock) { if (malock == null) { return(null); } do { MalockSocket socket = malock as MalockSocket; if (socket != null) { IPAddress address = socket.GetLocalEtherAddress(); if (address != null) { return(address.ToString()); } } } while (false); do { var socket = malock as global::malock.Server.MalockSocket; if (socket != null) { IPAddress address = socket.GetRemoteEtherAddress(); if (address != null) { return(address.ToString()); } } } while (false); return(null); }
protected virtual IMalockSocket Select(IMalockSocket socket) { if (socket == null) { MalockSocket malock = null; for (int i = 0; i < sockets.Length; i++) { MalockSocket current = sockets[i]; if (current == null || !current.Available) { continue; } malock = current; } return(malock); } else { MalockSocket malock = null; if (sockets[0] == socket) { malock = sockets[1]; } else { malock = sockets[0]; } if (!malock.Available) { malock = null; } return(malock); } }
internal static bool TryInvokeAsync(IMalockSocket malock, MalockMessage message, int timeout, Action <int, MalockMessage, Stream> callback, ref Exception exception) { if (malock == null) { throw new ArgumentNullException("malock"); } if (message == null) { throw new ArgumentNullException("message"); } Mappable mapinfo = new Mappable() { State = callback, Tag = null, Timeout = timeout, Client = malock, }; if (!MalockMessage.RegisterToMap(message.Sequence, mapinfo)) { exception = new InvalidOperationException("An internal error cannot add a call to a rpc-task in the map table"); return(false); } return(TrySendMessage(malock, message, ref exception)); }
internal static void Abort(IMalockSocket malock) { if (malock == null) { throw new ArgumentNullException("malock"); } lock (msgmap) { foreach (KeyValuePair <int, Mappable> kv in msgmap) { Mappable map = kv.Value; if (map == null && map.Client != malock) { continue; } else { Mappable mv; msgmap.TryRemove(kv.Key, out mv); } var state = map.State; if (state != null) { state(Mappable.ERROR_ABORTED, null, null); } } } }
internal static bool TrySendMessage(IMalockSocket socket, Stream message) { if (socket == null || message == null) { return(false); } MemoryStream ms = (MemoryStream)message; return(socket.Send(ms.GetBuffer(), 0, Convert.ToInt32(ms.Position))); }
public virtual bool Send(byte[] buffer, int ofs, int len) { IMalockSocket socket = null; lock (this.syncobj) { socket = this.preferred; } if (socket == null) { return(false); } return(socket.Send(buffer, ofs, len)); }
internal static bool TrySendMessage(IMalockSocket socket, MalockMessage message, byte[] buffer, int ofs, int len) { if (socket == null || message == null) { return(false); } using (MemoryStream ms = (MemoryStream)message.Serialize()) { if (buffer != null) { ms.Write(buffer, ofs, len); } return(socket.Send(ms.GetBuffer(), 0, Convert.ToInt32(ms.Position))); } }
private void SocketConnected(object sender, EventArgs e) { MalockSocket currentsocket = (MalockSocket)sender; this.OnConnected(currentsocket); do { TimeSpan ts = TimeSpan.MinValue; bool readying = false; lock (this.syncobj) { ts = unchecked (DateTime.Now - this.firsttime); if (this.preferred == null) { this.preferred = currentsocket; } else if (ts.TotalMilliseconds <= BESTMAXCONNECTTIME) { if (sender == sockets[0]) { this.preferred = currentsocket; } } if (!this.IsReady) { if (this.AllIsAvailable() || currentsocket == sockets[0] || (ts.TotalMilliseconds > BESTMAXCONNECTTIME && this.Available)) { readying = true; this.IsReady = true; } } if (currentsocket == sockets[0]) // 它可能只是链接发生中断,但服务器本身是可靠的,所以不需要平滑到备用服务器。 { TimeSpan tv = unchecked (DateTime.Now - this.GetAbortTime(currentsocket)); if (tv.TotalMilliseconds < Malock.SmoothingInvokeTime) { this.preferred = currentsocket; } } currentsocket = (MalockSocket)this.preferred; } if (readying) { this.OnReady(currentsocket); } } while (false); }
protected MalockNetworkMessage(IMalockSocket socket, Stream stream, MalockMessage message) { if (socket == null) { throw new ArgumentNullException("socket"); } if (stream == null) { throw new ArgumentNullException("stream"); } if (message == null) { throw new ArgumentNullException("message"); } this.socket = socket; this.Stream = stream; this.Message = message; }
private void SocketAborted(object sender, EventArgs e) { MalockSocket currentsocket = (MalockSocket)sender; bool aborted = false; lock (this.syncobj) { if (this.preferred == sender) { aborted = true; this.preferred = this.Select(this.preferred); } } if (aborted) { this.OnAborted(currentsocket); } this.UpdateAbortTime(currentsocket, DateTime.Now); }
internal static bool TrySendMessage(IMalockSocket malock, MalockMessage message, ref Exception exception) { if (malock == null) { throw new ArgumentNullException("malock"); } if (message == null) { throw new ArgumentNullException("message"); } using (MemoryStream ms = (MemoryStream)message.Serialize()) { if (!malock.Send(ms.GetBuffer(), 0, unchecked ((int)ms.Position))) { exception = new InvalidOperationException("The malock send returned results do not match the expected"); return(false); } } return(true); }
private bool TryRegisterHostEntryMessage(IMalockSocket malock) { if (malock == null) { return(false); } MSG msg = new MSG(); msg.Command = MSG.SERVER_NDN_COMMAND_REGISTERHOSTENTRYINFO; msg.Sequence = MSG.NewId(); using (MemoryStream ms = new MemoryStream()) { msg.Serialize(ms); do { HostEntry entry = new HostEntry(); entry.Primary.Address = Ipep.ToIpepString(GetEtherAddress(malock), this.configuration.Port); entry.Standby.Address = configuration.StandbyNode; entry.Serialize(ms); } while (false); return(MSG.TrySendMessage(malock, ms)); } }
private void UpdateAbortTime(IMalockSocket socket, DateTime dateTime) { abortedtime.AddOrUpdate(socket, dateTime, (ok, ov) => dateTime); }
internal static bool TryInvokeAsync(IMalockSocket malock, MalockMessage message, int timeout, Action <int, MalockMessage, Stream> callback) { Exception exception = null; return(TryInvokeAsync(malock, message, timeout, callback, ref exception)); }
internal static bool TrySendMessage(IMalockSocket malock, MalockMessage message) { Exception exception = null; return(TrySendMessage(malock, message, ref exception)); }