コード例 #1
0
ファイル: Disquuun.cs プロジェクト: sassembla/RolePlayingChat
        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;
                        }
                        }
                    }
                }
            }
        }
コード例 #2
0
        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);
                }
            }
        }
コード例 #3
0
        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;
                        }
                        }
                    }
                }
            }
        }
コード例 #4
0
ファイル: Disquuun.cs プロジェクト: sassembla/RolePlayingChat
 private void OnSocketConnectionFailed(DisquuunSocket source, string info, Exception e)
 {
     UpdateState();
     if (ConnectionFailed != null)
     {
         ConnectionFailed("OnSocketConnectionFailed:" + info, e);
     }
 }
コード例 #5
0
ファイル: Disquuun.cs プロジェクト: sassembla/Disquuun
        private void OnSocketConnectionFailed(DisquuunSocket source, string info, Exception e)
        {
            connectionState = ConnectionState.OPENED_RECOVERING;

            if (ConnectionFailed != null)
            {
                ConnectionFailed("OnSocketConnectionFailed:" + info, e);
            }
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
ファイル: Disquuun.cs プロジェクト: sassembla/RolePlayingChat
        private void OnSocketOpened(DisquuunSocket source, string socketId)
        {
            if (connectionState != ConnectionState.OPENING)
            {
                return;
            }
            var availableSocketCount = socketPool.AvailableSocketNum();

            if (availableSocketCount == minConnectionCount)
            {
                connectionState = ConnectionState.OPENED;
                ConnectionOpened(connectionId);
            }
        }
コード例 #8
0
        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);
            }
        }
コード例 #9
0
ファイル: Disquuun.cs プロジェクト: sassembla/Disquuun
        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);
        }
コード例 #10
0
        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;
                        }
                        }
                    }
                }
            }
        }