Guest User

Untitled

a guest
May 16th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <malloc.h>
  5. #include <ogcsys.h>
  6. #include <gccore.h>
  7. #include <network.h>
  8. #include <debug.h>
  9. #include <errno.h>
  10. #include <wiiuse/wpad.h>
  11.  
  12. static void *xfb = NULL;
  13. static GXRModeObj *rmode = NULL;
  14.  
  15. void *initialise();
  16. void *httpd (void *arg);
  17.  
  18. static  lwp_t httd_handle = (lwp_t)NULL;
  19. static  lwp_t hello_handle = (lwp_t)NULL;
  20.  
  21. syswd_t helloalarm;
  22. lwpq_t helloqueue;
  23.  
  24. void hellocb(unsigned a, void*b) {
  25.     LWP_ThreadBroadcast(helloqueue);
  26. }
  27.  
  28. void *hello(void*b) {
  29.     SYS_CreateAlarm(&helloalarm);
  30.     struct timespec p = {1, 0};
  31.     SYS_SetPeriodicAlarm(helloalarm, &p, &p, hellocb,0);
  32.     LWP_InitQueue(&helloqueue);
  33.  
  34.     while (true) {
  35.         printf("Hello!\n");
  36.         LWP_ThreadSleep(helloqueue);
  37.     }
  38.     return 0;
  39. }
  40.  
  41. //---------------------------------------------------------------------------------
  42. int main(int argc, char **argv) {
  43. //---------------------------------------------------------------------------------
  44.     s32 ret;
  45.  
  46.     char localip[16] = {0};
  47.     char gateway[16] = {0};
  48.     char netmask[16] = {0};
  49.  
  50.     xfb = initialise();
  51.  
  52.     printf ("\nlibogc network demo\n");
  53.     printf("Configuring network ...\n");
  54.  
  55.     // Configure the network interface
  56.     ret = if_config ( localip, netmask, gateway, TRUE, 20);
  57.     if (ret>=0) {
  58.         printf ("network configured, ip: %s, gw: %s, mask %s\n", localip, gateway, netmask);
  59.  
  60.         LWP_CreateThread(   &httd_handle,   /* thread handle */
  61.                             httpd,          /* code */
  62.                             localip,        /* arg pointer for thread */
  63.                             NULL,           /* stack base */
  64.                             16*1024,        /* stack size */
  65.                             50              /* thread priority */ );
  66.  
  67.         LWP_CreateThread(   &hello_handle,  /* thread handle */
  68.                             hello,          /* code */
  69.                             0,      /* arg pointer for thread */
  70.                             NULL,           /* stack base */
  71.                             16*1024,        /* stack size */
  72.                             16              /* thread priority */ );
  73.     } else {
  74.         printf ("network configuration failed!\n");
  75.     }
  76.  
  77.     while(1) {
  78.  
  79.         VIDEO_WaitVSync();
  80.         WPAD_ScanPads();
  81.  
  82.         int buttonsDown = WPAD_ButtonsDown(0);
  83.  
  84.         if (buttonsDown & WPAD_BUTTON_HOME) {
  85.             exit(0);
  86.         }
  87.     }
  88.  
  89.     return 0;
  90. }
  91.  
  92. const static char http_200[] = "HTTP/1.1 200 OK\r\n";
  93.  
  94. const static char indexdata[] = "<html> \
  95.                               <head><title>A test page</title></head> \
  96.                               <body> \
  97.                               This small test page has had %d hits. \
  98.                               </body> \
  99.                               </html>";
  100.  
  101. const static char http_html_hdr[] = "Content-type: text/html\r\n\r\n";
  102. const static char http_get_index[] = "GET / HTTP/1.1\r\n";
  103.  
  104. //---------------------------------------------------------------------------------
  105. void *httpd (void *arg) {
  106. //---------------------------------------------------------------------------------
  107.  
  108.     int sock, csock;
  109.     int ret;
  110.     u32 clientlen;
  111.     struct sockaddr_in client;
  112.     struct sockaddr_in server;
  113.     char temp[1026];
  114.     static int hits=0;
  115.  
  116.     clientlen = sizeof(client);
  117.  
  118.     sock = net_socket (AF_INET, SOCK_STREAM, IPPROTO_IP);
  119.  
  120.     if (sock == INVALID_SOCKET) {
  121.       printf ("Cannot create a socket!\n");
  122.     } else {
  123.  
  124.         memset (&server, 0, sizeof (server));
  125.         memset (&client, 0, sizeof (client));
  126.  
  127.         server.sin_family = AF_INET;
  128.         server.sin_port = htons (8080);
  129.         server.sin_addr.s_addr = INADDR_ANY;
  130.         ret = net_bind (sock, (struct sockaddr *) &server, sizeof (server));
  131.  
  132.         if ( ret ) {
  133.  
  134.             printf("Error %d binding socket!\n", ret);
  135.  
  136.         } else {
  137.  
  138.             if ( (ret = net_listen( sock, 5)) ) {
  139.  
  140.                 printf("Error %d listening!\n", ret);
  141.  
  142.             } else {
  143.                
  144.                 while(1) {
  145.                     struct pollsd sds = {sock, POLLIN};
  146.                     net_poll(&sds, 1, -1);
  147.                     csock = net_accept (sock, (struct sockaddr *) &client, &clientlen);
  148.  
  149.                     if ( csock < 0 ) {
  150.                         printf("Error connecting socket %d!\n", csock);
  151.                         while(1);
  152.                     }
  153.  
  154.                     printf("Connecting port %d from %s\n", client.sin_port, inet_ntoa(client.sin_addr));
  155.                     memset (temp, 0, 1026);
  156.                     ret = net_recv (csock, temp, 1024, 0);
  157.                     printf("Received %d bytes\n", ret);
  158.  
  159.                     if ( !strncmp( temp, http_get_index, strlen(http_get_index) ) ) {
  160.                         hits++;
  161.                         net_send(csock, http_200, strlen(http_200), 0);
  162.                         net_send(csock, http_html_hdr, strlen(http_html_hdr), 0);
  163.                         sprintf(temp, indexdata, hits);
  164.                         net_send(csock, temp, strlen(temp), 0);
  165.                     }
  166.  
  167.                     net_close (csock);
  168.  
  169.                 }
  170.             }
  171.         }
  172.     }
  173.     return NULL;
  174. }
  175.  
  176. //---------------------------------------------------------------------------------
  177. void *initialise() {
  178. //---------------------------------------------------------------------------------
  179.  
  180.     void *framebuffer;
  181.  
  182.     VIDEO_Init();
  183.     WPAD_Init();
  184.  
  185.     rmode = VIDEO_GetPreferredMode(NULL);
  186.     framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  187.     console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  188.  
  189.     VIDEO_Configure(rmode);
  190.     VIDEO_SetNextFramebuffer(framebuffer);
  191.     VIDEO_SetBlack(FALSE);
  192.     VIDEO_Flush();
  193.     VIDEO_WaitVSync();
  194.     if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  195.  
  196.     return framebuffer;
  197.  
  198. }
Add Comment
Please, Sign In to add comment