diff --git a/JGE/Makefile b/JGE/Makefile index 281f803b8..ae384bd78 100644 --- a/JGE/Makefile +++ b/JGE/Makefile @@ -7,7 +7,7 @@ GENERIC_OBJS = src/JApp.o src/JGBKFont.o \ src/JGE.o src/JGui.o src/JLBFont.o \ src/JGameObject.o src/JSpline.o src/JAnimator.o \ src/JResourceManager.o src/JFileSystem.o \ - src/JNetwork.o src/JSocket.o \ + src/JNetwork.o \ src/JParticle.o src/JParticleEmitter.o src/JParticleEffect.o \ src/JParticleSystem.o \ src/unzip/ioapi.o src/unzip/mztools.o src/unzip/unzip.o \ @@ -16,7 +16,7 @@ GENERIC_OBJS = src/JApp.o src/JGBKFont.o \ src/tinyxml/tinyxmlparser.o src/tinyxml/tinyxmlerror.o \ src/Encoding.o src/JTTFont.o \ src/JMD2Model.o src/JOBJModel.o src/vram.o -PSP_OBJS = src/JGfx.o src/JSfx.o src/JAudio.o src/JMP3.o src/decoder_prx.o src/main.o +PSP_OBJS = src/JSocket.o src/JGfx.o src/JSfx.o src/JAudio.o src/JMP3.o src/decoder_prx.o src/main.o LINUX_OBJS = src/linux/JGfx.o src/linux/JSfx.o src/Xmain.o diff --git a/JGE/include/JNetwork.h b/JGE/include/JNetwork.h index 7a94e1b3a..7f360b705 100644 --- a/JGE/include/JNetwork.h +++ b/JGE/include/JNetwork.h @@ -1,43 +1,47 @@ -#ifndef _JNETWORK_H_ -#define _JNETWORK_H_ - -#ifdef WIN32 -#elif defined (LINUX) -#else -#endif - -#include "JGE.h" -#include -using namespace std; - -class JNetwork{ -private: - static JNetwork * mInstance; - static int connected_to_ap; -public: - JNetwork(); - static JNetwork * GetInstance(); - static void EndInstance(); - static string serverIP; - int receive(char * buffer, int length); - int send(char * buffer, int length); - int connect(string serverIP = ""); - static int isConnected(); -#if defined (WIN32) || defined (LINUX) - static int net_thread(LPVOID param); -#else - static int net_thread(SceSize args, void *argp); - static int connect_to_apctl(int config); -#endif - -private: - -#if defined (WIN32) || defined (LINUX) - static DWORD netthread; -#else - static int netthread; -#endif - -}; - -#endif \ No newline at end of file +#ifndef _JNETWORK_H_ +#define _JNETWORK_H_ + +#ifdef WIN32 +#elif defined (LINUX) +#else +#endif + +#include "JGE.h" +#include +using namespace std; + +class JNetwork{ +private: + static JNetwork * mInstance; + static int connected_to_ap; +public: + JNetwork(); + static JNetwork * GetInstance(); + static void EndInstance(); + static string serverIP; + int receive(char * buffer, int length); + int send(char * buffer, int length); + int connect(string serverIP = ""); + static int isConnected(); +#if defined (WIN32) + static int net_thread(void* param); +#elif defined (LINUX) + static void* net_thread(void* param); +#else + static int net_thread(SceSize args, void *argp); + static int connect_to_apctl(int config); +#endif + +private: + +#if defined (WIN32) + static DWORD netthread; +#elif defined (LINUX) + static pthread_t netthread; +#else + static int netthread; +#endif + +}; + +#endif diff --git a/JGE/src/JNetwork.cpp b/JGE/src/JNetwork.cpp index 04a13158f..2d7ef005f 100644 --- a/JGE/src/JNetwork.cpp +++ b/JGE/src/JNetwork.cpp @@ -1,213 +1,230 @@ -/* - PSP Network function calls - - -*/ - -#if defined (WIN32) || defined (LINUX) -#else -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#endif - -#include "../include/JNetwork.h" -#include "../include/JSocket.h" - -JNetwork* JNetwork::mInstance = NULL; -string JNetwork::serverIP = ""; - -JNetwork * JNetwork::GetInstance(){ - if (!mInstance) mInstance = new JNetwork(); - return mInstance; -} - -void JNetwork::EndInstance(){ - SAFE_DELETE(mInstance); -} - - - -#if defined (WIN32) || defined (LINUX) - DWORD JNetwork::netthread = 0; - int JNetwork::connected_to_ap = 1; -#else - int JNetwork::connected_to_ap = 0; - int JNetwork::netthread = 0; -#endif - -int JNetwork::isConnected(){ - if (connected_to_ap !=1) return 0; - return JSocket::connected; -} - -JNetwork::JNetwork(){ -} - -int JNetwork::receive(char * buffer, int length){ - JSocket * socket = JSocket::mInstance; - if (!socket) return 0; - int size = 0; - while(!socket->received_data.empty() && size < length){ - buffer[size] = socket->received_data.front(); - socket->received_data.pop(); - size++; - } - return size; -} - -int JNetwork::send(char * buffer, int length){ - JSocket * socket = JSocket::mInstance; - if (!socket) return 0; - for (int i = 0; i < length; i++){ - socket->tosend_data.push(buffer[i]); - } - return length; -} - - - -#ifdef WIN32 -int JNetwork::net_thread(LPVOID param) -{ - do - { - JSocket::mInstance = new JSocket(); - JSocket * s = JSocket::mInstance; - if (JNetwork::serverIP.size()){ - OutputDebugString(JNetwork::serverIP.c_str()); - s->start_client(JNetwork::serverIP.c_str()); - - }else{ - s->start_server(""); //IP address useless for server ? - - } - } - while(0); - - return 0; -} - - -int JNetwork::connect(string serverIP){ - if(netthread) return 0; - - JNetwork::serverIP = serverIP; - /* Create a user thread to do the real work */ - HANDLE hthread; - hthread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)net_thread,0,0,&netthread); - return netthread; -} - -#else -int net_thread(SceSize args, void *argp) -{ - - - do - { - JSocket::mInstance = new JSocket(); - JSocket * s = JSocket::mInstance; - if (JNetwork::serverIP.size()){ - s->start_client(JNetwork::serverIP.c_str()); - - }else{ - // connected, get my IPADDR and run test - SceNetApctlInfo szMyIPAddr; - if (sceNetApctlGetInfo(8, &szMyIPAddr) != 0){ - }else{ - s->start_server(szMyIPAddr.ip); - } - - } - } - while(0); - - return 0; -} - - -int JNetwork::connect(string serverIP){ - int err; - if(netthread) return 0; - if((err = pspSdkInetInit())){ - printf("JGE Error, could not initialise the network %08X\n", err); - return err; - } - - if(JNetwork::connect_to_apctl(1)){ - JNetwork::serverIP = serverIP; - /* Create a user thread to do the real work */ - netthread = sceKernelCreateThread("net_thread", net_thread, 0x18, 0x10000, PSP_THREAD_ATTR_USER, NULL); - - if(netthread < 0) - { - printf("Error, could not create thread\n"); - sceKernelSleepThread(); - } - - sceKernelStartThread(netthread, 0, NULL); - return netthread; - } - - return 0; -} - - -/* Connect to an access point */ -int JNetwork::connect_to_apctl(int config) -{ - int err; - int stateLast = -1; - - /* Connect using the first profile */ - err = sceNetApctlConnect(config); - if (err != 0) - { - printf("JGE: sceNetApctlConnect returns %08X\n", err); - return 0; - } - - printf("JGE: Connecting...\n"); - while (1) - { - int state; - err = sceNetApctlGetState(&state); - if (err != 0) - { - printf("JGE: sceNetApctlGetState returns $%x\n", err); - break; - } - if (state > stateLast) - { - printf(" connection state %d of 4\n", state); - stateLast = state; - } - if (state == 4){ - connected_to_ap = 1; - break; // connected with static IP - } - // wait a little before polling again - sceKernelDelayThread(50*1000); // 50ms - } - printf("JGE: Connected!\n"); - - if(err != 0) - { - return 0; - } - - return 1; -} -#endif \ No newline at end of file +/* + PSP Network function calls + + +*/ + +#if defined (WIN32) || defined (LINUX) +#else +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +#include "../include/JNetwork.h" +#include "../include/JSocket.h" + +JNetwork* JNetwork::mInstance = NULL; +string JNetwork::serverIP = ""; + +JNetwork * JNetwork::GetInstance(){ + if (!mInstance) mInstance = new JNetwork(); + return mInstance; +} + +void JNetwork::EndInstance(){ + SAFE_DELETE(mInstance); +} + + + +#if defined (WIN32) || defined (LINUX) + DWORD JNetwork::netthread = 0; + int JNetwork::connected_to_ap = 1; +#else + int JNetwork::connected_to_ap = 0; + int JNetwork::netthread = 0; +#endif + +int JNetwork::isConnected(){ + if (connected_to_ap !=1) return 0; + return JSocket::connected; +} + +JNetwork::JNetwork(){ +} + +int JNetwork::receive(char * buffer, int length){ + JSocket * socket = JSocket::mInstance; + if (!socket) return 0; + int size = 0; + while(!socket->received_data.empty() && size < length){ + buffer[size] = socket->received_data.front(); + socket->received_data.pop(); + size++; + } + return size; +} + +int JNetwork::send(char * buffer, int length){ + JSocket * socket = JSocket::mInstance; + if (!socket) return 0; + for (int i = 0; i < length; i++){ + socket->tosend_data.push(buffer[i]); + } + return length; +} + + + +#if defined (WIN32) +int JNetwork::net_thread(void* param) +{ + do + { + JSocket::mInstance = new JSocket(); + JSocket * s = JSocket::mInstance; + if (JNetwork::serverIP.size()){ + OutputDebugString(JNetwork::serverIP.c_str()); + s->start_client(JNetwork::serverIP.c_str()); + }else{ + s->start_server(""); //IP address useless for server ? + } + } + while(0); + + return 0; +} +int JNetwork::connect(string serverIP){ + if(netthread) return 0; + + JNetwork::serverIP = serverIP; + /* Create a user thread to do the real work */ + HANDLE hthread; + hthread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)net_thread,0,0,&netthread); + return netthread; +} + +#elif defined (LINUX) +void* JNetwork::net_thread(void* param __attribute__((unused))) +{ + do + { + JSocket::mInstance = new JSocket(); + JSocket * s = JSocket::mInstance; + if (JNetwork::serverIP.size()) + s->start_client(JNetwork::serverIP.c_str()); + else + s->start_server(""); //IP address useless for server ? + } + while (0); + return NULL; +} + +int JNetwork::connect(string serverIP) +{ + if (netthread) return 0; + JNetwork::serverIP = serverIP; + return pthread_create(&netthread, NULL, net_thread, NULL); +} + +#else +int net_thread(SceSize args, void *argp) +{ + do + { + JSocket::mInstance = new JSocket(); + JSocket * s = JSocket::mInstance; + if (JNetwork::serverIP.size()){ + s->start_client(JNetwork::serverIP.c_str()); + + }else{ + // connected, get my IPADDR and run test + SceNetApctlInfo szMyIPAddr; + if (sceNetApctlGetInfo(8, &szMyIPAddr) != 0){ + }else{ + s->start_server(szMyIPAddr.ip); + } + + } + } + while(0); + + return 0; +} + + +int JNetwork::connect(string serverIP){ + int err; + if(netthread) return 0; + if((err = pspSdkInetInit())){ + printf("JGE Error, could not initialise the network %08X\n", err); + return err; + } + + if(JNetwork::connect_to_apctl(1)){ + JNetwork::serverIP = serverIP; + /* Create a user thread to do the real work */ + netthread = sceKernelCreateThread("net_thread", net_thread, 0x18, 0x10000, PSP_THREAD_ATTR_USER, NULL); + + if(netthread < 0) + { + printf("Error, could not create thread\n"); + sceKernelSleepThread(); + } + + sceKernelStartThread(netthread, 0, NULL); + return netthread; + } + + return 0; +} + + +/* Connect to an access point */ +int JNetwork::connect_to_apctl(int config) +{ + int err; + int stateLast = -1; + + /* Connect using the first profile */ + err = sceNetApctlConnect(config); + if (err != 0) + { + printf("JGE: sceNetApctlConnect returns %08X\n", err); + return 0; + } + + printf("JGE: Connecting...\n"); + while (1) + { + int state; + err = sceNetApctlGetState(&state); + if (err != 0) + { + printf("JGE: sceNetApctlGetState returns $%x\n", err); + break; + } + if (state > stateLast) + { + printf(" connection state %d of 4\n", state); + stateLast = state; + } + if (state == 4){ + connected_to_ap = 1; + break; // connected with static IP + } + // wait a little before polling again + sceKernelDelayThread(50*1000); // 50ms + } + printf("JGE: Connected!\n"); + + if(err != 0) + { + return 0; + } + + return 1; +} +#endif diff --git a/JGE/src/JSocket.cpp b/JGE/src/JSocket.cpp index e6ffcd437..960ed3a12 100644 --- a/JGE/src/JSocket.cpp +++ b/JGE/src/JSocket.cpp @@ -1,211 +1,209 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "../include/JGE.h" - +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../include/JGE.h" + #include "../include/JSocket.h" -JSocket * JSocket::mInstance = NULL; - -#define SERVER_PORT 20666 - -void JSocket::init(){ - sceUtilityLoadNetModule(1); - sceUtilityLoadNetModule(3); -} - -JSocket::JSocket(){ - init(); -} - -JSocket::~JSocket(){ - pspSdkInetTerm(); - sceNetApctlDisconnect(); - sceNetApctlTerm(); -} - - -int JSocket::make_socket(uint16_t port) -{ - int sock; - int ret; - struct sockaddr_in name; - - sock = socket(PF_INET, SOCK_STREAM, 0); - if(sock < 0) - { - return -1; - } - - name.sin_family = AF_INET; - name.sin_port = htons(port); - name.sin_addr.s_addr = htonl(INADDR_ANY); - ret = bind(sock, (struct sockaddr *) &name, sizeof(name)); - if(ret < 0) - { - return -1; - } - - return sock; -} - - -void JSocket::readWrite(int val){ - char data[1024]; - int readbytes; - readbytes = read(val, data, sizeof(data)); - if(readbytes < 0) - { - printf("Socket %d closed\n", val); - close(val); - } - else - { - for (int i = 0; i < readbytes; i++){ - received_data.push(data[i]); - } - } - - //Write - unsigned int size = 0; - while(!tosend_data.empty()){ - size++; - if (size > sizeof(data)){ - write(val,data,sizeof(data)); - size = 0; - } - data[size-1] = tosend_data.front(); - tosend_data.pop(); - } - if (size) write(val,data,size); -} - -/* Start a client */ -int JSocket::start_client(const char *szIpAddr){ - int sock; - sockaddr_in addrListen; - int error; - - sock = sceNetInetSocket(AF_INET, SOCK_STREAM, 0); - if (sock <= 0){ - printf("socket returned $%x\n", sock); - sceKernelDelayThread(500*1000); - return sock; - } - - addrListen.sin_family = AF_INET; - addrListen.sin_addr.s_addr = inet_addr(szIpAddr); - addrListen.sin_port = htons(SERVER_PORT); - - int err = sceNetInetConnect(sock, (sockaddr *)&addrListen, sizeof(addrListen)); - if (err != 0){ - printf("Unable to connect!\n"); - printf("connect returned $%x\n", err); - printf(" errno=$%x\n", sceNetInetGetErrno()); - sceKernelDelayThread(500*1000); - return err; - } - - while(1){ - readWrite(sock); - } - return 0; -} - -/* Start a server */ -int JSocket::start_server(const char *szIpAddr) -{ - int ret; - int sock; - int _new = -1; - struct sockaddr_in client; - size_t size; - int readbytes; - - fd_set set; - fd_set setsave; - - /* Create a socket for listening */ - sock = make_socket(SERVER_PORT); - if(sock < 0) - { - printf("Error creating server socket\n"); - return sock; - } - - ret = listen(sock, 1); - if(ret < 0) - { - printf("Error calling listen\n"); - return ret; - } - - printf("Listening for connections ip %s port %d\n", szIpAddr, SERVER_PORT); - - FD_ZERO(&set); - FD_SET(sock, &set); - setsave = set; - - while(1) - { - int i; - set = setsave; - if(select(FD_SETSIZE, &set, NULL, NULL, NULL) < 0) - { - printf("select error\n"); - return -1; - } - - for(i = 0; i < FD_SETSIZE; i++) - { - if(FD_ISSET(i, &set)) - { - int val = i; - - if(val == sock) - { - _new = accept(sock, (struct sockaddr *) &client, &size); - if(_new < 0) - { - printf("Error in accept %s\n", strerror(errno)); - close(sock); - return _new; - } - - printf("New connection %d from %s:%d\n", val, - inet_ntoa(client.sin_addr), - ntohs(client.sin_port)); - - write(_new, "123567", strlen("123567")); - - FD_SET(_new, &setsave); - } - else - { - readWrite(val); - } - } - } - } - - close(sock); - return 0; -} - - - +JSocket * JSocket::mInstance = NULL; + +#define SERVER_PORT 20666 + +void JSocket::init(){ + sceUtilityLoadNetModule(1); + sceUtilityLoadNetModule(3); +} + +JSocket::JSocket(){ + init(); +} + +JSocket::~JSocket(){ + pspSdkInetTerm(); + sceNetApctlDisconnect(); + sceNetApctlTerm(); +} + + +int JSocket::make_socket(uint16_t port) +{ + int sock; + int ret; + struct sockaddr_in name; + + sock = socket(PF_INET, SOCK_STREAM, 0); + if(sock < 0) + { + return -1; + } + + name.sin_family = AF_INET; + name.sin_port = htons(port); + name.sin_addr.s_addr = htonl(INADDR_ANY); + ret = bind(sock, (struct sockaddr *) &name, sizeof(name)); + if(ret < 0) + { + return -1; + } + + return sock; +} + + +void JSocket::readWrite(int val){ + char data[1024]; + int readbytes; + readbytes = read(val, data, sizeof(data)); + if(readbytes < 0) + { + printf("Socket %d closed\n", val); + close(val); + } + else + { + for (int i = 0; i < readbytes; i++){ + received_data.push(data[i]); + } + } + + //Write + unsigned int size = 0; + while(!tosend_data.empty()){ + size++; + if (size > sizeof(data)){ + write(val,data,sizeof(data)); + size = 0; + } + data[size-1] = tosend_data.front(); + tosend_data.pop(); + } + if (size) write(val,data,size); +} + +/* Start a client */ +int JSocket::start_client(const char *szIpAddr){ + int sock; + sockaddr_in addrListen; + int error; + + sock = sceNetInetSocket(AF_INET, SOCK_STREAM, 0); + if (sock <= 0){ + printf("socket returned $%x\n", sock); + sceKernelDelayThread(500*1000); + return sock; + } + + addrListen.sin_family = AF_INET; + addrListen.sin_addr.s_addr = inet_addr(szIpAddr); + addrListen.sin_port = htons(SERVER_PORT); + + int err = sceNetInetConnect(sock, (sockaddr *)&addrListen, sizeof(addrListen)); + if (err != 0){ + printf("Unable to connect!\n"); + printf("connect returned $%x\n", err); + printf(" errno=$%x\n", sceNetInetGetErrno()); + sceKernelDelayThread(500*1000); + return err; + } + + while(1){ + readWrite(sock); + } + return 0; +} + +/* Start a server */ +int JSocket::start_server(const char *szIpAddr) +{ + int ret; + int sock; + int _new = -1; + struct sockaddr_in client; + size_t size; + int readbytes; + + fd_set set; + fd_set setsave; + + /* Create a socket for listening */ + sock = make_socket(SERVER_PORT); + if(sock < 0) + { + printf("Error creating server socket\n"); + return sock; + } + + ret = listen(sock, 1); + if(ret < 0) + { + printf("Error calling listen\n"); + return ret; + } + + printf("Listening for connections ip %s port %d\n", szIpAddr, SERVER_PORT); + + FD_ZERO(&set); + FD_SET(sock, &set); + setsave = set; + + while(1) + { + int i; + set = setsave; + if(select(FD_SETSIZE, &set, NULL, NULL, NULL) < 0) + { + printf("select error\n"); + return -1; + } + + for(i = 0; i < FD_SETSIZE; i++) + { + if(FD_ISSET(i, &set)) + { + int val = i; + + if(val == sock) + { + _new = accept(sock, (struct sockaddr *) &client, &size); + if(_new < 0) + { + printf("Error in accept %s\n", strerror(errno)); + close(sock); + return _new; + } + + printf("New connection %d from %s:%d\n", val, + inet_ntoa(client.sin_addr), + ntohs(client.sin_port)); + + write(_new, "123567", strlen("123567")); + + FD_SET(_new, &setsave); + } + else + { + readWrite(val); + } + } + } + } + + close(sock); + return 0; +} +