Giter VIP home page Giter VIP logo

Comments (9)

plutooo avatar plutooo commented on August 10, 2024

Could you paste your test code? Thanks.

from libctru.

zeta0134 avatar zeta0134 commented on August 10, 2024

Sure, here's the contents of main.cpp.

#include <stdlib.h>
#include <string.h>

#include <string>

using namespace std;

#include <3ds.h>

Result http_download(httpcContext *context)//This error handling needs updated with proper text printing once ctrulib itself supports that.
{
    Result ret=0;
    u8* framebuf_top, *framebuf_bottom;
    u32 statuscode=0;
    u32 size=0, contentsize=0;
    u8 *buf;

    framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
    memset(framebuf_bottom, 0x40, 240*320*3);
    gfxFlushBuffers();
    gfxSwapBuffers();

    framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
    memset(framebuf_bottom, 0x40, 240*320*3);
    gfxFlushBuffers();
    gfxSwapBuffers();
    gspWaitForVBlank();

    ret = httpcBeginRequest(context);
    if(ret!=0)return ret;

    ret = httpcGetResponseStatusCode(context, &statuscode, 0);
    if(ret!=0)return ret;

    if(statuscode!=200)return -2;

    ret=httpcGetDownloadSizeState(context, NULL, &contentsize);
    if(ret!=0)return ret;

    buf = (u8*)malloc(contentsize);
    if(buf==NULL)return -1;
    memset(buf, 0, contentsize);

    framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
    memset(framebuf_bottom, 0xc0, 240*320*3);
    gfxFlushBuffers();
    gfxSwapBuffers();

    framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
    memset(framebuf_bottom, 0xc0, 240*320*3);
    gfxFlushBuffers();
    gfxSwapBuffers();
    gspWaitForVBlank();

    framebuf_top = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
    framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);

    ret = httpcDownloadData(context, buf, contentsize, NULL);
    if(ret!=0)
    {
        free(buf);
        return ret;
    }

    size = contentsize;
    if(size>(240*400*3))size = 240*400*3;

    memset(framebuf_bottom, 0xff, 240*320*3);
    memcpy(framebuf_top, buf + 8, size - 8); //skip past the width/height markers

    gfxFlushBuffers();
    gfxSwapBuffers();

    framebuf_top = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
    framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);

    memset(framebuf_bottom, 0xff, 240*320*3);
    memcpy(framebuf_top, buf + 8, size - 8);

    gfxFlushBuffers();
    gfxSwapBuffers();
    gspWaitForVBlank();

    free(buf);

    return 0;
}

void display_image(u8* data, int size) {
    u8* framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
    memcpy(framebuf_bottom, data + 8, size - 8); //skip past the width/height markers
}

void download_and_display_image(string url) {
    //debug_message("Attempting to download: " + url);
    Result ret=0;
    httpcContext context;
    ret = httpcOpenContext(&context, (char*)url.c_str(), 0);//Change this to your own URL.

    if(ret==0)
    {
        ret=http_download(&context);
        httpcCloseContext(&context);
    }
}

int main()
{
    // Initialize services
    srvInit();
    aptInit();
    hidInit(NULL);
    gfxInit();
    //gfxSet3D(true); // uncomment if using stereoscopic 3D
    httpcInit();

    string image_list[3] = {"http://darknovagames.com:1337/3ds/raw1.bin","http://darknovagames.com:1337/3ds/raw2.bin","http://darknovagames.com:1337/3ds/raw3.bin"};
    int current_image = 0;

    // Main loop
    while (aptMainLoop())
    {
        gspWaitForVBlank();
        hidScanInput();

        // Your code goes here

        u32 kDown = hidKeysDown();
        if (kDown & KEY_START) {
            break; // break in order to return to hbmenu
        }

        if (kDown & KEY_A) {
            //download an image!
            download_and_display_image(image_list[current_image]);
            current_image++;
            current_image = current_image % 3;
        }

        // Flush and swap framebuffers
        gfxFlushBuffers();
        gfxSwapBuffers();
    }

    // Exit services
    httpcExit();
    gfxExit();
    hidExit();
    aptExit();
    srvExit();
    return 0;
}

from libctru.

zeta0134 avatar zeta0134 commented on August 10, 2024

This is confirmed fixed on my end.

from libctru.

celcodioc avatar celcodioc commented on August 10, 2024

It seems to me that this has not been fixed yet: filiphsandstrom/DownloadMii-3DS#24

Looks like everyone is experiencing this; I get return code -660561894 (0xd8a0a01a) when calling httpcOpenContext the 9th+ time.

@zeta0134's modified example app doesn't work properly either for me, so I'm not sure what's going on. I'm using Ninjhax 1.1 (3DS XL, 9.2.0-20E), and the latest libctru commit (9fec42f).

from libctru.

WinterMute avatar WinterMute commented on August 10, 2024

Are you absolutely sure that you're using the latest libctru? @zeta0134's test code is working perfectly for me on the same setup with this patch (Ninjhax 1.1, 3DS XL, 9.2.0-20E). Is there some other version of that code that's failing?

from libctru.

plutooo avatar plutooo commented on August 10, 2024

Also can you check return value of every single httpcCloseContext? Maybe your code is leaking contexts.

from libctru.

plutooo avatar plutooo commented on August 10, 2024

Read my comment again.

from libctru.

celcodioc avatar celcodioc commented on August 10, 2024

It always closes with code 0 on my build, I assume that's what's supposed to happen?

Also, now that I'm getting the return codes from httpcCloseContext, for some reason the ninth httpcOpenContext doesn't even return anything; it simply freezes the 3DS (how that's related to storing that return value I have no idea). No problems running the apps in 3dmoo, except these messages when calling httpcCloseContext:

>> svcSendSyncRequest (0x32)
http_c_SyncRequest (line 217): CloseContext 0dadbb2a --stub--

>> svcCloseHandle (0x23)
svcCloseHandle: svcCloseHandle undefined for handle-type "service".

from libctru.

plutooo avatar plutooo commented on August 10, 2024

@filfat Me too. httpcCloseContext() can fail (return non-zero) and thus leak handles.

@celcodioc Yeah, that's correct. Sounds like you might be leaking something/somewhere else. Maybe you could modify 3dmoo to verify that all handles are closed/cleaned up correctly.

from libctru.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.