mirror of
https://github.com/pspdev/pspsdk.git
synced 2025-10-04 17:09:09 +00:00
samples/net/simple: Run in user mode instead of kernel mode
Kernel mode samples did not run correctly. This commit switches the sample over to run in userland, and changes some heap settings as well.
This commit is contained in:
@@ -9,278 +9,242 @@
|
|||||||
* Some small parts (c) 2005 PSPPet
|
* Some small parts (c) 2005 PSPPet
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <pspkernel.h>
|
#include <pspkernel.h>
|
||||||
#include <pspdebug.h>
|
#include <pspnet_apctl.h>
|
||||||
#include <pspsdk.h>
|
#include <pspsdk.h>
|
||||||
#include <stdlib.h>
|
#include <psputility.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pspnet.h>
|
|
||||||
#include <pspnet_inet.h>
|
|
||||||
#include <pspnet_apctl.h>
|
|
||||||
#include <pspnet_resolver.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <sys/select.h>
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#define printf pspDebugScreenPrintf
|
#define printf pspDebugScreenPrintf
|
||||||
|
|
||||||
#define MODULE_NAME "NetSample"
|
#define MODULE_NAME "NetSample"
|
||||||
#define HELLO_MSG "Hello there. Type away.\r\n"
|
#define HELLO_MSG "Hello there. Type away.\r\n"
|
||||||
|
|
||||||
PSP_MODULE_INFO(MODULE_NAME, 0x1000, 1, 1);
|
PSP_MODULE_INFO(MODULE_NAME, 0, 1, 1);
|
||||||
PSP_MAIN_THREAD_ATTR(0);
|
PSP_HEAP_THRESHOLD_SIZE_KB(1024);
|
||||||
|
PSP_HEAP_SIZE_KB(-2048);
|
||||||
|
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);
|
||||||
|
PSP_MAIN_THREAD_STACK_SIZE_KB(1024);
|
||||||
|
|
||||||
/* Exit callback */
|
/* Exit callback */
|
||||||
int exit_callback(int arg1, int arg2, void *common)
|
int exit_callback(int arg1, int arg2, void *common) {
|
||||||
{
|
sceKernelExitGame();
|
||||||
sceKernelExitGame();
|
return 0;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Callback thread */
|
/* Callback thread */
|
||||||
int CallbackThread(SceSize args, void *argp)
|
int CallbackThread(SceSize args, void *argp) {
|
||||||
{
|
int cbid;
|
||||||
int cbid;
|
|
||||||
|
|
||||||
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
|
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
|
||||||
sceKernelRegisterExitCallback(cbid);
|
sceKernelRegisterExitCallback(cbid);
|
||||||
sceKernelSleepThreadCB();
|
sceKernelSleepThreadCB();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Sets up the callback thread and returns its thread id */
|
/* Sets up the callback thread and returns its thread id */
|
||||||
int SetupCallbacks(void)
|
int SetupCallbacks(void) {
|
||||||
{
|
int thid = 0;
|
||||||
int thid = 0;
|
|
||||||
|
|
||||||
thid = sceKernelCreateThread("update_thread", CallbackThread,
|
thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0,
|
||||||
0x11, 0xFA0, PSP_THREAD_ATTR_USER, 0);
|
PSP_THREAD_ATTR_USER, 0);
|
||||||
if(thid >= 0)
|
if (thid >= 0) {
|
||||||
{
|
sceKernelStartThread(thid, 0, 0);
|
||||||
sceKernelStartThread(thid, 0, 0);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return thid;
|
return thid;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define SERVER_PORT 23
|
#define SERVER_PORT 23
|
||||||
|
|
||||||
int make_socket(uint16_t port)
|
int make_socket(uint16_t port) {
|
||||||
{
|
int sock;
|
||||||
int sock;
|
int ret;
|
||||||
int ret;
|
struct sockaddr_in name;
|
||||||
struct sockaddr_in name;
|
|
||||||
|
|
||||||
sock = socket(PF_INET, SOCK_STREAM, 0);
|
sock = socket(PF_INET, SOCK_STREAM, 0);
|
||||||
if(sock < 0)
|
if (sock < 0) {
|
||||||
{
|
return -1;
|
||||||
return -1;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
name.sin_family = AF_INET;
|
name.sin_family = AF_INET;
|
||||||
name.sin_port = htons(port);
|
name.sin_port = htons(port);
|
||||||
name.sin_addr.s_addr = htonl(INADDR_ANY);
|
name.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
ret = bind(sock, (struct sockaddr *) &name, sizeof(name));
|
ret = bind(sock, (struct sockaddr *)&name, sizeof(name));
|
||||||
if(ret < 0)
|
if (ret < 0) {
|
||||||
{
|
return -1;
|
||||||
return -1;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return sock;
|
return sock;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Start a simple tcp echo server */
|
/* Start a simple tcp echo server */
|
||||||
void start_server(const char *szIpAddr)
|
void start_server(const char *szIpAddr) {
|
||||||
{
|
int ret;
|
||||||
int ret;
|
int sock;
|
||||||
int sock;
|
int new = -1;
|
||||||
int new = -1;
|
struct sockaddr_in client;
|
||||||
struct sockaddr_in client;
|
unsigned int size;
|
||||||
size_t size;
|
int readbytes;
|
||||||
int readbytes;
|
char data[1024];
|
||||||
char data[1024];
|
fd_set set;
|
||||||
fd_set set;
|
fd_set setsave;
|
||||||
fd_set setsave;
|
|
||||||
|
|
||||||
/* Create a socket for listening */
|
/* Create a socket for listening */
|
||||||
sock = make_socket(SERVER_PORT);
|
sock = make_socket(SERVER_PORT);
|
||||||
if(sock < 0)
|
if (sock < 0) {
|
||||||
{
|
printf("Error creating server socket\n");
|
||||||
printf("Error creating server socket\n");
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
ret = listen(sock, 1);
|
ret = listen(sock, 1);
|
||||||
if(ret < 0)
|
if (ret < 0) {
|
||||||
{
|
printf("Error calling listen\n");
|
||||||
printf("Error calling listen\n");
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
printf("Listening for connections ip %s port %d\n", szIpAddr, SERVER_PORT);
|
printf("Listening for connections ip %s port %d\n", szIpAddr, SERVER_PORT);
|
||||||
|
|
||||||
FD_ZERO(&set);
|
FD_ZERO(&set);
|
||||||
FD_SET(sock, &set);
|
FD_SET(sock, &set);
|
||||||
setsave = set;
|
setsave = set;
|
||||||
|
|
||||||
while(1)
|
while (1) {
|
||||||
{
|
int i;
|
||||||
int i;
|
set = setsave;
|
||||||
set = setsave;
|
if (select(FD_SETSIZE, &set, NULL, NULL, NULL) < 0) {
|
||||||
if(select(FD_SETSIZE, &set, NULL, NULL, NULL) < 0)
|
printf("select error\n");
|
||||||
{
|
return;
|
||||||
printf("select error\n");
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(i = 0; i < FD_SETSIZE; i++)
|
for (i = 0; i < FD_SETSIZE; i++) {
|
||||||
{
|
if (FD_ISSET(i, &set)) {
|
||||||
if(FD_ISSET(i, &set))
|
int val = i;
|
||||||
{
|
|
||||||
int val = i;
|
|
||||||
|
|
||||||
if(val == sock)
|
if (val == sock) {
|
||||||
{
|
new = accept(sock, (struct sockaddr *)&client, &size);
|
||||||
new = accept(sock, (struct sockaddr *) &client, &size);
|
if (new < 0) {
|
||||||
if(new < 0)
|
printf("Error in accept %s\n", strerror(errno));
|
||||||
{
|
close(sock);
|
||||||
printf("Error in accept %s\n", strerror(errno));
|
return;
|
||||||
close(sock);
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("New connection %d from %s:%d\n", val,
|
printf("New connection %d from %s:%d\n", val,
|
||||||
inet_ntoa(client.sin_addr),
|
inet_ntoa(client.sin_addr), ntohs(client.sin_port));
|
||||||
ntohs(client.sin_port));
|
|
||||||
|
|
||||||
write(new, HELLO_MSG, strlen(HELLO_MSG));
|
write(new, HELLO_MSG, strlen(HELLO_MSG));
|
||||||
|
|
||||||
FD_SET(new, &setsave);
|
FD_SET(new, &setsave);
|
||||||
}
|
} else {
|
||||||
else
|
readbytes = read(val, data, sizeof(data));
|
||||||
{
|
if (readbytes <= 0) {
|
||||||
readbytes = read(val, data, sizeof(data));
|
printf("Socket %d closed\n", val);
|
||||||
if(readbytes <= 0)
|
FD_CLR(val, &setsave);
|
||||||
{
|
close(val);
|
||||||
printf("Socket %d closed\n", val);
|
} else {
|
||||||
FD_CLR(val, &setsave);
|
write(val, data, readbytes);
|
||||||
close(val);
|
printf("%.*s", readbytes, data);
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
{
|
}
|
||||||
write(val, data, readbytes);
|
}
|
||||||
printf("%.*s", readbytes, data);
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
close(sock);
|
close(sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Connect to an access point */
|
/* Connect to an access point */
|
||||||
int connect_to_apctl(int config)
|
int connect_to_apctl(int config) {
|
||||||
{
|
int err;
|
||||||
int err;
|
int stateLast = -1;
|
||||||
int stateLast = -1;
|
|
||||||
|
|
||||||
/* Connect using the first profile */
|
/* Connect using the first profile */
|
||||||
err = sceNetApctlConnect(config);
|
err = sceNetApctlConnect(config);
|
||||||
if (err != 0)
|
if (err != 0) {
|
||||||
{
|
printf(MODULE_NAME ": sceNetApctlConnect returns %08X\n", err);
|
||||||
printf(MODULE_NAME ": sceNetApctlConnect returns %08X\n", err);
|
return 0;
|
||||||
return 0;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
printf(MODULE_NAME ": Connecting...\n");
|
printf(MODULE_NAME ": Connecting...\n");
|
||||||
while (1)
|
while (1) {
|
||||||
{
|
int state;
|
||||||
int state;
|
err = sceNetApctlGetState(&state);
|
||||||
err = sceNetApctlGetState(&state);
|
if (err != 0) {
|
||||||
if (err != 0)
|
printf(MODULE_NAME ": sceNetApctlGetState returns $%x\n", err);
|
||||||
{
|
break;
|
||||||
printf(MODULE_NAME ": sceNetApctlGetState returns $%x\n", err);
|
}
|
||||||
break;
|
if (state > stateLast) {
|
||||||
}
|
printf(" connection state %d of 4\n", state);
|
||||||
if (state > stateLast)
|
stateLast = state;
|
||||||
{
|
}
|
||||||
printf(" connection state %d of 4\n", state);
|
if (state == 4)
|
||||||
stateLast = state;
|
break; // connected with static IP
|
||||||
}
|
|
||||||
if (state == 4)
|
|
||||||
break; // connected with static IP
|
|
||||||
|
|
||||||
// wait a little before polling again
|
// wait a little before polling again
|
||||||
sceKernelDelayThread(50*1000); // 50ms
|
sceKernelDelayThread(50 * 1000); // 50ms
|
||||||
}
|
}
|
||||||
printf(MODULE_NAME ": Connected!\n");
|
printf(MODULE_NAME ": Connected!\n");
|
||||||
|
|
||||||
if(err != 0)
|
if (err != 0) {
|
||||||
{
|
return 0;
|
||||||
return 0;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int net_thread(SceSize args, void *argp)
|
int net_thread(SceSize args, void *argp) {
|
||||||
{
|
int err;
|
||||||
int err;
|
|
||||||
|
|
||||||
do
|
do {
|
||||||
{
|
if ((err = pspSdkInetInit())) {
|
||||||
if((err = pspSdkInetInit()))
|
printf(MODULE_NAME ": Error, could not initialise the network %08X\n",
|
||||||
{
|
err);
|
||||||
printf(MODULE_NAME ": Error, could not initialise the network %08X\n", err);
|
break;
|
||||||
break;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if(connect_to_apctl(1))
|
if (connect_to_apctl(1)) {
|
||||||
{
|
// connected, get my IPADDR and run test
|
||||||
// connected, get my IPADDR and run test
|
union SceNetApctlInfo info;
|
||||||
union SceNetApctlInfo info;
|
|
||||||
|
|
||||||
if (sceNetApctlGetInfo(8, &info) != 0)
|
|
||||||
strcpy(info.ip, "unknown IP");
|
|
||||||
|
|
||||||
start_server(info.ip);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
while(0);
|
|
||||||
|
|
||||||
return 0;
|
if (sceNetApctlGetInfo(8, &info) != 0)
|
||||||
|
strcpy(info.ip, "unknown IP");
|
||||||
|
|
||||||
|
start_server(info.ip);
|
||||||
|
}
|
||||||
|
} while (0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Simple thread */
|
/* Simple thread */
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv) {
|
||||||
{
|
SceUID thid;
|
||||||
SceUID thid;
|
|
||||||
|
|
||||||
SetupCallbacks();
|
SetupCallbacks();
|
||||||
|
|
||||||
pspDebugScreenInit();
|
sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON);
|
||||||
|
sceUtilityLoadNetModule(PSP_NET_MODULE_INET);
|
||||||
|
|
||||||
if(pspSdkLoadInetModules() < 0)
|
pspDebugScreenInit();
|
||||||
{
|
|
||||||
printf("Error, could not load inet modules\n");
|
|
||||||
sceKernelSleepThread();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Create a user thread to do the real work */
|
/* Create a user thread to do the real work */
|
||||||
thid = sceKernelCreateThread("net_thread", net_thread, 0x18, 0x10000, PSP_THREAD_ATTR_USER, NULL);
|
thid =
|
||||||
if(thid < 0)
|
sceKernelCreateThread("net_thread", net_thread,
|
||||||
{
|
0x11, // default priority
|
||||||
printf("Error, could not create thread\n");
|
256 * 1024, // stack size (256KB is regular default)
|
||||||
sceKernelSleepThread();
|
PSP_THREAD_ATTR_USER, NULL);
|
||||||
}
|
if (thid < 0) {
|
||||||
|
printf("Error, could not create thread\n");
|
||||||
|
sceKernelSleepThread();
|
||||||
|
}
|
||||||
|
|
||||||
sceKernelStartThread(thid, 0, NULL);
|
sceKernelStartThread(thid, 0, NULL);
|
||||||
|
|
||||||
sceKernelExitDeleteThread(0);
|
sceKernelExitDeleteThread(0);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user