Advertisement
PauSix

Untitled

Feb 26th, 2017
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.53 KB | None | 0 0
  1. /*
  2.  * PSP Software Development Kit - http://www.pspdev.org
  3.  * -----------------------------------------------------------------------
  4.  * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
  5.  *
  6.  * main.c - Simple elf based network example.
  7.  *
  8.  * Copyright (c) 2005 James F <[email protected]>
  9.  * Some small parts (c) 2005 PSPPet
  10.  *
  11.  * $Id: main.c 2435 2008-10-15 18:05:02Z iwn $
  12.  * $HeadURL: svn://svn.ps2dev.org/psp/trunk/pspsdk/src/samples/net/simple/main.c $
  13.  */
  14. #include <pspkernel.h>
  15. #include <pspdebug.h>
  16. #include <pspsdk.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <pspnet.h>
  21. #include <pspnet_inet.h>
  22. #include <pspnet_apctl.h>
  23. #include <pspnet_resolver.h>
  24. #include <netinet/in.h>
  25. #include <arpa/inet.h>
  26. #include <sys/select.h>
  27. #include <errno.h>
  28.  
  29. #define printf pspDebugScreenPrintf
  30.  
  31. #define MODULE_NAME "NetSample"
  32. #define HELLO_MSG   "Hello there. Type away.\r\n"
  33.  
  34. PSP_MODULE_INFO(MODULE_NAME, 0x1000, 1, 1);
  35. PSP_MAIN_THREAD_ATTR(0);
  36.  
  37. /* Exit callback */
  38. int exit_callback(int arg1, int arg2, void *common)
  39. {
  40.     sceKernelExitGame();
  41.     return 0;
  42. }
  43.  
  44. /* Callback thread */
  45. int CallbackThread(SceSize args, void *argp)
  46. {
  47.     int cbid;
  48.  
  49.     cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
  50.     sceKernelRegisterExitCallback(cbid);
  51.     sceKernelSleepThreadCB();
  52.  
  53.     return 0;
  54. }
  55.  
  56. /* Sets up the callback thread and returns its thread id */
  57. int SetupCallbacks(void)
  58. {
  59.     int thid = 0;
  60.  
  61.     thid = sceKernelCreateThread("update_thread", CallbackThread,
  62.                      0x11, 0xFA0, PSP_THREAD_ATTR_USER, 0);
  63.     if(thid >= 0)
  64.     {
  65.         sceKernelStartThread(thid, 0, 0);
  66.     }
  67.  
  68.     return thid;
  69. }
  70.  
  71. #define SERVER_PORT 23
  72.  
  73. int make_socket(uint16_t port)
  74. {
  75.     int sock;
  76.     int ret;
  77.     struct sockaddr_in name;
  78.  
  79.     sock = socket(PF_INET, SOCK_STREAM, 0);
  80.     if(sock < 0)
  81.     {
  82.         return -1;
  83.     }
  84.  
  85.     name.sin_family = AF_INET;
  86.     name.sin_port = htons(port);
  87.     name.sin_addr.s_addr = htonl(INADDR_ANY);
  88.     ret = bind(sock, (struct sockaddr *) &name, sizeof(name));
  89.     if(ret < 0)
  90.     {
  91.         return -1;
  92.     }
  93.  
  94.     return sock;
  95. }
  96.  
  97. /* Start a simple tcp echo server */
  98. void start_server(const char *szIpAddr)
  99. {
  100.     int ret;
  101.     int sock;
  102.     int new = -1;
  103.     struct sockaddr_in client;
  104.     size_t size;
  105.     int readbytes;
  106.     char data[1024];
  107.     fd_set set;
  108.     fd_set setsave;
  109.  
  110.     /* Create a socket for listening */
  111.     sock = make_socket(SERVER_PORT);
  112.     if(sock < 0)
  113.     {
  114.         printf("Error creating server socket\n");
  115.         return;
  116.     }
  117.  
  118.     ret = listen(sock, 1);
  119.     if(ret < 0)
  120.     {
  121.         printf("Error calling listen\n");
  122.         return;
  123.     }
  124.  
  125.     printf("Listening for connections ip %s port %d\n", szIpAddr, SERVER_PORT);
  126.  
  127.     FD_ZERO(&set);
  128.     FD_SET(sock, &set);
  129.     setsave = set;
  130.  
  131.     while(1)
  132.     {
  133.         int i;
  134.         set = setsave;
  135.         if(select(FD_SETSIZE, &set, NULL, NULL, NULL) < 0)
  136.         {
  137.             printf("select error\n");
  138.             return;
  139.         }
  140.  
  141.         for(i = 0; i < FD_SETSIZE; i++)
  142.         {
  143.             if(FD_ISSET(i, &set))
  144.             {
  145.                 int val = i;
  146.  
  147.                 if(val == sock)
  148.                 {
  149.                     new = accept(sock, (struct sockaddr *) &client, &size);
  150.                     if(new < 0)
  151.                     {
  152.                         printf("Error in accept %s\n", strerror(errno));
  153.                         close(sock);
  154.                         return;
  155.                     }
  156.  
  157.                     printf("New connection %d from %s:%d\n", val,
  158.                             inet_ntoa(client.sin_addr),
  159.                             ntohs(client.sin_port));
  160.  
  161.                     write(new, HELLO_MSG, strlen(HELLO_MSG));
  162.  
  163.                     FD_SET(new, &setsave);
  164.                 }
  165.                 else
  166.                 {
  167.                     readbytes = read(val, data, sizeof(data));
  168.                     if(readbytes <= 0)
  169.                     {
  170.                         printf("Socket %d closed\n", val);
  171.                         FD_CLR(val, &setsave);
  172.                         close(val);
  173.                     }
  174.                     else
  175.                     {
  176.                         write(val, data, readbytes);
  177.                         printf("%.*s", readbytes, data);
  178.                     }
  179.                 }
  180.             }
  181.         }
  182.     }
  183.  
  184.     close(sock);
  185. }
  186.  
  187. /* Connect to an access point */
  188. int connect_to_apctl(int config)
  189. {
  190.     int err;
  191.     int stateLast = -1;
  192.  
  193.     /* Connect using the first profile */
  194.     err = sceNetApctlConnect(config);
  195.     if (err != 0)
  196.     {
  197.         printf(MODULE_NAME ": sceNetApctlConnect returns %08X\n", err);
  198.         return 0;
  199.     }
  200.  
  201.     printf(MODULE_NAME ": Connecting...\n");
  202.     while (1)
  203.     {
  204.         int state;
  205.         err = sceNetApctlGetState(&state);
  206.         if (err != 0)
  207.         {
  208.             printf(MODULE_NAME ": sceNetApctlGetState returns $%x\n", err);
  209.             break;
  210.         }
  211.         if (state > stateLast)
  212.         {
  213.             printf("  connection state %d of 4\n", state);
  214.             stateLast = state;
  215.         }
  216.         if (state == 4)
  217.             break;  // connected with static IP
  218.  
  219.         // wait a little before polling again
  220.         sceKernelDelayThread(50*1000); // 50ms
  221.     }
  222.     printf(MODULE_NAME ": Connected!\n");
  223.  
  224.     if(err != 0)
  225.     {
  226.         return 0;
  227.     }
  228.  
  229.     return 1;
  230. }
  231.  
  232. int net_thread(SceSize args, void *argp)
  233. {
  234.     int err;
  235.  
  236.     do
  237.     {
  238.         if((err = pspSdkInetInit()))
  239.         {
  240.             printf(MODULE_NAME ": Error, could not initialise the network %08X\n", err);
  241.             break;
  242.         }
  243.  
  244.         if(connect_to_apctl(1))
  245.         {
  246.             // connected, get my IPADDR and run test
  247.             union SceNetApctlInfo info;
  248.            
  249.             if (sceNetApctlGetInfo(8, &info) != 0)
  250.                 strcpy(info.ip, "unknown IP");
  251.            
  252.             start_server(info.ip);
  253.         }
  254.     }
  255.     while(0);
  256.  
  257.     return 0;
  258. }
  259.  
  260. /* Simple thread */
  261. int main(int argc, char **argv)
  262. {
  263.     SceUID thid;
  264.  
  265.     SetupCallbacks();
  266.  
  267.     pspDebugScreenInit();
  268.  
  269.     if(pspSdkLoadInetModules() < 0)
  270.     {
  271.         printf("Error, could not load inet modules\n");
  272.         sceKernelSleepThread();
  273.     }
  274.  
  275.     /* Create a user thread to do the real work */
  276.     thid = sceKernelCreateThread("net_thread", net_thread, 0x18, 0x10000, PSP_THREAD_ATTR_USER, NULL);
  277.     if(thid < 0)
  278.     {
  279.         printf("Error, could not create thread\n");
  280.         sceKernelSleepThread();
  281.     }
  282.  
  283.     sceKernelStartThread(thid, 0, NULL);
  284.  
  285.     sceKernelExitDeleteThread(0);
  286.  
  287.     return 0;
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement