1 #ifndef NETWORK_HEADER_FILE__
2 #define NETWORK_HEADER_FILE__
3 
4 /*
5 This file will contain two sets of headers and data. One for dealing with queued message
6 and the other for handling server sockets. This queue will be in standard, platform-neutral
7 C++. However, the sockets will use Linux/UNIX/BSD specific code, which will have to be
8 updated to run on other operating systems.
9 -- Jesse
10 */
11 
12 
13 #ifndef TRUE
14 #define TRUE 1
15 #endif
16 #ifndef FALSE
17 #define FALSE 0
18 #endif
19 
20 #define MAX_MESSAGE_LENGTH 256
21 
22 struct MESSAGE
23 {
24   char *text;
25   int to;   // which client does the message go to? May not be used as most will go to everyone
26   void *next;
27 };
28 
29 
30 
31 class MESSAGE_QUEUE
32 {
33 public:
34    MESSAGE *first_message, *last_message;
35 
36    MESSAGE_QUEUE();
37    ~MESSAGE_QUEUE();
38 
39    // add a message to the queue
40    bool Add(char *some_text, int to);
41 
42    // pull the first message from the queue and erase it from the queue
43    MESSAGE *Read();
44 
45    // read the next message in the queue without erasing it
46    MESSAGE *Peek();
47 
48    MESSAGE *Read_To(int to);
49 
50    // erases the next message in the queue without reading it
51    void Erase();
52 
53    // erase all messages in the queue
54    void Erase_All();
55 
56 };
57 
58 
59 
60 struct SEND_RECEIVE_TYPE
61 {
62     int listening_port;
63     int shut_down;
64 };
65 
66 
67 #define MAX_CLIENTS 10
68 #define DEFAULT_NETWORK_PORT 25645
69 
70 #ifdef NETWORK
71 
72 
73 /// Wrapper for safe socket writes with return value check.
74 /// A char buffer named "buffer" must be available to put the message in.
75 /// The two size_t values "towrite" and "written" must be at least declared.
76 /// All three variables will be overwritten.
77 #define SAFE_WRITE(sock_, fmt_, ...) { \
78 	sprintf(buffer, fmt_, __VA_ARGS__); \
79 	towrite = strlen(buffer); \
80 	written = write(sock_, buffer, towrite); \
81 	if (written < towrite) \
82 		fprintf(stderr, "%s:%d: Warning: Only %d/%d bytes sent to server\n", \
83 				__FILE__, __LINE__, written, towrite); \
84 }
85 
86 int Setup_Server_Socket(int port);
87 
88 int Setup_Client_Socket(char *server, char *port);
89 
90 int Accept_Incoming_Connection(int my_socket);
91 
92 int Send_Message(MESSAGE *mess, int to_socket);
93 
94 MESSAGE *Receive_Message(int from_socket);
95 
96 void Clean_Up_Server_Socket(int my_socket);
97 
98 void Clean_Up_Client_Socket(int my_socket);
99 
100 int Check_For_Incoming_Data(int socket_number);
101 
102 int Check_For_Errors(int socket_number);
103 
104 void *Send_And_Receive(void *data_we_need);
105 
106 #else
107 	#define SAFE_WRITE(sock_, fmt_, ...) { }
108 #endif
109 
110 
111 
112 #endif
113 
114