Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
NetClient.cpp
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: NetEx.lib
6  FILE: NetClient.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Network Server Pump for HTTP Server
13 */
14 
15 
16 #include "MemDebug.h"
17 #include "NetClient.h"
18 #include "NetHost.h"
19 #include "NetLayer.h"
20 #include <mmsystem.h>
21 
22 // +-------------------------------------------------------------------+
23 
24 NetClient::NetClient(const NetAddr& server_addr)
25  : addr(server_addr), sock(0), delta(0), time(0), err(0)
26 {
27 }
28 
30 {
31  delete sock;
32 }
33 
34 // +--------------------------------------------------------------------+
35 
36 bool
38 {
39  if (msg.length() > 0) {
40  if (sock)
41  delete sock;
42 
43  sock = new(__FILE__,__LINE__) NetSock(addr, true);
44  delta = 0;
45  time = timeGetTime();
46 
47  if (!sock) {
48  err = ERR_NOBUFS;
49  return false;
50  }
51 
52  err = sock->send(msg);
53  if (err < 0) {
55  return false;
56  }
57 
59  if (err < 0) {
61  return false;
62  }
63 
64  return true;
65  }
66 
67  else {
68  delete sock;
69  sock = 0;
70  }
71 
72  return false;
73 }
74 
75 Text
77 {
78  Text response;
79 
80  if (sock) {
81  int ready = sock->select();
82 
83  while (!ready && timeGetTime() - time < 2000) {
84  Sleep(5);
85  ready = sock->select();
86  }
87 
88  if (ready) {
89  Text msg = sock->recv();
90 
91  while (msg.length() > 0) {
92  response += msg;
93  msg = sock->recv();
94  }
95 
96  delta = timeGetTime() - time;
97  }
98 
99  delete sock;
100  sock = 0;
101  }
102 
103  return response;
104 }
105 
106 Text
108 {
109  Text response;
110 
111  if (msg.length() > 0 && Send(msg)) {
112  response = Recv();
113  }
114 
115  return response;
116 }