-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
149 lines (117 loc) · 4.57 KB
/
server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <malloc.h>
/*Includes about LED matrix */
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdint.h>
#include <string.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#define FILEPATH "/dev/fb0" // device path
#define NUM_WORDS 64
#define FILESIZE (NUM_WORDS *sizeof(unsigned short))
/*
Define 16bit for Color
RRRR RGGG GGGB BBBB
*/
#define RGB565_RED 0xF800 /* Red-color FrameBuffer */
#define RGB565_GREEN 0x07E0 /* GREEN-color FrameBuffer */
#define RGB565_BLUE 0x001F /* BLUE-color FrameBuffer */
/* RPI-server.c */
int main()
{
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
/*vars for LED MATRIX*/
int i,j;
int fbfd;
unsigned short *map;
struct fb_fix_screeninfo fix_info;
map = (unsigned short*)malloc(1024);
/* open the led frame buffer device */
fbfd = open(FILEPATH, O_RDWR);
if(fbfd == -1){
perror("Error (call to 'open')");
exit(EXIT_FAILURE);
}
/* read fixed screen info for the open device */
if(ioctl(fbfd, FBIOGET_FSCREENINFO, &fix_info) == -1) {
perror("Error (call to 'ioctl')");
close(fbfd);
exit(EXIT_FAILURE);
}
/* map the led frame buffer device into memory */
map = mmap(NULL, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
if( map== MAP_FAILED){
close(fbfd);
perror("Error mmapping the file");
exit(EXIT_FAILURE);
}
/* For running server */
unlink("server socket");
server_sockfd = socket(AF_INET, SOCK_STREAM , 0);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(2020);
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address,server_len);
for(i =0;i<8;i++)
for(j=0;j<8;j++)
*(map+i*8+j) =0;
/* make wait-Queue and infinite-loop(Running-loop) */
listen(server_sockfd, 5025);
while(1){
printf("server waiting\n");
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, &client_len);
read(client_sockfd, map, 1024);
/*Logic */
/*map[i] = 1P: 1 , 2P: 2, BOMB:3 */
for(i =0 ; i<8; i++) {
for(j =0; j<8;j++){
if(*(map+i*8+j) == 1) *(map+i*8+j)=RGB565_RED;
else if(*(map+i*8+j) == 2) *(map+i*8+j)=RGB565_GREEN;
else if(*(map+i*8+j) == 3) *(map+i*8+j)=RGB565_BLUE;
}
}
if(*(map+64)!=0){
usleep(1000*1000);
printf("GAME OVER!!\n");
if(*(map+64)==1) {
printf("1P WIN!!\n");
for(i=0;i<8;i++)
for(j=0;j<8;j++)
{*(map+i*8+j)=RGB565_RED; usleep(25*1000);}
}
if(*(map+64)==2) {
printf("2P WIN!!\n");
for(i=0;i<8;i++)
for(j=0;j<8;j++)
{*(map+i*8+j)=RGB565_GREEN; usleep(25*1000);}
}
if(*(map+64)==3) {
printf("NOBODY IS ALIVE !!\n DRAW GAME!!\n");
for(i=0;i<8;i++)
for(j=0;j<8;j++)
{*(map+i*8+j)=RGB565_BLUE; usleep(25*1000);}
}
memset(map, 0, FILESIZE+1);
if(munmap(map, FILESIZE+1) == -1){
perror("Error unmapping the file");
}
close(fbfd);
//return 0;
}
write(client_sockfd, map , 1024);
close(client_sockfd);
}
}