J :
* Write preliminary thread creation functions for Linux. * Debug the Makefile
This commit is contained in:
@@ -7,7 +7,7 @@ GENERIC_OBJS = src/JApp.o src/JGBKFont.o \
|
|||||||
src/JGE.o src/JGui.o src/JLBFont.o \
|
src/JGE.o src/JGui.o src/JLBFont.o \
|
||||||
src/JGameObject.o src/JSpline.o src/JAnimator.o \
|
src/JGameObject.o src/JSpline.o src/JAnimator.o \
|
||||||
src/JResourceManager.o src/JFileSystem.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/JParticle.o src/JParticleEmitter.o src/JParticleEffect.o \
|
||||||
src/JParticleSystem.o \
|
src/JParticleSystem.o \
|
||||||
src/unzip/ioapi.o src/unzip/mztools.o src/unzip/unzip.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/tinyxml/tinyxmlparser.o src/tinyxml/tinyxmlerror.o \
|
||||||
src/Encoding.o src/JTTFont.o \
|
src/Encoding.o src/JTTFont.o \
|
||||||
src/JMD2Model.o src/JOBJModel.o src/vram.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
|
LINUX_OBJS = src/linux/JGfx.o src/linux/JSfx.o src/Xmain.o
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,43 +1,47 @@
|
|||||||
#ifndef _JNETWORK_H_
|
#ifndef _JNETWORK_H_
|
||||||
#define _JNETWORK_H_
|
#define _JNETWORK_H_
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
#elif defined (LINUX)
|
#elif defined (LINUX)
|
||||||
#else
|
#else
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "JGE.h"
|
#include "JGE.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class JNetwork{
|
class JNetwork{
|
||||||
private:
|
private:
|
||||||
static JNetwork * mInstance;
|
static JNetwork * mInstance;
|
||||||
static int connected_to_ap;
|
static int connected_to_ap;
|
||||||
public:
|
public:
|
||||||
JNetwork();
|
JNetwork();
|
||||||
static JNetwork * GetInstance();
|
static JNetwork * GetInstance();
|
||||||
static void EndInstance();
|
static void EndInstance();
|
||||||
static string serverIP;
|
static string serverIP;
|
||||||
int receive(char * buffer, int length);
|
int receive(char * buffer, int length);
|
||||||
int send(char * buffer, int length);
|
int send(char * buffer, int length);
|
||||||
int connect(string serverIP = "");
|
int connect(string serverIP = "");
|
||||||
static int isConnected();
|
static int isConnected();
|
||||||
#if defined (WIN32) || defined (LINUX)
|
#if defined (WIN32)
|
||||||
static int net_thread(LPVOID param);
|
static int net_thread(void* param);
|
||||||
#else
|
#elif defined (LINUX)
|
||||||
static int net_thread(SceSize args, void *argp);
|
static void* net_thread(void* param);
|
||||||
static int connect_to_apctl(int config);
|
#else
|
||||||
#endif
|
static int net_thread(SceSize args, void *argp);
|
||||||
|
static int connect_to_apctl(int config);
|
||||||
private:
|
#endif
|
||||||
|
|
||||||
#if defined (WIN32) || defined (LINUX)
|
private:
|
||||||
static DWORD netthread;
|
|
||||||
#else
|
#if defined (WIN32)
|
||||||
static int netthread;
|
static DWORD netthread;
|
||||||
#endif
|
#elif defined (LINUX)
|
||||||
|
static pthread_t netthread;
|
||||||
};
|
#else
|
||||||
|
static int netthread;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -1,213 +1,230 @@
|
|||||||
/*
|
/*
|
||||||
PSP Network function calls
|
PSP Network function calls
|
||||||
|
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if defined (WIN32) || defined (LINUX)
|
#if defined (WIN32) || defined (LINUX)
|
||||||
#else
|
#else
|
||||||
#include <pspkernel.h>
|
#include <pspkernel.h>
|
||||||
#include <pspdebug.h>
|
#include <pspdebug.h>
|
||||||
#include <pspsdk.h>
|
#include <pspsdk.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pspnet.h>
|
#include <pspnet.h>
|
||||||
#include <pspnet_inet.h>
|
#include <pspnet_inet.h>
|
||||||
#include <pspnet_apctl.h>
|
#include <pspnet_apctl.h>
|
||||||
#include <pspnet_resolver.h>
|
#include <pspnet_resolver.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "../include/JNetwork.h"
|
#include "../include/JNetwork.h"
|
||||||
#include "../include/JSocket.h"
|
#include "../include/JSocket.h"
|
||||||
|
|
||||||
JNetwork* JNetwork::mInstance = NULL;
|
JNetwork* JNetwork::mInstance = NULL;
|
||||||
string JNetwork::serverIP = "";
|
string JNetwork::serverIP = "";
|
||||||
|
|
||||||
JNetwork * JNetwork::GetInstance(){
|
JNetwork * JNetwork::GetInstance(){
|
||||||
if (!mInstance) mInstance = new JNetwork();
|
if (!mInstance) mInstance = new JNetwork();
|
||||||
return mInstance;
|
return mInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
void JNetwork::EndInstance(){
|
void JNetwork::EndInstance(){
|
||||||
SAFE_DELETE(mInstance);
|
SAFE_DELETE(mInstance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if defined (WIN32) || defined (LINUX)
|
#if defined (WIN32) || defined (LINUX)
|
||||||
DWORD JNetwork::netthread = 0;
|
DWORD JNetwork::netthread = 0;
|
||||||
int JNetwork::connected_to_ap = 1;
|
int JNetwork::connected_to_ap = 1;
|
||||||
#else
|
#else
|
||||||
int JNetwork::connected_to_ap = 0;
|
int JNetwork::connected_to_ap = 0;
|
||||||
int JNetwork::netthread = 0;
|
int JNetwork::netthread = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int JNetwork::isConnected(){
|
int JNetwork::isConnected(){
|
||||||
if (connected_to_ap !=1) return 0;
|
if (connected_to_ap !=1) return 0;
|
||||||
return JSocket::connected;
|
return JSocket::connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNetwork::JNetwork(){
|
JNetwork::JNetwork(){
|
||||||
}
|
}
|
||||||
|
|
||||||
int JNetwork::receive(char * buffer, int length){
|
int JNetwork::receive(char * buffer, int length){
|
||||||
JSocket * socket = JSocket::mInstance;
|
JSocket * socket = JSocket::mInstance;
|
||||||
if (!socket) return 0;
|
if (!socket) return 0;
|
||||||
int size = 0;
|
int size = 0;
|
||||||
while(!socket->received_data.empty() && size < length){
|
while(!socket->received_data.empty() && size < length){
|
||||||
buffer[size] = socket->received_data.front();
|
buffer[size] = socket->received_data.front();
|
||||||
socket->received_data.pop();
|
socket->received_data.pop();
|
||||||
size++;
|
size++;
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
int JNetwork::send(char * buffer, int length){
|
int JNetwork::send(char * buffer, int length){
|
||||||
JSocket * socket = JSocket::mInstance;
|
JSocket * socket = JSocket::mInstance;
|
||||||
if (!socket) return 0;
|
if (!socket) return 0;
|
||||||
for (int i = 0; i < length; i++){
|
for (int i = 0; i < length; i++){
|
||||||
socket->tosend_data.push(buffer[i]);
|
socket->tosend_data.push(buffer[i]);
|
||||||
}
|
}
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef WIN32
|
#if defined (WIN32)
|
||||||
int JNetwork::net_thread(LPVOID param)
|
int JNetwork::net_thread(void* param)
|
||||||
{
|
{
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
JSocket::mInstance = new JSocket();
|
JSocket::mInstance = new JSocket();
|
||||||
JSocket * s = JSocket::mInstance;
|
JSocket * s = JSocket::mInstance;
|
||||||
if (JNetwork::serverIP.size()){
|
if (JNetwork::serverIP.size()){
|
||||||
OutputDebugString(JNetwork::serverIP.c_str());
|
OutputDebugString(JNetwork::serverIP.c_str());
|
||||||
s->start_client(JNetwork::serverIP.c_str());
|
s->start_client(JNetwork::serverIP.c_str());
|
||||||
|
}else{
|
||||||
}else{
|
s->start_server(""); //IP address useless for server ?
|
||||||
s->start_server(""); //IP address useless for server ?
|
}
|
||||||
|
}
|
||||||
}
|
while(0);
|
||||||
}
|
|
||||||
while(0);
|
return 0;
|
||||||
|
}
|
||||||
return 0;
|
int JNetwork::connect(string serverIP){
|
||||||
}
|
if(netthread) return 0;
|
||||||
|
|
||||||
|
JNetwork::serverIP = serverIP;
|
||||||
int JNetwork::connect(string serverIP){
|
/* Create a user thread to do the real work */
|
||||||
if(netthread) return 0;
|
HANDLE hthread;
|
||||||
|
hthread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)net_thread,0,0,&netthread);
|
||||||
JNetwork::serverIP = serverIP;
|
return netthread;
|
||||||
/* Create a user thread to do the real work */
|
}
|
||||||
HANDLE hthread;
|
|
||||||
hthread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)net_thread,0,0,&netthread);
|
#elif defined (LINUX)
|
||||||
return netthread;
|
void* JNetwork::net_thread(void* param __attribute__((unused)))
|
||||||
}
|
{
|
||||||
|
do
|
||||||
#else
|
{
|
||||||
int net_thread(SceSize args, void *argp)
|
JSocket::mInstance = new JSocket();
|
||||||
{
|
JSocket * s = JSocket::mInstance;
|
||||||
|
if (JNetwork::serverIP.size())
|
||||||
|
s->start_client(JNetwork::serverIP.c_str());
|
||||||
do
|
else
|
||||||
{
|
s->start_server(""); //IP address useless for server ?
|
||||||
JSocket::mInstance = new JSocket();
|
}
|
||||||
JSocket * s = JSocket::mInstance;
|
while (0);
|
||||||
if (JNetwork::serverIP.size()){
|
return NULL;
|
||||||
s->start_client(JNetwork::serverIP.c_str());
|
}
|
||||||
|
|
||||||
}else{
|
int JNetwork::connect(string serverIP)
|
||||||
// connected, get my IPADDR and run test
|
{
|
||||||
SceNetApctlInfo szMyIPAddr;
|
if (netthread) return 0;
|
||||||
if (sceNetApctlGetInfo(8, &szMyIPAddr) != 0){
|
JNetwork::serverIP = serverIP;
|
||||||
}else{
|
return pthread_create(&netthread, NULL, net_thread, NULL);
|
||||||
s->start_server(szMyIPAddr.ip);
|
}
|
||||||
}
|
|
||||||
|
#else
|
||||||
}
|
int net_thread(SceSize args, void *argp)
|
||||||
}
|
{
|
||||||
while(0);
|
do
|
||||||
|
{
|
||||||
return 0;
|
JSocket::mInstance = new JSocket();
|
||||||
}
|
JSocket * s = JSocket::mInstance;
|
||||||
|
if (JNetwork::serverIP.size()){
|
||||||
|
s->start_client(JNetwork::serverIP.c_str());
|
||||||
int JNetwork::connect(string serverIP){
|
|
||||||
int err;
|
}else{
|
||||||
if(netthread) return 0;
|
// connected, get my IPADDR and run test
|
||||||
if((err = pspSdkInetInit())){
|
SceNetApctlInfo szMyIPAddr;
|
||||||
printf("JGE Error, could not initialise the network %08X\n", err);
|
if (sceNetApctlGetInfo(8, &szMyIPAddr) != 0){
|
||||||
return err;
|
}else{
|
||||||
}
|
s->start_server(szMyIPAddr.ip);
|
||||||
|
}
|
||||||
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);
|
while(0);
|
||||||
|
|
||||||
if(netthread < 0)
|
return 0;
|
||||||
{
|
}
|
||||||
printf("Error, could not create thread\n");
|
|
||||||
sceKernelSleepThread();
|
|
||||||
}
|
int JNetwork::connect(string serverIP){
|
||||||
|
int err;
|
||||||
sceKernelStartThread(netthread, 0, NULL);
|
if(netthread) return 0;
|
||||||
return netthread;
|
if((err = pspSdkInetInit())){
|
||||||
}
|
printf("JGE Error, could not initialise the network %08X\n", err);
|
||||||
|
return err;
|
||||||
return 0;
|
}
|
||||||
}
|
|
||||||
|
if(JNetwork::connect_to_apctl(1)){
|
||||||
|
JNetwork::serverIP = serverIP;
|
||||||
/* Connect to an access point */
|
/* Create a user thread to do the real work */
|
||||||
int JNetwork::connect_to_apctl(int config)
|
netthread = sceKernelCreateThread("net_thread", net_thread, 0x18, 0x10000, PSP_THREAD_ATTR_USER, NULL);
|
||||||
{
|
|
||||||
int err;
|
if(netthread < 0)
|
||||||
int stateLast = -1;
|
{
|
||||||
|
printf("Error, could not create thread\n");
|
||||||
/* Connect using the first profile */
|
sceKernelSleepThread();
|
||||||
err = sceNetApctlConnect(config);
|
}
|
||||||
if (err != 0)
|
|
||||||
{
|
sceKernelStartThread(netthread, 0, NULL);
|
||||||
printf("JGE: sceNetApctlConnect returns %08X\n", err);
|
return netthread;
|
||||||
return 0;
|
}
|
||||||
}
|
|
||||||
|
return 0;
|
||||||
printf("JGE: Connecting...\n");
|
}
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
int state;
|
/* Connect to an access point */
|
||||||
err = sceNetApctlGetState(&state);
|
int JNetwork::connect_to_apctl(int config)
|
||||||
if (err != 0)
|
{
|
||||||
{
|
int err;
|
||||||
printf("JGE: sceNetApctlGetState returns $%x\n", err);
|
int stateLast = -1;
|
||||||
break;
|
|
||||||
}
|
/* Connect using the first profile */
|
||||||
if (state > stateLast)
|
err = sceNetApctlConnect(config);
|
||||||
{
|
if (err != 0)
|
||||||
printf(" connection state %d of 4\n", state);
|
{
|
||||||
stateLast = state;
|
printf("JGE: sceNetApctlConnect returns %08X\n", err);
|
||||||
}
|
return 0;
|
||||||
if (state == 4){
|
}
|
||||||
connected_to_ap = 1;
|
|
||||||
break; // connected with static IP
|
printf("JGE: Connecting...\n");
|
||||||
}
|
while (1)
|
||||||
// wait a little before polling again
|
{
|
||||||
sceKernelDelayThread(50*1000); // 50ms
|
int state;
|
||||||
}
|
err = sceNetApctlGetState(&state);
|
||||||
printf("JGE: Connected!\n");
|
if (err != 0)
|
||||||
|
{
|
||||||
if(err != 0)
|
printf("JGE: sceNetApctlGetState returns $%x\n", err);
|
||||||
{
|
break;
|
||||||
return 0;
|
}
|
||||||
}
|
if (state > stateLast)
|
||||||
|
{
|
||||||
return 1;
|
printf(" connection state %d of 4\n", state);
|
||||||
}
|
stateLast = state;
|
||||||
#endif
|
}
|
||||||
|
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
|
||||||
|
|||||||
@@ -1,211 +1,209 @@
|
|||||||
#include <pspkernel.h>
|
#include <pspkernel.h>
|
||||||
#include <pspdebug.h>
|
#include <pspdebug.h>
|
||||||
#include <pspdisplay.h>
|
#include <pspdisplay.h>
|
||||||
#include <pspsdk.h>
|
#include <pspsdk.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pspnet.h>
|
#include <pspnet.h>
|
||||||
#include <pspnet_inet.h>
|
#include <pspnet_inet.h>
|
||||||
#include <pspnet_apctl.h>
|
#include <pspnet_apctl.h>
|
||||||
#include <pspnet_resolver.h>
|
#include <pspnet_resolver.h>
|
||||||
#include <psputility_netmodules.h>
|
#include <psputility_netmodules.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include "../include/JGE.h"
|
#include "../include/JGE.h"
|
||||||
|
|
||||||
#include "../include/JSocket.h"
|
#include "../include/JSocket.h"
|
||||||
JSocket * JSocket::mInstance = NULL;
|
JSocket * JSocket::mInstance = NULL;
|
||||||
|
|
||||||
#define SERVER_PORT 20666
|
#define SERVER_PORT 20666
|
||||||
|
|
||||||
void JSocket::init(){
|
void JSocket::init(){
|
||||||
sceUtilityLoadNetModule(1);
|
sceUtilityLoadNetModule(1);
|
||||||
sceUtilityLoadNetModule(3);
|
sceUtilityLoadNetModule(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
JSocket::JSocket(){
|
JSocket::JSocket(){
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
JSocket::~JSocket(){
|
JSocket::~JSocket(){
|
||||||
pspSdkInetTerm();
|
pspSdkInetTerm();
|
||||||
sceNetApctlDisconnect();
|
sceNetApctlDisconnect();
|
||||||
sceNetApctlTerm();
|
sceNetApctlTerm();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int JSocket::make_socket(uint16_t port)
|
int JSocket::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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void JSocket::readWrite(int val){
|
void JSocket::readWrite(int val){
|
||||||
char data[1024];
|
char data[1024];
|
||||||
int readbytes;
|
int readbytes;
|
||||||
readbytes = read(val, data, sizeof(data));
|
readbytes = read(val, data, sizeof(data));
|
||||||
if(readbytes < 0)
|
if(readbytes < 0)
|
||||||
{
|
{
|
||||||
printf("Socket %d closed\n", val);
|
printf("Socket %d closed\n", val);
|
||||||
close(val);
|
close(val);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (int i = 0; i < readbytes; i++){
|
for (int i = 0; i < readbytes; i++){
|
||||||
received_data.push(data[i]);
|
received_data.push(data[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Write
|
//Write
|
||||||
unsigned int size = 0;
|
unsigned int size = 0;
|
||||||
while(!tosend_data.empty()){
|
while(!tosend_data.empty()){
|
||||||
size++;
|
size++;
|
||||||
if (size > sizeof(data)){
|
if (size > sizeof(data)){
|
||||||
write(val,data,sizeof(data));
|
write(val,data,sizeof(data));
|
||||||
size = 0;
|
size = 0;
|
||||||
}
|
}
|
||||||
data[size-1] = tosend_data.front();
|
data[size-1] = tosend_data.front();
|
||||||
tosend_data.pop();
|
tosend_data.pop();
|
||||||
}
|
}
|
||||||
if (size) write(val,data,size);
|
if (size) write(val,data,size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Start a client */
|
/* Start a client */
|
||||||
int JSocket::start_client(const char *szIpAddr){
|
int JSocket::start_client(const char *szIpAddr){
|
||||||
int sock;
|
int sock;
|
||||||
sockaddr_in addrListen;
|
sockaddr_in addrListen;
|
||||||
int error;
|
int error;
|
||||||
|
|
||||||
sock = sceNetInetSocket(AF_INET, SOCK_STREAM, 0);
|
sock = sceNetInetSocket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (sock <= 0){
|
if (sock <= 0){
|
||||||
printf("socket returned $%x\n", sock);
|
printf("socket returned $%x\n", sock);
|
||||||
sceKernelDelayThread(500*1000);
|
sceKernelDelayThread(500*1000);
|
||||||
return sock;
|
return sock;
|
||||||
}
|
}
|
||||||
|
|
||||||
addrListen.sin_family = AF_INET;
|
addrListen.sin_family = AF_INET;
|
||||||
addrListen.sin_addr.s_addr = inet_addr(szIpAddr);
|
addrListen.sin_addr.s_addr = inet_addr(szIpAddr);
|
||||||
addrListen.sin_port = htons(SERVER_PORT);
|
addrListen.sin_port = htons(SERVER_PORT);
|
||||||
|
|
||||||
int err = sceNetInetConnect(sock, (sockaddr *)&addrListen, sizeof(addrListen));
|
int err = sceNetInetConnect(sock, (sockaddr *)&addrListen, sizeof(addrListen));
|
||||||
if (err != 0){
|
if (err != 0){
|
||||||
printf("Unable to connect!\n");
|
printf("Unable to connect!\n");
|
||||||
printf("connect returned $%x\n", err);
|
printf("connect returned $%x\n", err);
|
||||||
printf(" errno=$%x\n", sceNetInetGetErrno());
|
printf(" errno=$%x\n", sceNetInetGetErrno());
|
||||||
sceKernelDelayThread(500*1000);
|
sceKernelDelayThread(500*1000);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
while(1){
|
while(1){
|
||||||
readWrite(sock);
|
readWrite(sock);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Start a server */
|
/* Start a server */
|
||||||
int JSocket::start_server(const char *szIpAddr)
|
int JSocket::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;
|
||||||
size_t size;
|
size_t size;
|
||||||
int readbytes;
|
int readbytes;
|
||||||
|
|
||||||
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 sock;
|
return sock;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
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");
|
printf("select error\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
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));
|
printf("Error in accept %s\n", strerror(errno));
|
||||||
close(sock);
|
close(sock);
|
||||||
return _new;
|
return _new;
|
||||||
}
|
}
|
||||||
|
|
||||||
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, "123567", strlen("123567"));
|
write(_new, "123567", strlen("123567"));
|
||||||
|
|
||||||
FD_SET(_new, &setsave);
|
FD_SET(_new, &setsave);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
readWrite(val);
|
readWrite(val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
close(sock);
|
close(sock);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user