예제 #1
0
        private void OnReceived(object unused, SocketAsyncEventArgs args)
        {
            var token = (SocketToken)args.UserToken;

            if (args.SocketError != SocketError.Success)
            {
                switch (token.socketState)
                {
                case SocketState.CLOSING:
                case SocketState.CLOSED:
                {
                    // already closing, ignore.
                    return;
                }

                default:
                {
                    switch (args.SocketError)
                    {
                    case SocketError.ConnectionReset:
                    {
                        DisquuunLogger.Log("ConnectionResetが出てる. " + " token.socketState:" + token.socketState, true);
                        break;
                    }

                    default:
                    {
                        DisquuunLogger.Log("onReceive default token.socketState:" + token.socketState + " error:" + args.SocketError, true);
                        break;
                    }
                    }

                    Disconnect();

                    var e1 = new Exception("receive status is not good.");
                    SocketClosed(this, "failed to receive.", e1);
                    return;
                }
                }
            }

            if (args.BytesTransferred == 0)
            {
                return;
            }

            var bytesAmount = args.BytesTransferred;

            // update token-dataLength as read completed.
            token.readableDataLength = token.readableDataLength + bytesAmount;

            if (token.isPipeline)
            {
                PipelineReceive(token);
            }
            else
            {
                LoopOrAsyncReceive(token);
            }
        }
예제 #2
0
        /*
         *              Core methods of Disquuun.
         */

        /**
         *              method for Sync execution of specific Disque command.
         *              DEPRECATED. only use for testing.
         */
        public override DisquuunResult[] DEPRECATED_Sync(DisqueCommand command, byte[] data)
        {
            try
            {
                socketToken.socket.Send(data);

                var currentLength = 0;
                var scanResult    = new DisquuunAPI.ScanResult(false);

                while (true)
                {
                    // waiting for head of transferring data or rest of data.
                    socketToken.socket.Receive(socketToken.receiveBuffer, currentLength, 1, SocketFlags.None);
                    currentLength = currentLength + 1;

                    var available      = socketToken.socket.Available;
                    var readableLength = currentLength + available;
                    {
                        if (socketToken.receiveBuffer.Length < readableLength)
                        {
                            // Disquuun.Log("サイズオーバーしてる " + socketToken.receiveBuffer.Length + " vs:" + readableLength);
                            Array.Resize(ref socketToken.receiveBuffer, readableLength);
                        }
                    }

                    // read rest.
                    socketToken.socket.Receive(socketToken.receiveBuffer, currentLength, available, SocketFlags.None);
                    currentLength = currentLength + available;

                    scanResult = DisquuunAPI.ScanBuffer(command, socketToken.receiveBuffer, 0, currentLength, socketId);
                    if (scanResult.isDone)
                    {
                        break;
                    }

                    // continue reading data from socket.
                    // if need, prepare for next 1 byte.
                    if (socketToken.receiveBuffer.Length == readableLength)
                    {
                        Array.Resize(ref socketToken.receiveBuffer, socketToken.receiveBuffer.Length + 1);
                    }
                }

                socketToken.socketState = SocketState.OPENED;
                return(scanResult.data);
            }
            catch (Exception e)
            {
                DisquuunLogger.Log("DEPRECATED_Sync error:" + e, true);
                throw e;
            }
        }
예제 #3
0
        public void Disconnect()
        {
            try
            {
                socketToken.socketState = SocketState.CLOSING;
                socketToken.socket.Shutdown(SocketShutdown.Both);

#if !UNITY_EDITOR
                {
                    socketToken.socket.Dispose();
                }
#endif

                socketToken.socketState = SocketState.CLOSED;
            }
            catch (Exception e)
            {
                DisquuunLogger.Log("Disconnect e:" + e.Message, true);
            }
            return;
        }
예제 #4
0
        private void OnSend(object unused, SocketAsyncEventArgs args)
        {
            switch (args.SocketError)
            {
            case SocketError.Success:
            {
                var token = args.UserToken as SocketToken;

                switch (token.socketState)
                {
                case SocketState.BUSY:
                {
                    token.socketState = SocketState.SENDED;
                    break;
                }

                case SocketState.RECEIVED:
                {
                    if (token.continuation)
                    {
                        // ready for next loop receive.
                        token.readableDataLength = 0;
                        token.receiveArgs.SetBuffer(token.receiveBuffer, 0, token.receiveBuffer.Length);
                        if (!token.socket.ReceiveAsync(token.receiveArgs))
                        {
                            OnReceived(token.socket, token.receiveArgs);
                        }

                        try
                        {
                            token.sendArgs.SetBuffer(token.currentSendingBytes, 0, token.currentSendingBytes.Length);
                        }
                        catch
                        {
                            // renew. potential error is exists and should avoid this error.
                            var sendArgs = new SocketAsyncEventArgs();
                            sendArgs.RemoteEndPoint = token.receiveArgs.RemoteEndPoint;
                            sendArgs.Completed     += new EventHandler <SocketAsyncEventArgs>(OnSend);
                            sendArgs.UserToken      = token;
                            token.sendArgs          = sendArgs;
                            token.sendArgs.SetBuffer(token.currentSendingBytes, 0, token.currentSendingBytes.Length);
                        }
                        if (!token.socket.SendAsync(token.sendArgs))
                        {
                            OnSend(token.socket, token.sendArgs);
                        }
                        return;
                    }

                    token.socketState = SocketState.OPENED;
                    SocketReloaded(this);
                    return;
                }
                }
                return;
            }

            default:
            {
                DisquuunLogger.Log("onsend error, " + args.SocketError, true);
                // if (Error != null) {
                //  var error = new Exception("send error:" + socketError.ToString());
                //  Error(error);
                // }
                return;
            }
            }
        }