diff --git a/source/Network/tcp.cpp b/source/Network/tcp.cpp
index efaecac..00eac47 100644
--- a/source/Network/tcp.cpp
+++ b/source/Network/tcp.cpp
@@ -49,15 +49,15 @@ int oport(unsigned short portnum)
// wait for a connection to occur on a socket created with oport()
int get_connection(int s, struct sockaddr_in *sa)
-{
+{
int t; /* socket of connection */
// struct sockaddr_in sa;
sa->sin_len = 8;
sa->sin_family = AF_INET;
u32 buflen = 8;
t = net_accept(s,(struct sockaddr *)sa, &buflen);
-
-/*
+
+/*
debug("Incoming connection");
printf("Incoming connection from %d.%d.%d.%d\n",
(sa->sin_addr.s_addr >> 24) & 0xFF,
@@ -118,8 +118,8 @@ static void * tcp_callback(void *arg)
u8 *bfr[READ_SIZE];
bool compress = false;
struct sockaddr_in addr;
-
- redo:
+
+ redo:
s32 listen = oport(4299);
while(1)
{
@@ -137,10 +137,10 @@ static void * tcp_callback(void *arg)
if(client > 0)
{
// client connected
-
+
GuiImage * progressImg = new GuiImage(new GuiImageData(Theme.progress));
progressImg->SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
-
+
GXColor ImgColor[4];
ImgColor[0] = (GXColor){Theme.progressbar_color1_1, Theme.progressbar_color1_2, Theme.progressbar_color1_3, 200}; // oben links
@@ -148,19 +148,20 @@ static void * tcp_callback(void *arg)
ImgColor[2] = (GXColor){Theme.progressbar_color2_1, Theme.progressbar_color2_2, Theme.progressbar_color2_3, 200}; // unten rechts
ImgColor[3] = ImgColor[2]; // unten links
-
+
GuiImage * progressbarImg = new GuiImage(0, 38, (GXColor *) &ImgColor);
progressbarImg->SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE);
progressbarImg->SetPosition((screenwidth - progressImg->GetWidth()) /2 +36, 2);
-
+
GuiText * PercentTxt = new GuiText("0%", 20, (GXColor){Theme.text_1, Theme.text_2, Theme.text_3, 255});
PercentTxt->SetAlignment(ALIGN_RIGHT, ALIGN_MIDDLE);
PercentTxt->SetPosition(-((screenwidth - progressImg->GetWidth()) /2 +36), 38);
-
+
float Percent = 0.0f;
int temp = read_data(client, (char *)&read, 4);
- if(temp < 0)
+ int temp2 = usb_recvbuffer_safe_ex(1, (char *)&read, 4, 1);
+ if(temp < 0 && temp2 < 0)
{
// printf("read_data() error while reading check\n");
net_close(client);
@@ -176,53 +177,86 @@ static void * tcp_callback(void *arg)
mainWindow->Append(PercentTxt);
ResumeGui();
}
-
+
if(read == 1212242008) // 1212242008 -> 48415858 -> HAXX -> wiiload
{
compress = true;
// printf("HAXX\n");
-
- read_data(client, (char *)&read, 4);
+
// int WIILOAD_VERSION_MAYOR = (u8)(((u16)(read >> 16)) >> 8);
// int WIILOAD_VERSION_MINOR = (u8)(((u16)(read >> 16)) & 0xFF);
// int a = (u8)(((u16)(read & 0xFFFF)) >> 8);
// int b = (u8)(((u16)(read & 0xFFFF)) & 0xFF);
-
+
// printf("wiiload v%x.%x\n", WIILOAD_VERSION_MAYOR, WIILOAD_VERSION_MINOR);
// printf("args %x08\n", read);
// printf("args a=%x b=%x\n", a, b);
-
- read_data(client, (char *)&size, 4);
- read_data(client, (char *)&uncfilesize, 4);
+
+ if(temp < 0)
+ {
+ usb_recvbuffer_safe_ex(1, (char *)&read, 4, 1);
+ usb_recvbuffer_safe_ex(1, (char *)&size, 4, 1);
+ usb_recvbuffer_safe_ex(1, (char *)&uncfilesize, 4, 1);
+ }
+ else
+ {
+ read_data(client, (char *)&read, 4);
+ read_data(client, (char *)&size, 4);
+ read_data(client, (char *)&uncfilesize, 4);
+ }
+
}
else
size = read;
-
+
offset = 0;
- while(offset < size && (read = read_data(client, (char *)bfr, (size - offset) > READ_SIZE ? READ_SIZE : (size - offset))) > 0)
+
+ if(temp < 0)
{
- memcpy(data + offset, bfr, READ_SIZE);
- offset += read;
-
- Percent = 100.0f * offset/size;
- progressbarImg->SetSize(Percent*3.27f, 38);
-
- char buffer[6];
- sprintf(buffer, "%i %%", (int)Percent);
- PercentTxt->SetText(buffer);
+
+ while(offset < size && (read = usb_recvbuffer_safe_ex(1, (char *)bfr, (size - offset) > READ_SIZE ? READ_SIZE : (size - offset), 1)) > 0)
+ {
+ memcpy(data + offset, bfr, READ_SIZE);
+ offset += read;
+
+ Percent = 100.0f * offset/size;
+ progressbarImg->SetSize(Percent*3.27f, 38);
+
+ char buffer[6];
+ sprintf(buffer, "%i %%", (int)Percent);
+ PercentTxt->SetText(buffer);
+ }
+
+
}
+ else
+ {
+ while(offset < size && (read = read_data(client, (char *)bfr, (size - offset) > READ_SIZE ? READ_SIZE : (size - offset))) > 0)
+ {
+ memcpy(data + offset, bfr, READ_SIZE);
+ offset += read;
+
+ Percent = 100.0f * offset/size;
+ progressbarImg->SetSize(Percent*3.27f, 38);
+
+ char buffer[6];
+ sprintf(buffer, "%i %%", (int)Percent);
+ PercentTxt->SetText(buffer);
+ }
+ }
+
usleep(100000);
net_close(client);
net_close(listen);
-
+
if(compress)
{
u8 *zdata = (u8 *) malloc(uncfilesize);
if(!zdata)
return NULL;
-
+
uLongf zdatalen = uncfilesize;
-
+
int res = uncompress (zdata, &zdatalen, data, (uLongf)size);
if (res != Z_OK)
@@ -238,13 +272,13 @@ static void * tcp_callback(void *arg)
// free(zdata);
}
}
-
+
CopyHomebrewMemory(data, 0, size);
if(data)
free(data);
// if(zdata)
// free(zdata);
-
+
mainWindow->Remove(PercentTxt);
mainWindow->Remove(progressbarImg);
mainWindow->Remove(progressImg);