File: ClientSocket.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 (199 lines) | stat: -rwxr-xr-x 4,553 bytes parent folder | download | duplicates (2)
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
// ClientSocket.cpp : implementation file
//

#include "stdafx.h"
#include "aime_w32.h"
#include "ClientSocket.h"
#include "mudtypes.h"
#include "strings.h"
#include "newfuncts.h"
#include "global.h"
#include "sysdep.h"
#include "utils.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CClientSocket

CClientSocket::CClientSocket()
{
	valid_input = FALSE;
	locked = FALSE;
}

CClientSocket::~CClientSocket()
{
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CClientSocket, CSocket)
	//{{AFX_MSG_MAP(CClientSocket)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0

/////////////////////////////////////////////////////////////////////////////
// CClientSocket member functions

void CClientSocket::OnConnect(int nErrorCode)
{
	AfxMessageBox("Connection complete");
}


void CClientSocket::OnReceive(int nErrorCode)
{
   char input_buffer[MAX_COM_LEN+4];
   int  holder_count = 0;
   BOOL the_end = FALSE;

   // We need to avoid letting two connection-accessing cycles run concurrently so while this
   // is locked, we sleep it off
   while (set_locked() == FALSE)
		Sleep(10);

   for (int x=0; x<the_config.maxreadsperpoll; x++)
   {
		BZERO(input_buffer,MAX_COM_LEN+3);

		the_end = FALSE;
		if (Receive(input_buffer, MAX_COM_LEN - 1) == SOCKET_ERROR)
		{
			if (GetLastError() == WSAEMSGSIZE)
				the_end = FALSE;
			else
				the_end = TRUE;
		}
		else
		{
			the_end = TRUE;
		}

        if (*input_buffer != '\0')
        {
           if (fill_read_buffer(input_buffer) < 0)
		   {
			  unlock();
              return;
		   }
		}

		if (the_end == TRUE)
			break;

   } // main for
   valid_input = TRUE;

   unlock();
   if (read_buffer.get_first() == NULL)
      return;

   return;
}


/***********************************************************************
 ** set_locked - locks this socket.  If it has already been locked,
 **              returns FALSE, otherwise returns TRUE
 ***********************************************************************/

BOOL CClientSocket::set_locked()
{
	if (locked)
		return FALSE;

	locked = TRUE;
	return TRUE;
}


/***********************************************************************
 ** unlock - unlocks the socket so it is free for use
 ***********************************************************************/

void CClientSocket::unlock()
{
	locked = FALSE;
}


/***********************************************************************
 ** fill_read_buffer - fills up the read buffer with any input in the socket
 **
 ** Parameters: the_conn - the connection to check for
 **
 ** Returns: 1 for has it, 0 if not found
 **
 ***********************************************************************/

int CClientSocket::fill_read_buffer(char *input_buf)
{
  char copy_buffer[MAX_COM_LEN+4];
  char *str_ptr = input_buf;
  char *new_ptr = NULL;
  int  counter = 0;
  Strings *new_str = NULL;

  /* remove all wierd telnet function chars */
#ifdef DEBUG_INPUT
  printf("Packet: %s\n", str_ptr);
  printf("Strlen: %d\n", strlen(str_ptr));
#endif

  // Read the input buffer until we hit the end of it
  while ((str_ptr) && (*str_ptr != '\0'))
  {
    // Set up a buffer to write valid chars to
    BZERO(copy_buffer, MAX_COM_LEN+4);
    new_ptr = copy_buffer;

    // Now search along the input for the length of the line
    while ((str_ptr) && (*str_ptr != '\n') && (*str_ptr != '\0'))
    {
      // Skip this char if it is not an authorized char
      if ( ((((int) (*str_ptr)) < 32) || (((int) (*str_ptr)) > 126)) &&
		  (*str_ptr != '\b'))
		str_ptr++;

      // Else, it is an authorized char, copy it
      else
      {
		*new_ptr = *str_ptr;
		new_ptr++;
		str_ptr++;
      }
    }

    // Now we theoretically have the entire line, if the length is greater
    // than zero, lets copy it to a buffer.  If it is just a newline, they simply
	// hit enter but we still want to register that
    if ((strlen(copy_buffer) > 0) || (*str_ptr == '\n'))
    {
      // If we don't have a newline, add this to the partial buffer
      if (*str_ptr != '\n')
      {
		 partial_line.str_cat(copy_buffer);
      }
      else
      {
		 new_str = new Strings(partial_line.str_show());
		 new_str->str_cat(copy_buffer);
		 partial_line.truncate(0);
		 new_str->remove_newline();
		 read_buffer.add_tail_entry(new_str);
		 counter++;
      }
    }

    if ((str_ptr) && (*str_ptr != '\0'))
      str_ptr++;
  }
  return counter;
}