public void OnReloaded(DisquuunSocket reloadedSocket) { lock (lockObject) { if (stackSocket.IsQueued()) { if (reloadedSocket.IsChoosable()) { reloadedSocket.SetBusy(); var commandAndData = stackSocket.Dequeue(); switch (commandAndData.executeType) { case DisquuunExecuteType.ASYNC: { reloadedSocket.Async(commandAndData.commands, commandAndData.data, commandAndData.Callback); return; } case DisquuunExecuteType.LOOP: { reloadedSocket.Loop(commandAndData.commands, commandAndData.data, commandAndData.Callback); return; } case DisquuunExecuteType.PIPELINE: { reloadedSocket.Execute(commandAndData.commands, commandAndData.data, commandAndData.Callback); return; } } } } } }
private void OnSocketOpened(DisquuunSocket source, string socketId) { lock (lockObject) { if (connectionState != ConnectionState.OPENING) { return; } var availableSocketCount = 0; for (var i = 0; i < socketPool.Length; i++) { var socket = socketPool[i]; if (socket == null) { continue; } if (socket.IsChoosable()) { availableSocketCount++; } } if (availableSocketCount == minConnectionCount) { connectionState = ConnectionState.OPENED; ConnectionOpened(connectionId); } } }
public void OnReloaded(DisquuunSocket reloadedSocket) { lock (lockObject) { if (stackSocket.IsQueued()) { if (reloadedSocket.IsChoosable()) { reloadedSocket.SetBusy(); var commandAndData = stackSocket.Dequeue(); if (commandAndData.data.Length == 0) { Disquuun.Log("OnReloaded len = 0.", true); } switch (commandAndData.executeType) { case DisquuunExecuteType.ASYNC: { reloadedSocket.Async(commandAndData.command, commandAndData.data, commandAndData.Callback); return; } case DisquuunExecuteType.LOOP: { reloadedSocket.Loop(commandAndData.command, commandAndData.data, commandAndData.Callback); return; } } } } } }
private void OnSocketConnectionFailed(DisquuunSocket source, string info, Exception e) { UpdateState(); if (ConnectionFailed != null) { ConnectionFailed("OnSocketConnectionFailed:" + info, e); } }
private void OnSocketConnectionFailed(DisquuunSocket source, string info, Exception e) { connectionState = ConnectionState.OPENED_RECOVERING; if (ConnectionFailed != null) { ConnectionFailed("OnSocketConnectionFailed:" + info, e); } }
public Disquuun( string host, int port, long bufferSize, int minConnectionCount, Action <string> ConnectionOpenedAct = null, Action <string, Exception> ConnectionFailedAct = null ) { this.connectionId = Guid.NewGuid().ToString(); this.bufferSize = bufferSize; this.endPoint = new IPEndPoint(IPAddress.Parse(host), port); this.stackSocket = new StackSocket(); this.connectionState = ConnectionState.OPENING; /* * ConnectionOpened handler treats all connections are opened. */ if (ConnectionOpenedAct != null) { this.ConnectionOpened = ConnectionOpenedAct; } else { this.ConnectionOpened = conId => {} }; /* * ConnectionFailed handler only treats connection error. * * other runtime errors will emit in API handler. */ if (ConnectionFailedAct != null) { this.ConnectionFailed = ConnectionFailedAct; } else { this.ConnectionFailed = (info, e) => {} }; this.minConnectionCount = minConnectionCount; socketPool = new DisquuunSocket[minConnectionCount]; for (var i = 0; i < minConnectionCount; i++) { socketPool[i] = new DisquuunSocket(endPoint, bufferSize, OnSocketOpened, OnReloaded, OnSocketConnectionFailed); } }
private void OnSocketOpened(DisquuunSocket source, string socketId) { if (connectionState != ConnectionState.OPENING) { return; } var availableSocketCount = socketPool.AvailableSocketNum(); if (availableSocketCount == minConnectionCount) { connectionState = ConnectionState.OPENED; ConnectionOpened(connectionId); } }
private void AddNewSocket(int count) { for (var i = 0; i < count; i++) { var newSock = new DisquuunSocket( -1, (newSocket, index) => { // connected, add to hashtable. lock (poolLock) { sockets.Add(sockets.Count, newSocket); } }, this.OnReloaded, this.OnSocketConnectionFailed ); newSock.Connect(endPoint, bufferSize); } }
private void OnSocketOpened(DisquuunSocket source, int socketIndex) { if (!(bool)defaultConnectionIndexies[socketIndex]) { lock (lockObject) { defaultConnectionIndexies[socketIndex] = true; } for (var i = 0; i < defaultConnectionIndexies.Count; i++) { if (!(bool)defaultConnectionIndexies[i]) { return; } } } connectionState = ConnectionState.OPENED; ConnectionOpened(connectionId); }
public void OnReloaded(DisquuunSocket reloadedSocket) { // consume stacked command if need. lock (poolLock) { if (disquuunDataStack.IsQueued()) { if (reloadedSocket.IsAvailable()) { reloadedSocket.SetBusy(); var commandAndData = disquuunDataStack.Dequeue(); switch (commandAndData.executeType) { case DisquuunExecuteType.ASYNC: { reloadedSocket.Async(commandAndData.commands, commandAndData.data, commandAndData.Callback); return; } case DisquuunExecuteType.LOOP: { reloadedSocket.Loop(commandAndData.commands, commandAndData.data, commandAndData.Callback); return; } case DisquuunExecuteType.PIPELINE: { reloadedSocket.Execute(commandAndData.commands, commandAndData.data, commandAndData.Callback); return; } } } } } }