Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - jValdron

Pages: [1]
1
Network / [Solved] Sending binary files over TCP
« on: September 23, 2011, 06:14:59 pm »
Hi there,

My question might not be directly related to SFML, however, I use SFML for all my networking needs.

I'm currently trying to implement an update function that would require sending binary files over TCP. The problem is, with binary files, they always get corrupted. I can transfer text files correctly.

Here is an example of the code used in the server:
Code: [Select]

std::ifstream File(Filename, std::ios::in | std::ios::binary | std::ios::ate);

// Here I got some code to send the filename and size before sending packets

File.seekg(0, std::ios::beg);
EncryptedPacket ToSend; // EncryptedPacket is the same as the one used in the tutorials, for now
Buffer = new char[4096];
while(File.read(Buffer, sizeof(Buffer)))
{
    ToSend.Clear(); // Clear the previous packet
    ToSend << Buffer; // Add the 4096 bytes we just read to the packet
    Socket.Send(ToSend); // Send the packet
    std::fill(Buffer, Buffer + sizeof Buffer, 0); // Clear the buffer
}
File.close();
delete[] Buffer;


And here is the code used in the client to write the packets into a file:
Code: [Select]

OutputFile.open(Filename, std::ofstream::binary); // Open a new file for writing, filename comes from the server

while(true)
{
    EncryptedPacket Received;
    Client.Receive(Received);
    std::string Buffer;
    Received >> Buffer; // Read the buffer from the packet
    OutputFile.write(Buffer.c_str(), sizeof(Buffer)); // Write the buffer to the file
}


I've tried sending PNG, MP4 and TAR files, without success.

Here is an example of the original PNG file header:
http://www.zimagez.com/zimage/screenshot-230911-120853pm.php

And here is an example of the transfered PNG file header:
http://www.zimagez.com/zimage/screenshot-230911-121142pm.php

If you need a complete working example, I could write one.

Thanks for your help, I've tried a lot of different things...[/img]

2
System / [SOLVED] Thread acting differently than in SDL
« on: September 07, 2011, 08:15:28 pm »
Hello from Canada! (French Canadian, but nope not a "Quebecois")

I'm new to SFML, as of today. I'm porting my SDL application to SFML 1.6 for performance reasons. A simple 1080p video is rendering about 4x faster in SFML.

So, I got a problem with my threads... I'm not really an expert with C++ nor an expert with multi-threading.

The problem is that I create and launch a thread in the constructor of one of my classes, but my main loop (in which the object is created, hence the constructor called), doesn't display the RenderWindow until the thread is finished. Which is the reason I want to have threads.

So here is an example of my situation (not the exact code, but you'll understand):

main.cpp
Code: [Select]

new Scene();

while(App->IsRunning())
{
    ... my render stuff, that only starts after the thread is terminated (so after the FadeInThread method is done) ...    
}


Scene.cpp
Code: [Select]

Scene::Scene(sf::RenderWindow* Window, float Length)
{
   
    ... some code ...

    this->fadeIn();
   
}

void Scene::FadeIn()
{

    this->Opacity = 0;
    sf::Thread Thread(FadeInWrap, this);
    Thread.Launch();
    // SDL : SDL_CreateThread(FadeInWrap, this);

}

// This is a static method.
void FadeInWrap(void* Scn)
{
    ((Scene*) Scn)->FadeInThread();
}

void Scene::FadeInThread()
{
 
    float Delay = 0.007;
   
    while(this->Opacity < 255)
    {
        this->Opacity++;
        sf::Sleep(Delay);
        // SDL: SDL_Delay(Delay); (Delay was an int calculated differently)
    }
   
}


I know the static to non-static method is kinda weird, but only way I found out in SDL.

There's not much of a difference between a thread in SDL and in SFML, as far as I can see. Just don't understand why it doesn't continue the main execution while performing an operation in a thread.

Basically, if I launch my application, there's a 1.785 second delay because of the FadeInThread.

Could anyone help me out? I'm currently using the SDL_Thread, it works along SFML, but why import and use a huge library when SFML could do that for me.

Pages: [1]
anything