-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommunication_Bridge.cpp
More file actions
166 lines (149 loc) · 4.8 KB
/
Copy pathCommunication_Bridge.cpp
File metadata and controls
166 lines (149 loc) · 4.8 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <string>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include "Communication_Bridge.h"
#include "busOperation.h"
int Send_2_Client(int fds, unsigned char boardmap[BOARDSIZE][BOARDSIZE], const char game_msg[], bool dummyline, const char text_msg[])
{
int Sent_Bytenum;
unsigned int i, j;
//Server_Message *msg;
char msg[BOARDSIZE * BOARDSIZE + 10 + 20 + 1];
memcpy(msg, game_msg, strlen(game_msg));
for (i = strlen(game_msg); i < 10; i++)
msg[i] = ' ';
memcpy(&msg[10], boardmap, BOARDSIZE * BOARDSIZE);
i = 10 + BOARDSIZE * BOARDSIZE;
memcpy(&msg[i], text_msg, strlen(text_msg));
j = strlen(text_msg);
i += j;
for ( ; j < 20; j++, i++)
msg[i] = ' ';
msg[i] = '\0';
/*for ( ; i < BOARDSIZE; i++)
for (j = 0; j < BOARDSIZE; j++)
msg[i * BOARDSIZE + j] = boardmap[i][j];
for ( ; i < 10; i++)
msg[i] = ' ';
for ( ; i < strlen(text_msg); i++)
msg[i] = text_msg[i];
msg[i] = '\0';*/
/*
msg = (Server_Message *)malloc(sizeof(Server_Message));
if ( boardmap )
memcpy(msg->Map, boardmap, BOARDSIZE * BOARDSIZE);
if ( game_msg ){
//msg->game_msg = (unsigned char *)malloc(strlen(game_msg) * sizeof(unsigned char));
strcpy(msg->game_msg, game_msg, strlen(game_msg));
}
printf("Send_2_Client game_message: %s\n", msg->game_msg);
msg->dummyline = dummyline;
if ( text_msg ){
//msg->game_msg = (unsigned char *)malloc(strlen(text_msg) * sizeof(unsigned char));
strcpy(msg->text_msg, text_msg, strlen(text_msg));
}
*/
if ( (Sent_Bytenum = send(fds, msg, sizeof(msg), 0)) < 0 ){
perror("Server: SendError"); exit(1);
}
//if ( msg->game_msg ) free(msg->game_msg);
//if ( msg->text_msg ) free(msg->text_msg);
//free(msg);
return Sent_Bytenum;
}
int Send_2_Server(int fds, char Operation[])
{
int Sent_Bytenum;
//unsigned int i;
//char msg[11];
/*Client_Message *msg;
printf("Send_2_Server: %s\n", Operation);
msg = (Client_Message *)malloc(sizeof(Client_Message));
if ( Operation ){
//msg->operation = (unsigned char *)malloc(strlen(Operation) * sizeof(unsigned char));
memcpy(msg->operation, Operation, strlen(Operation));
}
*/
/*
for (i = 0; i < strlen(Operation); i++)
msg[i] = Operation[i];
for ( ; i < 10; i++)
msg[i] = ' ';
msg[i] = '\0';
printf("Send_2_Server: %s\n", msg);
*/
Sent_Bytenum = send(fds, Operation, sizeof(Operation), 0);
//if ( msg->operation ) free(msg->operation);
//free(msg);
return Sent_Bytenum;
}
int Reqs_4_Client(int fds, char buf[])
{
int Recv_Bytenum;//, i;
//unsigned char buf[255];//*buf;
//buf = (unsigned char *)malloc(255);
if ( (Recv_Bytenum = recv(fds, buf, sizeof(buf), 0)) > 0 ){
buf[Recv_Bytenum] = '\0';
}
else{
perror("Server: RecvError"); exit(1);
}
/*
for (i = 0; i < Recv_Bytenum; i++)
if( buf[i] == ' '){
buf[i] = '\0';
break;
}
*/
printf("Reqs_4_Client: %s.for %dBytes\n", buf, Recv_Bytenum);
return Recv_Bytenum;
}
int Reqs_4_Server(int fds, char game_msg[])
{
int Recv_Bytenum;
unsigned int i, j;
unsigned char Map[BOARDSIZE][BOARDSIZE];
unsigned int Map_ram[38];
char Test_msg[20];
memset(Map, 0, BOARDSIZE * BOARDSIZE);
memset(Map_ram, 0, 38);
//Server_Message *sev_msg;
//sev_msg = (Server_Message *)malloc(sizeof(Server_Message));
unsigned char buf[450];//*buf;
//buf = (unsigned char *)malloc(255);
if ( (Recv_Bytenum = recv(fds, buf, sizeof(buf), 0)) > 0 ){
printf("Reqs_4_Server_Recv_Bytenum: %d\n", Recv_Bytenum);
//memcpy(sev_msg, buf, sizeof(Server_Message));
//strcpy(game_msg, sev_msg->game_msg);
for (i = 0; buf[i] != ' '; i++)
game_msg[i] = buf[i];
game_msg[i] = '\0';
Recv_Bytenum = i;
printf("Reqs_4_Server: %s.for %dBytes.\n", game_msg, Recv_Bytenum);
memcpy(Map, &buf[10], BOARDSIZE * BOARDSIZE);
printf("After Sending from server to client.\n");
busMap(Map, Map_ram);
busSend(Map_ram);
j = 0;
for (i = 371; j < 16; i++)
Test_msg[j++] = buf[i];
Test_msg[j] = '\0';
if ( Test_msg[0] != ' ' )
busText(Test_msg);
}
return Recv_Bytenum;
}