-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework networking sample to TCP server #77
- Loading branch information
1 parent
e2e028d
commit 2695279
Showing
4 changed files
with
53 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,79 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <errno.h> | ||
#include <netinet/in.h> | ||
#include <sys/socket.h> | ||
#include <arpa/inet.h> | ||
|
||
#include <orbis/libkernel.h> | ||
#include <orbis/Pad.h> | ||
#include <orbis/UserService.h> | ||
|
||
#define IP "192.168.0.10" | ||
#define PORT 9030 | ||
#include "../../_common/log.h" | ||
|
||
void waitOnButton(int pad, unsigned int button) | ||
{ | ||
OrbisPadData padData; | ||
|
||
for(;;) | ||
{ | ||
scePadReadState(pad, &padData); | ||
|
||
if(padData.buttons & button) { | ||
return; | ||
} | ||
} | ||
} | ||
#define PORT 9025 | ||
|
||
// Logging | ||
std::stringstream debugLogStream; | ||
|
||
int main(void) | ||
{ | ||
int userID; | ||
const char *msg = "ping"; | ||
struct sockaddr_in addr; | ||
int sockfd; | ||
int connfd; | ||
socklen_t addrLen; | ||
|
||
struct sockaddr_in serverAddr; | ||
struct sockaddr_in clientAddr; | ||
|
||
// No buffering | ||
setvbuf(stdout, NULL, _IONBF, 0); | ||
|
||
// Create a server socket | ||
sockfd = socket(AF_INET, SOCK_STREAM, 0); | ||
|
||
// Initialize the Pad library | ||
if (scePadInit() != 0) | ||
if (sockfd < 0) | ||
{ | ||
printf("[DEBUG] [ERROR] Failed to initialize pad library!\n"); | ||
return -1; | ||
DEBUGLOG << "Failed to create socket: " << strerror(errno); | ||
for (;;); | ||
} | ||
|
||
// Get the user ID | ||
OrbisUserServiceInitializeParams param; | ||
param.priority = ORBIS_KERNEL_PRIO_FIFO_LOWEST; | ||
sceUserServiceInitialize(¶m); | ||
sceUserServiceGetInitialUser(&userID); | ||
|
||
// Open a handle for the controller | ||
int pad = scePadOpen(userID, 0, 0, NULL); | ||
// Bind to 0.0.0.0:PORT | ||
memset(&serverAddr, 0, sizeof(serverAddr)); | ||
serverAddr.sin_family = AF_INET; | ||
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
serverAddr.sin_port = htons(PORT); | ||
|
||
if (pad < 0) | ||
if (bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) != 0) | ||
{ | ||
printf("[DEBUG] Failed to open pad!\n"); | ||
return -1; | ||
DEBUGLOG << "Failed to bind to 0.0.0.0:" << PORT << ": " << strerror(errno); | ||
for (;;); | ||
} | ||
|
||
// Setup the socket | ||
(void)memset(&addr, 0, sizeof(struct sockaddr_in)); | ||
|
||
int sock = socket(AF_INET, SOCK_STREAM, 0); | ||
// Listen and accept clients | ||
addrLen = sizeof(clientAddr); | ||
|
||
addr.sin_family = AF_INET; | ||
addr.sin_port = htons(PORT); | ||
|
||
// Wait on X | ||
waitOnButton(pad, ORBIS_PAD_BUTTON_CROSS); | ||
|
||
if (inet_pton(AF_INET, IP, &addr.sin_addr) <= 0) | ||
if (listen(sockfd, 5) != 0) | ||
{ | ||
printf("[DEBUG] [ERROR] IP Address not supported.\n"); | ||
return -1; | ||
DEBUGLOG << "Failed to listen: " << strerror(errno); | ||
for (;;); | ||
} | ||
|
||
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) | ||
for (;;) | ||
{ | ||
printf("[DEBUG] [ERROR] Failed to connect to %s:%d.\n", IP, PORT); | ||
return -1; | ||
} | ||
connfd = accept(sockfd, (struct sockaddr*)&clientAddr, &addrLen); | ||
|
||
if (connfd < 0) | ||
{ | ||
DEBUGLOG << "Failed to accept client: " << strerror(errno); | ||
for (;;); | ||
} | ||
|
||
printf("[DEBUG] Sending message to %s:%d.\n", IP, PORT); | ||
send(sock, msg, strlen(msg), 0); | ||
printf("[DEBUG] Message sent. Closing and infinitely looping.\n"); | ||
DEBUGLOG << "Accepted client '" << connfd << "'"; | ||
|
||
close(sock); | ||
for (;;) {} | ||
// Write a "hello" message then terminate the connection | ||
const char msg[] = "hello\n"; | ||
write(connfd, msg, sizeof(msg)); | ||
close(connfd); | ||
|
||
DEBUGLOG << "Closed client '" << connfd << "'"; | ||
} | ||
} |