public void Clear() { port = -1; backLogNum = -1; if (socket != null) { socket.Close(); socket = null; } lock (stateLock) state = GServerState.Stopped; }
public void Start(int port, int backLogNum) { lock (stateLock) { if (state != GServerState.Stopped) { return; } state = GServerState.Starting; } lock (globalLock) { try { //소켓 설정 this.port = port; this.backLogNum = backLogNum; socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Bind(new IPEndPoint(IPAddress.Any, port)); socket.Listen(backLogNum); socket.NoDelay = !socketArgs.noDelay; byte[] optionBuffer = new byte[12]; Array.Copy(BitConverter.GetBytes(socketArgs.useKeepAlive ? 1 : 0), 0, optionBuffer, 0, sizeof(int)); Array.Copy(BitConverter.GetBytes(socketArgs.keepAliveTime), 0, optionBuffer, sizeof(int), sizeof(int)); Array.Copy(BitConverter.GetBytes(socketArgs.keepAliveInterval), 0, optionBuffer, sizeof(int) * 2, sizeof(int)); try { socket.IOControl(IOControlCode.KeepAliveValues, optionBuffer, null); } catch { socketArgs.useKeepAlive = false; } acceptThread = new Thread(AcceptClient); acceptThread.Start(); lock (stateLock) state = GServerState.Running; OnStarted(); } catch (Exception ex) { Clear(); OnFatalError(ex); } } }
private void CloseWork() { lock (stateLock) { if (state != GServerState.Running) { return; } state = GServerState.Stopping; } int nClientNum; lock (ClientSet) nClientNum = ClientSet.Count; lock (globalLock) { lock (ClientSet) { foreach (var sClient in ClientSet) { try { sClient.Shutdown(SocketShutdown.Both); } finally { sClient.Close(); } } ClientSet.Clear(); clientDataDict.Clear(); } port = -1; backLogNum = -1; if (socket != null) { socket.Close(); socket = null; } lock (stateLock) state = GServerState.Stopped; } }