64eed9be35872133a73d8212a875c28586c90d12
[reactos.git] / base / applications / network / telnet / src / tnetwork.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 //Telnet Win32 : an ANSI telnet client.
3 //Copyright (C) 1998-2000 Paul Brannan
4 //Copyright (C) 1998 I.Ioannou
5 //Copyright (C) 1997 Brad Johnson
6 //
7 //This program is free software; you can redistribute it and/or
8 //modify it under the terms of the GNU General Public License
9 //as published by the Free Software Foundation; either version 2
10 //of the License, or (at your option) any later version.
11 //
12 //This program is distributed in the hope that it will be useful,
13 //but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 //GNU General Public License for more details.
16 //
17 //You should have received a copy of the GNU General Public License
18 //along with this program; if not, write to the Free Software
19 //Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 //I.Ioannou
22 //roryt@hol.gr
23 //
24 ///////////////////////////////////////////////////////////////////////////
25
26 ///////////////////////////////////////////////////////////////////////////////
27 //
28 // Module: tnetwork.cpp
29 //
30 // Contents: telnet network module
31 //
32 // Product: telnet
33 //
34 // Revisions: March 18, 1999 Paul Brannan (pbranna@clemson.edu)
35 //
36 ///////////////////////////////////////////////////////////////////////////////
37
38 #include "tnetwork.h"
39
40 void TNetwork::SetSocket(SOCKET s) {
41 socket = s;
42 net_type = TN_NETSOCKET;
43 local_echo = line_mode = 1;
44 }
45
46 void TNetwork::SetPipe(HANDLE pIn, HANDLE pOut) {
47 pipeIn = pIn;
48 pipeOut = pOut;
49 net_type = TN_NETPIPE;
50 local_echo = line_mode = 0;
51 }
52
53 int TNetwork::WriteString(const char *str, const int length) {
54 switch(net_type) {
55 case TN_NETSOCKET:
56 return send(socket, str, length, 0);
57 case TN_NETPIPE:
58 {
59 DWORD dwWritten;
60 if(!WriteFile(pipeOut, str, length, &dwWritten, (LPOVERLAPPED)NULL)) return -1;
61 return dwWritten;
62 }
63 }
64 return 0;
65 }
66
67 int TNetwork::ReadString (char *str, const int length) {
68 switch(net_type) {
69 case TN_NETSOCKET:
70 return recv(socket, str, length, 0);
71 case TN_NETPIPE:
72 {
73 DWORD dwRead;
74 if(!ReadFile(pipeIn, str, length, &dwRead, (LPOVERLAPPED)NULL)) return -1;
75 return dwRead;
76 }
77 }
78 return 0;
79 }
80
81 void TNetwork::do_naws(int width, int height) {
82 if(!naws_func) return;
83 char buf[100];
84 int len = (*naws_func)(buf, width, height);
85 WriteString(buf, len);
86 }
87
88 void TNetwork::SetLocalAddress(char *buf) {
89 local_address = new char[strlen(buf) + 1];
90 strcpy(local_address, buf);
91 }
92