- reworked completly the JNetwork, JSocket interface (had to ifdef out the PSP socket code)

- added 2 menus to wait for connection and wait for deck selection
- tested compilation on Qt Linux, Qt Windows and PSP
- deactivated everywhere (NETWORK_SUPPORT to activate).
This commit is contained in:
Xawotihs
2011-03-13 21:19:02 +00:00
parent 3220f94e00
commit f9be0a6341
23 changed files with 735 additions and 400 deletions

View File

@@ -6,47 +6,41 @@
#include "JGE.h"
#include <string>
#include <map>
using namespace std;
class JSocket;
#include <iostream>
#include <sstream>
#include "Threading.h"
class JNetwork{
typedef void(*processCmd)(istream&, ostream&);
class JNetwork {
private:
static JNetwork * mInstance;
static int connected_to_ap;
int connected_to_ap;
JSocket* socket;
boost::mutex sendMutex;
boost::mutex receiveMutex;
stringstream received;
stringstream toSend;
static map<string, processCmd> sCommandMap;
public:
static string error;
JNetwork();
static JNetwork * GetInstance();
static void EndInstance();
static string serverIP;
int receive(char * buffer, int length);
int send(char * buffer, int length);
~JNetwork();
string serverIP;
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
bool isConnected();
static void ThreadProc(void* param);
#if !defined (WIN32) && !defined (LINUX)
static int connect_to_apctl(int config);
#endif
bool sendCommand(string command);
static void registerCommand(string command, processCmd processCommand, processCmd processResponse);
private:
#if defined (WIN32)
static DWORD netthread;
#elif defined (LINUX)
static pthread_t netthread;
#else
static int netthread;
#endif
boost::thread *mpWorkerThread;
};
#if defined (WIN32)
#elif defined (LINUX)
#else
static int net_thread(SceSize args, void *argp);
#endif
#endif

View File

@@ -2,29 +2,48 @@
#define _JSOCKET_H_
#include <queue>
#include "Threading.h"
using namespace std;
//TODO config ?
#define SERVER_PORT 20666
class JSocket{
public:
queue<char> received_data;
queue<char> tosend_data;
static JSocket * mInstance;
int start_server(const char *szIpAddr);
int start_client(const char *szIpAddr);
typedef enum {
// no network available currently from the device
NOT_AVAILABLE,
// network available but disconnected from another socket
DISCONNECTED,
// connected with another socket
CONNECTED,
// some fatal problem
FATAL_ERROR,
} SOCKET_STATE;
// Server creation
JSocket(string ipAddr);
// Client creation
JSocket();
~JSocket();
static int connected;
JSocket* Accept();
int Read(char* buff, int size);
int Write(char* buff, int size);
bool isConnected() { return state == CONNECTED; };
void Disconnect();
private:
void init();
void readWrite(int sock);
#if defined (WIN32) || defined (LINUX)
#else
int make_socket(uint16_t port);
// socket creation when server accepts a connection
JSocket(int fd);
// convert the socket into non-blocking state
bool SetNonBlocking(int sock);
// socket handle
#ifdef WIN32
SOCKET mfd;
#elif LINUX
int mfd;
#endif
// socket state
SOCKET_STATE state;
};
#endif

View File

@@ -4,7 +4,7 @@
*/
#include "../include/DebugRoutines.h"
#include "../include/JNetwork.h"
#if defined (WIN32) || defined (LINUX)
@@ -30,144 +30,154 @@
#endif
#include <sstream>
#include "../include/JSocket.h"
JNetwork* JNetwork::mInstance = NULL;
string JNetwork::serverIP = "";
string JNetwork::error = "";
map<string, processCmd> JNetwork::sCommandMap;
JNetwork * JNetwork::GetInstance(){
if (!mInstance) mInstance = new JNetwork();
return mInstance;
bool JNetwork::isConnected(){
if (connected_to_ap !=1) return false;
return socket->isConnected();
}
void JNetwork::EndInstance(){
SAFE_DELETE(mInstance);
}
#if defined (WIN32)
DWORD JNetwork::netthread = 0;
int JNetwork::connected_to_ap = 1;
#elif defined (LINUX)
pthread_t JNetwork::netthread = NULL;
int JNetwork::connected_to_ap = 1;
JNetwork::JNetwork()
: mpWorkerThread(NULL)
{
#if (defined WIN32) || (defined LINUX)
connected_to_ap = 1;
#else
int JNetwork::connected_to_ap = 0;
int JNetwork::netthread = 0;
connected_to_ap = 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)
JNetwork::~JNetwork()
{
do
if(mpWorkerThread) {
socket->Disconnect();
mpWorkerThread->join();
delete mpWorkerThread;
}
if(socket)
delete socket;
}
bool JNetwork::sendCommand(string xString)
{
string aString = xString;
boost::mutex::scoped_lock l(sendMutex);
if(!socket) {
DebugTrace("sendCommand failed: no sockeet");
return false;
}
aString = aString + "Command";
if(sCommandMap.find(aString) == sCommandMap.end()) {
DebugTrace("sendCommand failed: command not registered");
return false;
}
aString = aString + "\n";
toSend << aString;
return true;
}
void JNetwork::registerCommand(string command, processCmd processCommand, processCmd processResponse)
{
sCommandMap[command + "Command"] = processCommand;
sCommandMap[command + "Response"] = processResponse;
}
void JNetwork::ThreadProc(void* param)
{
JNetwork* pThis = reinterpret_cast<JNetwork*>(param);
JSocket* pSocket = NULL;
if (pThis->serverIP.size()) {
DebugTrace("Starting Client Thread");
pThis->socket = new JSocket(pThis->serverIP);
if(pThis->socket->isConnected())
pSocket = pThis->socket;
} else {
DebugTrace("Starting Server Thread");
pThis->socket = new JSocket();
// Wait for some client
pSocket = pThis->socket->Accept();
}
while(pSocket && pSocket->isConnected()) {
char buff[1024];
{
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 ?
boost::mutex::scoped_lock l(pThis->receiveMutex);
int len = pSocket->Read(buff, sizeof(buff));
if(len) {
DebugTrace("receiving " << len << " bytes : " << buff);
pThis->received << buff;
}
// Checking for some command to execute
size_t found = pThis->received.str().find("Command");
if(found != string::npos)
{
map<string, processCmd>::iterator ite = sCommandMap.find((pThis->received.str()).substr(0, found) + "Command");
if(ite != sCommandMap.end())
{
DebugTrace("begin of command received : "<< pThis->received.str() );
DebugTrace("begin of command toSend : "<< pThis->toSend.str() );
boost::mutex::scoped_lock l(pThis->sendMutex);
pThis->toSend << pThis->received.str().substr(0, found) + "Response ";
pThis->received.str("");
processCmd theMethod = (ite)->second;
theMethod(pThis->received, pThis->toSend);
DebugTrace("end of command received : "<< pThis->received.str() );
DebugTrace("end of command toSend : "<< pThis->toSend.str() );
}
}
// Checking for some response to execute
found = pThis->received.str().find("Response");
if(found != string::npos)
{
map<string, processCmd>::iterator ite = sCommandMap.find((pThis->received.str()).substr(0, found) + "Response");
if(ite != sCommandMap.end())
{
DebugTrace("begin of response received : "<< pThis->received.str() );
DebugTrace("begin of response toSend : "<< pThis->toSend.str() );
boost::mutex::scoped_lock l(pThis->sendMutex);
string aString;
pThis->received >> aString;
processCmd theMethod = (ite)->second;
theMethod(pThis->received, pThis->toSend);
pThis->received.str("");
DebugTrace("end of response received : "<< pThis->received.str() );
DebugTrace("end of response toSend : "<< pThis->toSend.str() );
}
}
}
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
boost::mutex::scoped_lock l(pThis->sendMutex);
if(!pThis->toSend.str().empty())
{
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 ?
DebugTrace("sending " << pThis->toSend.str().size() << " bytes : " << pThis->toSend.str());
pSocket->Write((char*)pThis->toSend.str().c_str(), pThis->toSend.str().size()+1);
pThis->toSend.str("");
}
while (0);
return NULL;
}
DebugTrace("Quitting Thread");
}
int JNetwork::connect(string serverIP)
#if defined (WIN32) || defined (LINUX)
int JNetwork::connect(string ip)
{
if (netthread) return 0;
JNetwork::serverIP = serverIP;
return pthread_create(&netthread, NULL, net_thread, NULL);
if (mpWorkerThread) return 0;
serverIP = ip;
mpWorkerThread = new boost::thread(JNetwork::ThreadProc, this);
return 42;
}
#else
int net_thread(SceSize args, void *argp)
{
#ifdef NETWORK_SUPPORT
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);
#endif
return 0;
}
int JNetwork::connect(string serverIP){
#ifdef NETWORK_SUPPORT

View File

@@ -1,3 +1,5 @@
#if 0
// need a complete rewrite to comply to the new interface
#ifdef NETWORK_SUPPORT
#include <pspkernel.h>
@@ -223,5 +225,6 @@ int JSocket::start_server(const char *szIpAddr)
return 0;
}
#endif //NETWORK_SUPPORT

View File

@@ -1,230 +1,301 @@
#include "PrecompiledHeader.h"
#include <errno.h>
#ifdef WIN32
#pragma comment(lib,"WSOCK32.LIB")
#include <stdio.h>
#include <conio.h>
#include <winsock.h>
#include <errno.h>
#include <winsock.h>
#include <fcntl.h>
#elif LINUX
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#endif //WINDOWS
#include "../../include/JSocket.h"
JSocket * JSocket::mInstance = NULL;
//JSocket * JSocket::mInstance = NULL;
#define SERVER_PORT 20666
//#define SERVER_PORT 20666
#define SERVER_PORT 5001
unsigned char ADRESSE_IP_SERVEUR [4] = {127,0,0,1};
int JSocket::connected = 0;
void JSocket::init(){
//TODO ?
}
JSocket::JSocket(){
init();
}
JSocket::~JSocket(){
//TODO ?
}
void JSocket::readWrite(int val){
char data[1024];
fd_set set;
FD_ZERO(&set);
FD_SET(val, &set);
struct timeval tv;
int result = select(0, &set, NULL, NULL, &tv);
if( result< 0)
{
printf("Socket %d closed\n", val);
closesocket(val);
connected= 0;
printf("select error\n");
return;
}
if (result > 0){
OutputDebugString("Receiving!\n");
int readbytes = recv(val, data, sizeof(data),0);
if(readbytes < 0)
{
int error = WSAGetLastError();
if (error != WSAEWOULDBLOCK){
printf("Socket %d closed\n", val);
closesocket(val);
connected= 0;
}
}
else
{
OutputDebugString("received Data!!!\n");
for (int i = 0; i < readbytes; i++){
received_data.push(data[i]);
OutputDebugString("getting byte\n");
}
}
}
//Write
int size = 0;
while(!tosend_data.empty()){
size++;
if (size > sizeof(data)){
send(val,data,sizeof(data),0);
size = 0;
}
data[size-1] = tosend_data.front();
tosend_data.pop();
}
if (size) {
send(val,data,size-1,0);
OutputDebugString("sending Data\n");
}
}
/* Start a client */
int JSocket::start_client(const char *szIpAddr){
JSocket::JSocket(string ipAddr)
: state(NOT_AVAILABLE),
mfd(-1)
{
int result = -1;
#ifdef WIN32
SOCKET Desc_Socket_Cliente;
SOCKADDR_IN Adresse_Socket_Serveur;
#elif LINUX
int Desc_Socket_Cliente;
#endif
struct hostent *hostentptr;
#ifdef WIN32
SOCKADDR_IN Adresse_Socket_Server;
WORD wVersionRequested;
WSADATA wsaData;
struct hostent *hostentptr;
wVersionRequested=MAKEWORD(1,1);
WSAStartup(wVersionRequested,&wsaData);
result = WSAStartup(wVersionRequested,&wsaData);
if(result!=0){
DebugTrace("WSAStartup\t");
return;
}
#elif LINUX
struct sockaddr_in Adresse_Socket_Server;
#endif
Desc_Socket_Cliente=socket(AF_INET,SOCK_STREAM,0);
unsigned int addr_dest = inet_addr(szIpAddr);
mfd=socket(AF_INET,SOCK_STREAM,0);
if(mfd < 0)
return;
DebugTrace("Connecting " << ipAddr);
#ifdef WIN32
unsigned int addr_dest = inet_addr(ipAddr.c_str());
hostentptr=gethostbyaddr((char*) &addr_dest,4,AF_INET);
#elif LINUX
hostentptr = gethostbyname(ipAddr.c_str());
#endif
if (hostentptr == NULL) {
DebugTrace("ERROR, no such host\n");
return;
}
ZeroMemory(&Adresse_Socket_Serveur,sizeof(Adresse_Socket_Serveur));
#ifdef WIN32
ZeroMemory( (char*)&Adresse_Socket_Server,sizeof(Adresse_Socket_Server));
#elif LINUX
bzero( (char*)&Adresse_Socket_Server,sizeof(Adresse_Socket_Server));
#endif //WINDOWS
Adresse_Socket_Serveur.sin_family=(*hostentptr).h_addrtype;
Adresse_Socket_Serveur.sin_port=htons(SERVER_PORT);
Adresse_Socket_Serveur.sin_addr=*((struct in_addr*)(*hostentptr).h_addr);
int result = connect(
Desc_Socket_Cliente,
(const struct sockaddr*)&Adresse_Socket_Serveur,
sizeof(Adresse_Socket_Serveur));
OutputDebugString("client state 0\n");
Adresse_Socket_Server.sin_family=(*hostentptr).h_addrtype;
Adresse_Socket_Server.sin_port=htons(SERVER_PORT);
Adresse_Socket_Server.sin_addr=*((struct in_addr*)(*hostentptr).h_addr);
result = connect(
mfd,
(const struct sockaddr*)&Adresse_Socket_Server,
sizeof(Adresse_Socket_Server));
if (result != 0){
connected = 0;
return 0;
DebugTrace("client connect failed :" << strerror(errno));
state = FATAL_ERROR;
return;
}
OutputDebugString("client state 1\n");
state = CONNECTED;
}
bool JSocket::SetNonBlocking(int sock)
{
int opts;
connected = 1;
while(1){
readWrite(Desc_Socket_Cliente);
/* Nb_Caracteres_Recus=recv(Desc_Socket_Cliente,Message_Recu,sizeof(Message_Recu),0);
if(Nb_Caracteres_Recus<=0){
//continuer=FALSE;
}else{
strcpy(message,Message_Recu);
len=strlen(message);
#ifdef WIN32
#elif LINUX
opts = fcntl(sock,F_GETFL);
if (opts < 0) {
perror("fcntl(F_GETFL)");
return false;
}
*/
}
#
closesocket(Desc_Socket_Cliente);
WSACleanup();
return 0;
opts = (opts | O_NONBLOCK);
if (fcntl(sock,F_SETFL,opts) < 0) {
perror("fcntl(F_SETFL)");
return false;
}
#endif //WINDOWS
return true;
}
/* Start a server */
int JSocket::start_server(const char *szIpAddr){
OutputDebugString("server state 0\n");
connected= 0;
int Code_Retour;
SOCKET Desc_Socket_Connection;
JSocket::JSocket()
: state(NOT_AVAILABLE),
mfd(-1)
{
int result;
#ifdef WIN32
SOCKADDR_IN Adresse_Socket_Connection;
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested=MAKEWORD(1,1);
result=WSAStartup(wVersionRequested,&wsaData);
Code_Retour=WSAStartup(wVersionRequested,&wsaData);
if(Code_Retour!=0){
perror("WSAStartup\t");
_getch();
WSACleanup();
return Code_Retour;
if(result!=0){
DebugTrace("WSAStartup\t");
return;
}
OutputDebugString("server state 1\n");
/* printf("la version supportee est : %d.%d\n",
LOBYTE(wsaData.wHighVersion),
HIBYTE(wsaData.wHighVersion)
);*/
#elif LINUX
struct sockaddr_in Adresse_Socket_Connection;
#endif //WINDOWS
Desc_Socket_Connection=socket( AF_INET, SOCK_STREAM,0);
mfd=socket( AF_INET, SOCK_STREAM,0);
//printf("valeur de la socket = %d\n",Desc_Socket_Connection);
#ifdef WIN32
#elif LINUX
int reuse_addr = 1; /* Used so we can re-bind to our port
while a previous connection is still
in TIME_WAIT state. */
/* So that we can re-bind to it without TIME_WAIT problems */
setsockopt(mfd, SOL_SOCKET, SO_REUSEADDR, &reuse_addr,
sizeof(reuse_addr));
#endif //WINDOWS
ZeroMemory(&Adresse_Socket_Connection,sizeof(Adresse_Socket_Connection));
SetNonBlocking(mfd);
#ifdef WIN32
ZeroMemory( &Adresse_Socket_Connection,sizeof(Adresse_Socket_Connection));
#elif LINUX
bzero( &Adresse_Socket_Connection,sizeof(Adresse_Socket_Connection));
#endif //WINDOWS
Adresse_Socket_Connection.sin_family=AF_INET;
Adresse_Socket_Connection.sin_port=htons(SERVER_PORT);
Code_Retour=bind(Desc_Socket_Connection,
result=bind(mfd,
(struct sockaddr*)&Adresse_Socket_Connection,
sizeof(Adresse_Socket_Connection));
if(Code_Retour!=0){
perror("bind\t");
_getch();
closesocket(Desc_Socket_Connection);
WSACleanup();
return Code_Retour;
if(result!=0){
state = FATAL_ERROR;
DebugTrace("bind error:" << strerror(errno));
return;
}
OutputDebugString("server state 3\n");
Code_Retour=listen(Desc_Socket_Connection,1);
if(Code_Retour!=0){
perror("listen\n");
WSACleanup();
return Code_Retour;
result=listen(mfd,1);
if(result!=0){
state = FATAL_ERROR;
DebugTrace("listen error:" << strerror(errno));
return;
}
OutputDebugString("server state 4\n");
printf("serveur en attente d'une connection\n\n");
printf("***************arret du serveur par<CTRL><C>**************\n\n");
connected = 1;
while(1){
SOCKET * pt_Nouveau_Socket_Serveur;
SOCKADDR_IN Adresse_Socket_Cliente;
int Longueur_Adresse;
pt_Nouveau_Socket_Serveur = new SOCKET;
state = DISCONNECTED;
}
Longueur_Adresse = sizeof(Adresse_Socket_Cliente);
JSocket::JSocket(int fd)
: state(CONNECTED),
mfd(fd)
{
}
int val=accept(
Desc_Socket_Connection,
(struct sockaddr*)&Adresse_Socket_Cliente,
&Longueur_Adresse);
printf("connection accepte depuis le port client %d\n", ntohs(Adresse_Socket_Cliente.sin_port));
OutputDebugString("connection accepte depuis le port client\n");
JSocket::~JSocket(){
Disconnect();
#ifdef WIN32
WSACleanup();
#endif
}
while(1)
{
readWrite(val);
}
closesocket(*pt_Nouveau_Socket_Serveur);
return 0;
void JSocket::Disconnect()
{
state = JSocket::DISCONNECTED;
if(mfd) {
#ifdef WIN32
closesocket(mfd);
#elif LINUX
close(mfd);
#endif
mfd = 0;
}
}
JSocket* JSocket::Accept()
{
#ifdef WIN32
SOCKADDR_IN Adresse_Socket_Cliente;
int Longueur_Adresse;
#elif LINUX
struct sockaddr_in Adresse_Socket_Cliente;
socklen_t Longueur_Adresse;
#endif //WINDOWS
while(mfd) {
fd_set set;
FD_ZERO(&set);
FD_SET(mfd, &set);
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1000*100;
int result = select(mfd+1, &set, NULL, NULL, &tv);
if( result > 0 && FD_ISSET(mfd, &set) ) {
Longueur_Adresse = sizeof(Adresse_Socket_Cliente);
int val=accept(
mfd,
(struct sockaddr*)&Adresse_Socket_Cliente,
&Longueur_Adresse);
DebugTrace("connection on client port "<< ntohs(Adresse_Socket_Cliente.sin_port));
if(val >=0 ) {
state = CONNECTED;
return new JSocket(val);
} else {
return NULL;
}
}
}
}
int JSocket::Read(char* buff, int size)
{
if(state == CONNECTED)
{
fd_set set;
FD_ZERO(&set);
FD_SET(mfd, &set);
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1000*100;
int result = select(mfd+1, &set, NULL, NULL, &tv);
if( result > 0 && FD_ISSET(mfd, &set) )
{
#ifdef WIN32
int readbytes = recv(mfd, buff, size,0);
#elif LINUX
int readbytes = read(mfd, buff, size);
#endif //WINDOWS
if(readbytes < 0)
{
DebugTrace("Error reading from socket\n");
return -1;
}
else
return readbytes;
}
else if( result < 0)
{
return -1;
}
}
return 0;
}
int JSocket::Write(char* buff, int size)
{
int size1 = size;
while (size > 0 && state == CONNECTED) {
fd_set set;
FD_ZERO(&set);
FD_SET(mfd, &set);
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1000*100;
int result = select(mfd+1, NULL, &set, NULL, &tv);
if( result > 0 && FD_ISSET(mfd, &set))
{
int len = send(mfd, buff, size, 0);
if (len < 0) {
return -1;
}
size -= len;
buff += len;
} else if (result < 0) {
return -1;
}
}
return size1 - size;
}