File: winport.cpp

package info (click to toggle)
aime 0.60.3-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,016 kB
  • ctags: 5,217
  • sloc: cpp: 77,611; ansic: 3,765; sh: 2,996; makefile: 234; sed: 93
file content (291 lines) | stat: -rwxr-xr-x 7,227 bytes parent folder | download | duplicates (3)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**********************************************************************
 ** Port class: This class handles a listening port, creating it and
 **             providing methods to poll it. Win32 version.
 **
 **   
 ** Reviewed through: 
 **    
 **
 ** Copyright (C) 2000 George Noel (Slate)
 **
 **   This program is free software; you can redistribute it and/or modify
 **   it under the terms of the GNU General Public License as
 **   published by the Free Software Foundation; either version 2 of the 
 **   License, or any later version. 
 **
 **   This program is distributed in the hope that it will be useful, but 
 **   WITHOUT ANY WARRANTY; without even the implied warranty of 
 **   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 **   General Public License for more details. 
 ** 
 **   You should have received a copy of the GNU General Public License 
 **   along with this program (in the docs dir); if not, write to the Free
 **   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 **
 **********************************************************************/

#ifndef PORT_C
#define PORT_C

#include "config.h"
#include "sysdep.h"
#include "strings.h"
#include "mudobject.h"
#include "individual.h"
#include "player.h"
#include "winport.h"
#include "mud.h"
#include "global.h"

#include "utils.h"

#include "winconnection.h"


/***********************************************************************
 ** check_socket - checks the listening socket for data, and if it finds
 **                any it handles it
 **
 ** Paramters: None
 **
 ** Returns:  1 if successful
 **          -1 if an error
 **
 ***********************************************************************/

int Port::check_socket(/*Timing *tmp_timer*/)
{
/*   struct timeval timeout, null_time;
   long   elapsed = 0;
   int    v;  */          /* holds result of select */

   


   /* check the socket for data */

   		
/*   if ((v = select (list_sock + 1, &input_set, &output_set, 
                                               &exc_set, &null_time)) < 0)
   {
      perror("select, port");
      return -1;
   }
*/
   /* get time left in cycle for sleep */
/*   timeout.tv_sec = 0;
   if ((tmp_timer != NULL) && ((elapsed = tmp_timer->cycle_elapsed()) < 
                                  ((long) (1000000/the_config.pollspersec)))) 
      timeout.tv_usec = (1000000/the_config.pollspersec) - elapsed;
   else
   {
      if (tmp_timer != NULL)
      {
         mainstruct->increment_lagged_cycle();
      }
      timeout.tv_usec = 0;
   }


#if defined( AIME_WIN32 )
   Sleep( timeout.tv_usec / 1000 );
   
#else
   if (select(0, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0, &timeout) < 0) {
      perror("Select sleep, port");
      return -1;
   }
#endif // AIME_WIN32


   if( tmp_timer )
      tmp_timer->set_cycle();
*/
   /* if we find an exception */
/*   if (FD_ISSET (list_sock, &exc_set))
   {
      printf("Exception pending with listening socket\n");
      v--;
   }
*/




   /* if we find a connection, create a new player */
/*      if (list_sock.m_hSocket == the_config.gameport)
      {
         AfxMessageBox("Accepting player");
         mainstruct->add_player();
      }
      else if (port == the_config.buildport)
      {
         AfxMessageBox("Accepting builder");

		 mainstruct->add_builder();
      }
   } */

   return 1;
}


/***********************************************************************
 ** get_serv_hostname - returns the server hostname string
 **
 ** Paramters: the_str - the string to put the hostname into
 **
 ** Returns: pointer to the_str with hostname loaded
 **
 ***********************************************************************/

char *Port::get_serv_hostname(void) {
   return serv_hostname.str_show();
}



/***********************************************************************
 ** connect -  Opens the listening socket and starts it listening. It
 ** (private)  places the socket in the Mud class attribute list_sock
 **            and you can poll for socket data by calling check_socket
 **
 ** Parameters: the_port - the port number to open this to
 **             quiet_mode - display output or not
 **
 ** Returns: 1  if successful
 **          -1 if socket create error
 **          -2 if gethostname error
 **          -3 if gethostbyname error
 **          -5 if listen error
 **          -6 if fcntl error
 **
 ***********************************************************************/

int Port::connect(unsigned short int the_port, bool quiet_mode)
{
   char           tmp_serv[MAXHOSTNAMELEN];
   Strings        holder;

#ifndef WIN32
   if (!quiet_mode)
      printf("Setting up listening socket.\n");
#endif

   if (!list_sock.Create(the_port))
   {
		return -1;
   }
   list_sock.port_num = the_port;

   if (!list_sock.Listen())
   {
	   return -5;	  
   }

   /* get the hostname of the server */
   if (gethostname (tmp_serv, MAXHOSTNAMELEN - 1) != 0) {
      perror("gethostname");
      return -1;
   }
   serv_hostname = tmp_serv;

   /* try to get the IP address of the server from the hostname */

   h = gethostbyname(serv_hostname.str_show());
   if (h == NULL) {
      holder.sprintf("gethostbyname host: %s", serv_hostname.str_show());
      perror(holder.str_show());
      return -2;
   }
   if (!quiet_mode)
      sysmessage("Socket configured and listening at port %d%s", list_sock.port_num, NEWLINE);

   valid = 1;

   return 1;
}

/***********************************************************************
 ** is_valid - tells if the listening port is still valid
 **
 ** Paramters: None
 **
 ** Returns: 1 if valid
 **          0 if not valid
 **
 ***********************************************************************/

int Port::is_valid() {
   return valid;
}


/***********************************************************************
 ** get_socket - gets the listening socket
 **
 ** Paramters: None
 **
 ** Returns: the listening socket
 **
 ***********************************************************************/

int Port::get_socket() {
   return 0;
}


/***********************************************************************
 ** Port (constructor) - creates and opens a listening port
 **
 ** Parameters: the_port - the port to open the listening port up to
 **             quiet_mode - display output or not
 **
 ** Returns: Nothing
 **
 ***********************************************************************/
   
Port::Port(unsigned short int the_port, bool quiet_mode)
{
   if (connect(the_port, quiet_mode) <= 0)
      valid = 0;
   else
      valid = 1;


	sin = NULL;
}   


/***********************************************************************
 ** ~Port (destructor) - closes the port and cleans it up
 **
 ** Parameters: None
 **
 ** Returns: Nothing
 **
 ***********************************************************************/

Port::~Port()
{
//   printf("Disconnecting socket\n");
   list_sock.Close();

//   printf("Socket disconnected\n");
   return;
}


int Port::accept_conn(Connection *new_conn, MudObject *the_user)
{
	 if (!list_sock.Accept(*(new_conn->get_socket())))
	 {
	    new_conn->set_invalid();
		return -1;
	 }

	 return new_conn->accept_conn(the_user);
}


#endif