/**
 * Name   : Edison Chindrawaly
 * Group  : Sehoon Jung & Sangtae Park
 * Class  : CSCI 3780 Spring 2001
 * File   : game_server.
 * Project: #2
 */

#include <iostream.h>
#include <fstream.h>
#include <fcntl.h>
#include <ctype.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <netinet/in.h>

#define STDIN 0
#define STDOUT 1
#define ERROR -1
#define READ 0
#define WRITE 1
#define MAXSIZE 128
#define BACKLOG 5
#define MAX_PLAYER 5;

//for result's respond of the guess:
#define ball 1
#define strike 2

//for error handling flags:
#define socket_flag 0
#define bind_flag 1
#define listen_flag 2
#define accept_flag 3
#define send_flag 4
#define receive_flag 5
#define pipe_flag 6
#define write_flag 7
#define read_flag 8
#define select_flag 9

/**
 * int flag = 0 -> no message
 *     flag = 1 -> there is message
 *     flag = 2 -> chat message to player
 * int guess[3] contains the numbers to guess [3 digits]
 * int result[3] contains server responds to guess
 * char message contains message
 * char name contains player's name
 */
typedef struct
{
 int flag; 
 int guess[3];
 int result[3];
 char message[MAXSIZE];
 char name[MAXSIZE];
} MESSAGE;

/**
 * int pid contains child pid
 * char player_name contains players' names
 */
typedef struct 
{
 int pid;
 int win;
 int end;
 char player_name[MAXSIZE];
} STORAGE;

/**
 * verify's method is to verify the success of the operation
 * @param int sockfd contains socket descriptor if available
 *        int byte contains the byte result of the operation
 *        int flag contains the description of the operation
 * @return none
 */
void verify(int,int,int);

/**
 * beginProcess's method is to begin the process
 * @param int sockfd contains socket descriptor for TCP
 * @return none
 */
void beginProcess(int);

/**
 * closeConnection's method is to close the connection
 * @param  int sockfd contains socket descriptor
 * @return none
 */
void closeConnection(int);

/**
 * sendStruct's method is to send a struct
 * @param  int sockfd contains the socket descriptor
 *         MESSAGE msg contains MESSAGE struct
 * @return int number of byte send
 */
int sendStruct(int, MESSAGE);

/**
 * receivedStruct's method is  to receive a struct
 * @param  int sockfd contains the socket descriptor
 *         MESSAGE msg contains MESSAGE struct
 * @return int number of byte receive
 */
int receivedStruct(int,MESSAGE&);

/**
 * openConnection's method is open TCP connection
 * @param  int port contains the port number
 * @return int socket descriptor
 */
int openConnection(int);


/**
 * generateNumber's method is to generate random number
 * @param  int input
 * @return int random number 1 digit long
 */
int generateNumber(int);

/**
 * checkGuess's method is to check the guess number with the
 * generated number.
 * @param  int[] contains the number to be guess
 *         MESSAGE struct contain the guess for the number
 * @return int 1 if guess is correct else return 0
 */
int checkGuess(int[],MESSAGE&);
 

1