adding multiplayer
This commit is contained in:
@@ -1,29 +1,160 @@
|
||||
#include "server.h"
|
||||
#include "transactions.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int start_server() {
|
||||
|
||||
int start_server(s_game_server* s) {
|
||||
struct sockaddr_in server;
|
||||
int serverSock;
|
||||
|
||||
serverSock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
s->sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_port = htons(2223);
|
||||
server.sin_addr.s_addr = INADDR_ANY;
|
||||
|
||||
if(bind(serverSock, (struct sockaddr*)&serverSock, sizeof(server)) < 0) {
|
||||
if(bind(s->sock, (struct sockaddr*)&server, sizeof(server)) < 0) {
|
||||
printf("could not bind socket\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
listen(serverSock, 4);
|
||||
|
||||
int blocking_mode = 1;
|
||||
|
||||
ioctl(s->sock, FIONBIO, &blocking_mode); // Shuld be 1
|
||||
|
||||
printf("lissening\n");
|
||||
listen(s->sock, 4);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int accept_client(s_game_server* s) {
|
||||
struct sockaddr_in addr;
|
||||
socklen_t client_addr_size;
|
||||
|
||||
client_addr_size = sizeof(addr);
|
||||
int tmp = accept(s->sock, (struct sockaddr*)&addr, &client_addr_size);
|
||||
if(tmp == -1) {
|
||||
// return -1;
|
||||
} else {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if(s->clients[i].sock == 0) {
|
||||
s->clients[i].sock = tmp;
|
||||
s->clients[i].userId = rand();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//server_transaction st;
|
||||
//memset(&st, 0, sizeof(st));
|
||||
//st.id = SERVER_T_SERVER_FLAG;
|
||||
//st.action = SERVER_T_SET_PLAYER_ID;
|
||||
//st.argsize = sizeof(int);
|
||||
//st.serverArg =
|
||||
|
||||
|
||||
//SendServerTransaction(&st, tmp);
|
||||
|
||||
//user_transaction ut;
|
||||
//memset(&ut, 0, sizeof(user_transaction));
|
||||
|
||||
//for(int i = 0; i < 4; i++) {
|
||||
// if(s->clients[i].sock != 0) {
|
||||
// RecvUserTransaction(&ut, s->sock);
|
||||
|
||||
// }
|
||||
//}
|
||||
// accempt client
|
||||
// send world seed
|
||||
// send player colors
|
||||
// player data
|
||||
// - class
|
||||
// - level
|
||||
// - items
|
||||
// - etc
|
||||
return tmp;
|
||||
}
|
||||
|
||||
int limit_fps(int fps, struct timespec* last_time) {
|
||||
struct timespec current_time;
|
||||
double frame_time = 1.0/fps;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, ¤t_time);
|
||||
|
||||
double elapsed = (current_time.tv_sec - last_time->tv_sec) +
|
||||
(current_time.tv_nsec - last_time->tv_nsec) / 1e9;
|
||||
|
||||
if (elapsed < frame_time) {
|
||||
return 0;
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC, last_time);
|
||||
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
int server_update(s_game_server* gs) {
|
||||
if(limit_fps(60, &gs->current_time)) {
|
||||
for(int i = 0; i < 4; i++) {
|
||||
if(gs->clients[0].sock > 0 && gs->clients[1].sock > 0) {
|
||||
|
||||
if(rst2.userArg != 0) {
|
||||
FreeUserTransaction(&rst2);
|
||||
}
|
||||
if(RecvUserTransaction(&rst2, s.clients[0].sock)){
|
||||
st2 = rst2;
|
||||
}
|
||||
SendUserTransaction(&st2, s.clients[1].sock);
|
||||
SendUserTransaction(&st1, s.clients[0].sock);
|
||||
|
||||
if(rst1.userArg != 0) {
|
||||
FreeUserTransaction(&rst1);
|
||||
}
|
||||
if(RecvUserTransaction(&rst1, s.clients[1].sock)) {
|
||||
st1 = rst1;
|
||||
}
|
||||
SendUserTransaction(&st1, s.clients[0].sock);
|
||||
SendUserTransaction(&st2, s.clients[1].sock);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//int get_player_data(s_game_server* s, int client) {
|
||||
//
|
||||
// server_transaction st;
|
||||
// server_transaction rst;
|
||||
// memset(&st,0,sizeof(st));
|
||||
// memset(&rst,0,sizeof(rst));
|
||||
//
|
||||
// st.id = SERVER_T_SERVER_FLAG & SERVER_T_REQUEST;
|
||||
// st.action = SERVER_T_GET_PLAYER_DATA;
|
||||
//
|
||||
// printf("Getting Player Data\n");
|
||||
//
|
||||
//
|
||||
// if(!SendServerTransaction(&st, client)) {
|
||||
// return -1;
|
||||
// }
|
||||
//
|
||||
// if(RecvServerTransaction(&rst, client)) {
|
||||
// return -1;
|
||||
// }
|
||||
// return 0;
|
||||
//}
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user