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 DateTime GetAbortTime(MalockSocket socket) { DateTime dateTime; this.abortedtime.TryGetValue(socket, out dateTime); return(dateTime); }
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); } }
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); }
void IMalockSocket.Abort() { for (int i = 0; i < sockets.Length; i++) { MalockSocket socket = sockets[i]; socket.Abort(); } }
internal MalockSocketStream(MalockSocket socket, Stream stream) { if (socket == null) { throw new ArgumentNullException("socket"); } if (stream == null) { throw new ArgumentNullException("stream"); } this.Stream = stream; this.Socket = socket; }
protected virtual void SocketReceived(object sender, MalockSocketStream e) { TMessage message = default(TMessage); if (!this.TryDeserializeMessage(e, out message)) { MalockSocket socket = e.Socket; socket.Abort(); } else if (message != null) { this.OnMessage(new MalockNetworkMessage <TMessage>(this, e.Socket, e.Stream, message)); } }
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 bool AllIsAvailable() { bool success = false; for (int i = 0; i < sockets.Length; i++) { MalockSocket socket = sockets[i]; if (!socket.Available) { return(false); } else { success = true; } } return(success); }
public SocketWorkContext(MalockSocket malock) { if (malock == null) { throw new ArgumentNullException("malock"); } this.malock = malock; using (MemoryStream ms = new MemoryStream()) { BinaryWriter bw = new BinaryWriter(ms); bw.Write(Convert.ToByte(malock.LinkMode)); bw.Write(Convert.ToUInt16(malock.listenport)); MalockMessage.WriteStringToStream(bw, malock.identity); this.connectauthbuf = ms.ToArray(); } this.auxiliary = new MalockSocketAuxiliary(this.syncobj, this.OnError, this.OnReceive); this.connectcallback = this.StartConnect; }
public MalockMixClient <TMessage> Run() { lock (this.syncobj) { if (this.firsttime == DateTime.MinValue && !this.Available) { for (int i = 0; i < sockets.Length; i++) { MalockSocket socket = sockets[i]; socket.Run(); } if (!this.IsReady) { this.firsttime = DateTime.Now; var waitforconn = Malock.NewTimer(); waitforconn.Tick += (sender, e) => { MalockSocket socket = null; bool readying = false; lock (this.syncobj) { if (!this.IsReady) { socket = (MalockSocket)this.preferred; if (socket != null && this.Available) { readying = true; this.IsReady = true; } waitforconn.Stop(); } } if (readying) { this.OnReady(socket); } }; waitforconn.Interval = BESTMAXCONNECTTIME; waitforconn.Start(); } } return(this); } }
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 MalockSocket(string identity, EndPoint address, int listenport, int linkMode) { if (address == null) { throw new ArgumentNullException("The address of the connection may not be null"); } if (string.IsNullOrEmpty(identity)) { throw new ArgumentNullException("The identity is absolutely not nullable and it must be specified that each link is unique before you can"); } if (!MalockSocket.LAN(address)) { throw new ArgumentOutOfRangeException("Malock does not accept IPv4 addresses for not ABC class LAN segments"); } this.identity = identity; this.address = address; this.listenport = listenport; this.LinkMode = linkMode; }
private void InternalAbort(bool notifyEvent) { bool lastisconnected = false; lock (this.syncobj) { lastisconnected = this.currentconnected; this.currentconnected = false; if (this.socket != null) { MalockSocket.Close(socket); } this.socket = null; } if (notifyEvent && lastisconnected) { this.OnAborted(); } }
/// <summary> /// 创建一个双机热备的 malock 客户端 /// </summary> /// <param name="identity">代表客户端唯一的身份标识</param> /// <param name="mainuseNode">主用服务器主机地址</param> /// <param name="standbyNode">备用服务器主机地址</param> /// <param name="state">自定标记的状态对象</param> internal MalockMixClient(string identity, string mainuseNode, string standbyNode, object state) { if (identity == null) { throw new ArgumentNullException("identity"); } if (identity.Length <= 0) { throw new ArgumentOutOfRangeException("identity"); } if (mainuseNode == null) { throw new ArgumentNullException("mainuseNode"); } if (mainuseNode.Length <= 0) { throw new ArgumentOutOfRangeException("mainuseNode"); } if (standbyNode == null) { throw new ArgumentNullException("standbyNode"); } if (standbyNode.Length <= 0) { throw new ArgumentOutOfRangeException("standbyNode"); } this.Identity = identity; this.state = state; int linkMode = this.GetLinkMode(); int listenport = this.GetListenPort(); sockets[0] = new MalockSocket(identity, mainuseNode, listenport, linkMode); sockets[1] = new MalockSocket(identity, standbyNode, listenport, linkMode); for (int i = 0; i < sockets.Length; i++) { MalockSocket socket = sockets[i]; socket.Aborted += this.SocketAborted; socket.Connected += this.SocketConnected; socket.Received += this.SocketReceived; } this.BindEventToMessage(); }