private ClientSocketBase pop(long currentSecond) { while (System.Threading.Interlocked.CompareExchange(ref queueLock, 1, 0) != 0) { AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.TimerLinkQueuePop); } if (Head == null || Head.CheckTimeoutSeconds > currentSecond) { System.Threading.Interlocked.Exchange(ref queueLock, 0); return(null); } ClientSocketBase value = Head; if ((Head = Head.CheckNext) == null) { End = null; System.Threading.Interlocked.Exchange(ref queueLock, 0); } else { value.CheckNext = null; Head.CheckPrevious = null; System.Threading.Interlocked.Exchange(ref queueLock, 0); } return(value); }
/// <summary> /// TCP 服务客户端套接字数据发送 /// </summary> /// <param name="socket">TCP 服务客户端套接字</param> internal ClientSocketSenderBase(ClientSocketBase socket) : base(socket.Socket) { clientCreator = socket.ClientCreator; this.queueCommandSize = Math.Max(socket.ClientCreator.Attribute.GetQueueCommandSize, 1); OutputWaitHandle.Set(0); }
/// <summary> /// 客户端延迟日志 /// </summary> /// <param name="seconds">延迟秒数</param> /// <param name="clientSocket">TCP 服务客户端套接字</param> internal ClientLazyLog(int seconds, ClientSocketBase clientSocket) : base(seconds) { if (clientSocket != null) { this.clientSocket = clientSocket; AppendTaskArray(); } }
public bool IsSocketVersion(ref ClientSocketBase oldSocket) { if (oldSocket == null || CreateVersion > oldSocket.CreateVersion) { oldSocket = this; return(true); } return(false); }
/// <summary> /// 创建套接字 /// </summary> /// <param name="ipAddress"></param> /// <param name="port"></param> /// <param name="createVersion"></param> private void createSocket(IPAddress ipAddress, int port, int createVersion) { if (check(ipAddress, port)) { ClientSocketBase createSocket = CommandClient.CreateSocketByCreator(this, ipAddress, port, createVersion); if (createSocket != null) { CreateSocket = createSocket; return; } } CommandClient.SocketWait.Set(); }
/// <summary> /// 移除 TCP 服务客户端套接字 /// </summary> /// <param name="clientSocketCreator">TCP 服务客户端创建器</param> /// <param name="socket"></param> /// <param name="type"></param> internal void CallOnSocket(ClientSocketCreator clientSocketCreator, ClientSocketBase socket, ClientSocketEventParameter.EventType type) { if (onSocket != null) { Monitor.Enter(OnSocketLock); try { onSocket(new ClientSocketEventParameter(clientSocketCreator, socket, type)); } catch (Exception error) { Log.Add(AutoCSer.Log.LogType.Error, error); } finally { Monitor.Exit(OnSocketLock); } } }
internal void OnTimer() { ++currentSeconds; ClientSocketBase head = Head; if (head != null && head.CheckTimeoutSeconds <= currentSeconds && Interlocked.CompareExchange(ref isTimer, 1, 0) == 0) { do { if ((head = pop(currentSeconds)) == null) { System.Threading.Interlocked.Exchange(ref isTimer, 0); return; } head.Check(); }while (true); } }
/// <summary> /// 套接字验证通过以后的处理 /// </summary> /// <param name="socket"></param> /// <returns></returns> internal bool OnSocketVerifyMethod(TcpServer.ClientSocketBase socket) { do { ClientSocketBase oldSocket = this.Socket; if (oldSocket != null && oldSocket.CreateVersion > socket.CreateVersion) { return(false); } if (Interlocked.CompareExchange(ref this.Socket, socket, oldSocket) == oldSocket) { CommandClient.SocketWait.Set(); CommandClient.CallOnSocket(this, socket, ClientSocketEventParameter.EventType.SetSocket); return(true); } AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.TcpCommandClientSetSocket); }while (true); }
/// <summary> /// 重置心跳检测 /// </summary> /// <param name="value"></param> internal void Reset(ClientSocketBase value) { long newSeconds = currentSeconds + seconds; if (value.CheckTimeoutSeconds != newSeconds) { value.CheckTimeoutSeconds = newSeconds; while (System.Threading.Interlocked.CompareExchange(ref queueLock, 1, 0) != 0) { AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.TimerLinkQueuePush); } if (value != End) { if (value.CheckNext == null) { if (End == null) { Head = value; } else { End.CheckNext = value; value.CheckPrevious = End; } } else { if (value == Head) { (Head = value.CheckNext).CheckPrevious = null; value.CheckNext = null; } else { value.FreeCheckReset(); } End.CheckNext = value; value.CheckPrevious = End; } End = value; } System.Threading.Interlocked.Exchange(ref queueLock, 0); } }
internal void Push(ClientSocketBase value) { value.CheckTimeoutSeconds = currentSeconds + seconds; while (System.Threading.Interlocked.CompareExchange(ref queueLock, 1, 0) != 0) { AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.TimerLinkQueuePush); } if (End == null) { End = Head = value; System.Threading.Interlocked.Exchange(ref queueLock, 0); } else { End.CheckNext = value; value.CheckPrevious = End; End = value; System.Threading.Interlocked.Exchange(ref queueLock, 0); } }
/// <summary> /// 释放心跳检测 /// </summary> /// <param name="value"></param> internal void Free(ClientSocketBase value) { while (System.Threading.Interlocked.CompareExchange(ref queueLock, 1, 0) != 0) { AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.TimerLinkQueuePop); } if (value == Head) { if ((Head = value.CheckNext) == null) { End = null; System.Threading.Interlocked.Exchange(ref queueLock, 0); } else { value.CheckNext = null; Head.CheckPrevious = null; System.Threading.Interlocked.Exchange(ref queueLock, 0); } } else if (value == End) { End = value.CheckPrevious; value.CheckPrevious = null; End.CheckNext = null; System.Threading.Interlocked.Exchange(ref queueLock, 0); } else { if (value.CheckNext != null) { value.FreeCheck(); } System.Threading.Interlocked.Exchange(ref queueLock, 0); } }
/// <summary> /// 移除 TCP 服务客户端套接字 /// </summary> /// <param name="socket">TCP 服务客户端套接字</param> public abstract void OnDisposeSocket(ClientSocketBase socket);
/// <summary> /// TCP 客户端套接字事件参数 /// </summary> /// <param name="creator">TCP 服务客户端创建器</param> /// <param name="socket">TCP 服务客户端套接字</param> /// <param name="type">事件类型</param> internal ClientSocketEventParameter(ClientSocketCreator creator, ClientSocketBase socket, EventType type) { Creator = creator; Socket = socket; Type = type; }
/// <summary> /// TCP 服务客户端套接字数据发送 /// </summary> /// <param name="socket">TCP 服务客户端套接字</param> internal ClientSocketSenderBase(ClientSocketBase socket) : base(socket.Socket) { //ClientSocket = socket; OutputWaitHandle.Set(0); }
internal void FreeCheck() { FreeCheckReset(); CheckPrevious = null; }
internal void FreeCheckReset() { CheckPrevious.CheckNext = CheckNext; CheckNext.CheckPrevious = CheckPrevious; CheckNext = null; }
/// <summary> /// 客户端心跳检测定时 /// </summary> /// <param name="client"></param> /// <param name="seconds">超时秒数</param> internal ClientCheckTimer(ClientSocketBase client, int seconds) : base(AutoCSer.Threading.SecondTimer.InternalTaskArray, seconds, Threading.SecondTimerThreadMode.Synchronous, Threading.SecondTimerKeepMode.After, seconds) { this.client = client; }
internal void CallOnSetSocketOnly(ClientSocketCreator clientSocketCreator, ClientSocketBase socket) { onSocket(new ClientSocketEventParameter(clientSocketCreator, socket, ClientSocketEventParameter.EventType.SetSocket)); }