]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGfdmSocket.cpp
FG_HAVE_STD_INCLUDES -> SG_HAVE_STD_INCLUDES
[flightgear.git] / src / FDM / JSBSim / FGfdmSocket.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGfdmSocket.cpp
4  Author:       Jon S. Berndt
5  Date started: 11/08/99
6  Purpose:      Encapsulates a socket
7  Called by:    FGOutput, et. al.
8
9  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
10
11  This program is free software; you can redistribute it and/or modify it under
12  the terms of the GNU General Public License as published by the Free Software
13  Foundation; either version 2 of the License, or (at your option) any later
14  version.
15
16  This program is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  details.
20
21  You should have received a copy of the GNU General Public License along with
22  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23  Place - Suite 330, Boston, MA  02111-1307, USA.
24
25  Further information about the GNU General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This class excapsulates a socket for simple data writing
31
32 HISTORY
33 --------------------------------------------------------------------------------
34 11/08/99   JSB   Created
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 INCLUDES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40 #include "FGfdmSocket.h"
41
42 static const char *IdSrc = "$Header$";
43 static const char *IdHdr = ID_FDMSOCKET;
44
45 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46 CLASS IMPLEMENTATION
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
48
49 FGfdmSocket::FGfdmSocket(string address, int port)
50 {
51   size = 0;
52
53   #if defined(__BORLANDC__) || defined(_MSC_VER)
54     WSADATA wsaData;
55     int PASCAL FAR wsaReturnCode;
56     wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
57     if (wsaReturnCode == 0) cout << "Winsock DLL loaded ..." << endl;
58     else cout << "Winsock DLL not initialized ..." << endl;
59   #endif
60
61   if (address.find_first_not_of("0123456789.",0) != address.npos) {
62     if ((host = gethostbyname(address.c_str())) == NULL) {
63       cout << "Could not get host net address by name..." << endl;
64     }
65   } else {
66     if ((host = gethostbyaddr(address.c_str(), address.size(), PF_INET)) == NULL) {
67       cout << "Could not get host net address by number..." << endl;
68     }
69   }
70
71   if (host != NULL) {
72     cout << "Got host net address..." << endl;
73     sckt = socket(AF_INET, SOCK_STREAM, 0);
74
75     if (sckt >= 0) {  // successful
76       memset(&scktName, 0, sizeof(struct sockaddr_in));
77       scktName.sin_family = AF_INET;
78       scktName.sin_port = htons(port);
79       memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
80       int len = sizeof(struct sockaddr_in);
81       if (connect(sckt, (struct sockaddr*)&scktName, len) == 0) {   // successful
82         cout << "Successfully connected to socket ..." << endl;
83       } else {                // unsuccessful
84         cout << "Could not connect to socket ..." << endl;
85       }
86     } else {          // unsuccessful
87       cout << "Could not create socket for FDM, error = " << errno << endl;
88     }
89   }
90 }
91
92 FGfdmSocket::~FGfdmSocket(void)
93 {
94   #ifndef macintosh
95   if (sckt) shutdown(sckt,2);
96   #endif
97   
98   #ifdef __BORLANDC__
99     WSACleanup();
100   #endif
101 }
102
103 void FGfdmSocket::Clear(void)
104 {
105   buffer = "";
106   size = 0;
107 }
108
109 void FGfdmSocket::Append(const char* item)
110 {
111   if (size == 0) buffer += string(item);
112   else buffer += string(",") + string(item);
113   size++;
114 }
115
116 void FGfdmSocket::Append(float item)
117 {
118   char s[25];
119
120   sprintf(s,"%12.7f\0",item);
121
122   if (size == 0) buffer += string(s);
123   else buffer += string(",") + string(s);
124   size++;
125 }
126
127 void FGfdmSocket::Append(long item)
128 {
129   char s[25];
130
131   sprintf(s,"%12d\0",item);
132
133   if (size == 0) buffer += string(s);
134   else buffer += string(",") + string(s);
135   size++;
136 }
137
138 void FGfdmSocket::Send(void)
139 {
140   buffer += string("\n");
141   if ((send(sckt,buffer.c_str(),buffer.size(),0)) <= 0) {
142     perror("send");
143   } else {
144   }
145 }
146