internal async Task <byte[]> DevRequestSync(string serial, byte[] request, YGenericHub.RequestProgress progress,
                                                    object context)
        {
            YUSBDevice d = imm_devFromSerial(serial);

            return(await d.DevRequestSync(serial, request, progress, context));
        }
Beispiel #2
0
 internal WSRequest(string dbglabel, int tcpchanel, byte[] full_request, YGenericHub.RequestProgress progress, Object context)
 {
     _async       = false;
     _asyncId     = 0;
     _channel     = tcpchanel;
     _requestData = full_request;
     _dbglabel    = dbglabel;
     _progressCb  = progress;
     _progressCtx = context;
 }
Beispiel #3
0
 internal WSRequest(string dbglabel, int tcpchanel, byte asyncid, byte[] full_request)
 {
     _async       = true;
     _asyncId     = asyncid;
     _channel     = tcpchanel;
     _requestData = full_request;
     _dbglabel    = dbglabel;
     _progressCb  = null;
     _progressCtx = null;
 }
Beispiel #4
0
        internal async Task <byte[]> DevRequestSync(string serial, byte[] request, YGenericHub.RequestProgress progress, object context)
        {
            YUSBDevice d = imm_devFromSerial(serial);

            if (d == null)
            {
                throw new YAPI_Exception(YAPI.DEVICE_NOT_FOUND, "Device has been uplugged");
            }

            return(await d.DevRequestSync(serial, request, progress, context));
        }
 internal WSRequest(string dbglabel, int tcpchanel, byte asyncid, byte[] full_request, YGenericHub.RequestAsyncResult asyncResult, Object asyncContext)
 {
     _async         = true;
     _asyncId       = asyncid;
     _channel       = tcpchanel;
     _requestData   = full_request;
     _dbglabel      = dbglabel;
     _progressCb    = null;
     _progressCtx   = null;
     _asyncResultCb = asyncResult;
     _asyncCtx      = asyncContext;
 }
        public async Task <byte[]> DevRequestSync(string serial, byte[] request, YGenericHub.RequestProgress progress, object context)
        {
            if (_devState != DevState.StreamReadyReceived)
            {
                throw new YAPI_Exception(YAPI.IO_ERROR, "Device not ready");
            }

            await sendRequest(request, null, null);

            byte[] res = await _currentRequest.GetResponse();

            _currentRequest = null;
            return(res);
        }
        internal override async Task <byte[]> devRequestSync(YDevice device, string req_first_line, byte[] req_head_and_body, uint mstimeout, YGenericHub.RequestProgress progress, object context)
        {
            ulong start = YAPI.GetTickCount();

            if (!_httpReqByDev.ContainsKey(device))
            {
                _httpReqByDev[device] = new YHTTPRequest(_hub, "Device " + device.SerialNumber);
            }

            YHTTPRequest req = _httpReqByDev[device];

            byte[] result = await req.RequestSync(req_first_line, req_head_and_body, mstimeout);

            ulong stop = YAPI.GetTickCount();

            //Debug.WriteLine(string.Format("SyncRes on {0} took {1}ms", device.SerialNumber, stop - start));

            return(result);
        }
Beispiel #8
0
        internal override async Task <byte[]> devRequestSync(YDevice device, string req_first_line, byte[] req_head_and_body, uint mstimeout, YGenericHub.RequestProgress progress, object context)
        {
            if (mstimeout == 0)
            {
                // simulate a wait indefinitely
                mstimeout = 86400000; //24h
            }

            WSRequest wsRequest = await sendRequest(req_first_line, req_head_and_body, DEVICE_TCP_CHANNEL, false, progress, context);

            byte[] full_result = await wsRequest.getResponseBytes();

            return(imm_verifyHTTPheader(full_result));
        }
Beispiel #9
0
        private async Task <WSRequest> sendRequest(string req_first_line, byte[] req_head_and_body, int tcpchanel, bool async, YGenericHub.RequestProgress progress, Object context)
        {
            WSRequest request;
            string    debug = req_first_line.Trim();

            byte[] full_request;
            byte[] req_first_lineBytes;
            if (req_head_and_body == null)
            {
                req_first_line     += "\r\n\r\n";
                req_first_lineBytes = YAPI.DefaultEncoding.GetBytes(req_first_line);
                full_request        = req_first_lineBytes;
            }
            else
            {
                req_first_line     += "\r\n";
                req_first_lineBytes = YAPI.DefaultEncoding.GetBytes(req_first_line);
                full_request        = new byte[req_first_lineBytes.Length + req_head_and_body.Length];
                Array.Copy(req_first_lineBytes, 0, full_request, 0, req_first_lineBytes.Length);
                Array.Copy(req_head_and_body, 0, full_request, req_first_lineBytes.Length, req_head_and_body.Length);
            }

            ulong timeout = YAPI.GetTickCount() + WS_REQUEST_MAX_DURATION;

            while ((_connectionState != ConnectionState.CONNECTED && _connectionState != ConnectionState.DEAD))
            {
                if (timeout < YAPI.GetTickCount())
                {
                    if (_connectionState != ConnectionState.CONNECTED && _connectionState != ConnectionState.CONNECTING)
                    {
                        throw new YAPI_Exception(YAPI.IO_ERROR, "IO error with hub");
                    }
                    else
                    {
                        throw new YAPI_Exception(YAPI.TIMEOUT, "Last request did not finished correctly");
                    }
                }

                if (_connectionState == ConnectionState.DEAD)
                {
                    throw new YAPI_Exception(_session_errno, _session_error);
                }
            }

            if (async)
            {
                request = new WSRequest(debug, tcpchanel, _nextAsyncId++, full_request);
                if (_nextAsyncId >= 127)
                {
                    _nextAsyncId = 48;
                }
            }
            else
            {
                request = new WSRequest(debug, tcpchanel, full_request, progress, context);
            }

            _workingRequests[tcpchanel].Enqueue(request);

            //todo: handle timeout
            await processRequests(request);

            if (request.ErrorCode != YAPI.SUCCESS)
            {
                throw new YAPI_Exception(request.ErrorCode, request.ErrorMsg);
            }

            return(request);
        }
Beispiel #10
0
 /// <param name="device">            the device that will receive the request</param>
 /// <param name="req_first_line">    first line of request without space, HTTP1.1 or \r\n </param>
 /// <param name="req_head_and_body"> http headers with double \r\n followed by potential body </param>
 /// <param name="mstimeout">         number of milisecond allowed to the request to finish </param>
 /// <param name="progress">          progress callback (usefull for file upload)</param>
 /// <param name="context">           context for progress callback</param>
 /// <returns> return the raw response without the http header </returns>
 internal abstract Task <byte[]> devRequestSync(YDevice device, string req_first_line, byte[] req_head_and_body, uint mstimeout, YGenericHub.RequestProgress progress, object context);