First command!
This commit is contained in:
parent
1256c65f62
commit
ca1e959861
|
@ -69,6 +69,8 @@ struct {
|
||||||
#define LOGMSG "[%s]"
|
#define LOGMSG "[%s]"
|
||||||
#define printLog(FORMAT, ...) printf(LOGMSG " " FORMAT, __func__, ##__VA_ARGS__)
|
#define printLog(FORMAT, ...) printf(LOGMSG " " FORMAT, __func__, ##__VA_ARGS__)
|
||||||
|
|
||||||
|
#define LEN(x) (sizeof(x) / sizeof((x)[0]))
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------------- */
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -114,12 +116,6 @@ struct {
|
||||||
// Server
|
// Server
|
||||||
//
|
//
|
||||||
|
|
||||||
struct {
|
|
||||||
const char *name;
|
|
||||||
void* (*func)(void*);
|
|
||||||
const char *help;
|
|
||||||
} typedef Command_t;
|
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
pthread_t *id;
|
pthread_t *id;
|
||||||
bool pleaseStop;
|
bool pleaseStop;
|
||||||
|
|
|
@ -24,12 +24,12 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// -------------------------------------------------------------------------- //
|
// -------------------------------------------------------------------------- //
|
||||||
// Server init function //
|
// Server init function //
|
||||||
// -------------------------------------------------------------------------- //
|
// -------------------------------------------------------------------------- //
|
||||||
pthread_t *ServerInit(Server_t *server);
|
pthread_t *ServerInit(Server_t *server);
|
||||||
|
|
||||||
// -------------------------------------------------------------------------- //
|
// -------------------------------------------------------------------------- //
|
||||||
// Server destructor function //
|
// Server destructor function //
|
||||||
// -------------------------------------------------------------------------- //
|
// -------------------------------------------------------------------------- //
|
||||||
static inline void ServerDestroy(Server_t *server)
|
static inline void ServerDestroy(Server_t *server)
|
||||||
{
|
{
|
||||||
|
@ -37,7 +37,7 @@ static inline void ServerDestroy(Server_t *server)
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------------- //
|
// -------------------------------------------------------------------------- //
|
||||||
// Server wait function //
|
// Server wait function //
|
||||||
// -------------------------------------------------------------------------- //
|
// -------------------------------------------------------------------------- //
|
||||||
static inline void ServerWait(Server_t *server)
|
static inline void ServerWait(Server_t *server)
|
||||||
{
|
{
|
||||||
|
|
48
src/server.c
48
src/server.c
|
@ -70,11 +70,6 @@ static inline int bindSocket(int newSocket)
|
||||||
return effectiveSocket;
|
return effectiveSocket;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// Commands understood by the server
|
|
||||||
//
|
|
||||||
// TODO dict of commands
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------------- //
|
// -------------------------------------------------------------------------- //
|
||||||
// Server main function //
|
// Server main function //
|
||||||
// -------------------------------------------------------------------------- //
|
// -------------------------------------------------------------------------- //
|
||||||
|
@ -86,8 +81,6 @@ static void *serverMain(void *server)
|
||||||
char clientRequest[255]= {0};
|
char clientRequest[255]= {0};
|
||||||
char serverAnswer[255] = {0};
|
char serverAnswer[255] = {0};
|
||||||
|
|
||||||
const char *pMessage = "hello";
|
|
||||||
|
|
||||||
// Get args
|
// Get args
|
||||||
args = (Server_t*) server;
|
args = (Server_t*) server;
|
||||||
printLog("Server #%lu online\n", *args->id);
|
printLog("Server #%lu online\n", *args->id);
|
||||||
|
@ -95,16 +88,14 @@ static void *serverMain(void *server)
|
||||||
//Create socket
|
//Create socket
|
||||||
socketDescriptor = createSocket();
|
socketDescriptor = createSocket();
|
||||||
|
|
||||||
if (socketDescriptor < 0)
|
if (socketDescriptor < 0) {
|
||||||
{
|
|
||||||
printLog("Could not create socket\n");
|
printLog("Could not create socket\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
printLog("Socket created\n");
|
printLog("Socket created\n");
|
||||||
|
|
||||||
//Bind
|
//Bind
|
||||||
if( bindSocket(socketDescriptor) < 0)
|
if( bindSocket(socketDescriptor) < 0) {
|
||||||
{
|
|
||||||
//print the error serverAnswer
|
//print the error serverAnswer
|
||||||
printLog("Socket bind failed\n");
|
printLog("Socket bind failed\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -116,16 +107,14 @@ static void *serverMain(void *server)
|
||||||
listen(socketDescriptor, 3);
|
listen(socketDescriptor, 3);
|
||||||
|
|
||||||
//Accept and incoming connection
|
//Accept and incoming connection
|
||||||
while(!args->pleaseStop)
|
while(!args->pleaseStop) {
|
||||||
{
|
|
||||||
printLog("Waiting for incoming connections...\n");
|
printLog("Waiting for incoming connections...\n");
|
||||||
clientLen = sizeof(struct sockaddr_in);
|
clientLen = sizeof(struct sockaddr_in);
|
||||||
|
|
||||||
//accept connection from an incoming client
|
//accept connection from an incoming client
|
||||||
effectiveSocket = accept(socketDescriptor,(struct sockaddr *)&client,(socklen_t*)&clientLen);
|
effectiveSocket = accept(socketDescriptor,(struct sockaddr *)&client,(socklen_t*)&clientLen);
|
||||||
|
|
||||||
if (effectiveSocket < 0)
|
if (effectiveSocket < 0) {
|
||||||
{
|
|
||||||
printLog("Acceptation failed\n");
|
printLog("Acceptation failed\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -133,26 +122,25 @@ static void *serverMain(void *server)
|
||||||
printLog("Connection accepted\n");
|
printLog("Connection accepted\n");
|
||||||
|
|
||||||
memset(clientRequest, '\0', sizeof(clientRequest));
|
memset(clientRequest, '\0', sizeof(clientRequest));
|
||||||
memset(serverAnswer, '\0', sizeof(serverAnswer));
|
strcpy(serverAnswer,"Invalid command\0");
|
||||||
|
|
||||||
//Receive a reply from the client
|
//Receive REQUEST from client
|
||||||
if (recv(effectiveSocket, clientRequest, 200, 0) < 0)
|
if (recv(effectiveSocket, clientRequest, 200, 0) < 0) {
|
||||||
{
|
|
||||||
printLog("Reception failed");
|
printLog("Reception failed");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
printLog("Client reply : %s\n",clientRequest);
|
|
||||||
if (strcmp(pMessage,clientRequest) == 0)
|
printLog("Client request : %s\n",clientRequest);
|
||||||
{
|
|
||||||
strcpy(serverAnswer,"Hello there.");
|
for (int i = 0; i < LEN(cmdList); i++) {
|
||||||
|
if (strcmp(cmdList[i].name, clientRequest) == 0) {
|
||||||
|
cmdList[i].execute(NULL);
|
||||||
|
strcpy(serverAnswer,"OK\0");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
// REPLY to client
|
||||||
strcpy(serverAnswer,"Invalid command");
|
if (send(effectiveSocket, serverAnswer, strlen(serverAnswer), 0) < 0) {
|
||||||
}
|
|
||||||
// Send some data
|
|
||||||
if (send(effectiveSocket, serverAnswer, strlen(serverAnswer), 0) < 0)
|
|
||||||
{
|
|
||||||
printLog("Send failed\n");
|
printLog("Send failed\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue