2 ** \file echoserver.cpp
4 ** \author grymse@alhem.net
7 Copyright (C) 2006 Anders Hedstrom
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #pragma warning(disable:4786)
26 #include <StdoutLog.h>
27 #include <ListenSocket.h>
28 #include <SocketHandler.h>
29 #include <TcpSocket.h>
33 #include <HttpDebugSocket.h>
37 static bool g_b_detach
= false;
40 static bool g_b_ssl
= false;
42 static std::string gFilename
= "server.pem";
43 static std::string gPw
;
44 static bool quit
= false;
45 static bool g_b_http
= false;
46 static bool g_b_nobuf
= false;
48 static bool g_b_detach2
= false;
54 class MySocket
: public TcpSocket
57 MySocket(ISocketHandler
& h
) : TcpSocket(h
) {
74 fprintf(stderr
, "\nDetach failed\n");
79 void OnRawData(const char *buf
,size_t len
) {
86 void OnLine(const std::string
& line
) {
88 if (g_b_detach2
&& !IsDetach())
93 fprintf(stderr
, "\nDetach failed\n");
97 DEB(printf("fd %d OnLine; %s\n", GetSocket(), Handler().IsSlave() ? "slave" : "master");)
103 DEB(printf("fd %d OnDetached; %s\n", GetSocket(), Handler().IsSlave() ? "slave" : "master");)
104 // fprintf(stderr, "-");
115 void InitSSLServer() {
116 InitializeContext("echoserver", gFilename
, gPw
, SSLv23_method());
132 int main(int argc
,char *argv
[])
135 signal(SIGPIPE
, SIG_IGN
);
136 signal(SIGINT
, sigint
);
139 bool enableLog
= false;
141 for (int i
= 1; i
< argc
; i
++)
144 if (!strcmp(argv
[i
], "-detach"))
146 if (!strcmp(argv
[i
], "-detach2"))
150 if (!strcmp(argv
[i
], "-ssl"))
152 if (!strcmp(argv
[i
], "-file") && i
< argc
- 1)
153 gFilename
= argv
[++i
];
155 if (!strcmp(argv
[i
], "-port") && i
< argc
- 1)
156 port
= atoi(argv
[++i
]);
157 if (!strcmp(argv
[i
], "-pw") && i
< argc
- 1)
159 if (!strcmp(argv
[i
], "-log"))
161 if (!strcmp(argv
[i
], "-queue") && i
< argc
- 1)
162 queue
= atoi(argv
[++i
]);
163 if (!strcmp(argv
[i
], "-http"))
165 if (!strcmp(argv
[i
], "-nobuf"))
167 if (!strcmp(argv
[i
], "-h"))
169 printf("Usage: %s [ options ] [-ssl]\n", *argv
);
170 printf(" -port nn listen on port nn\n");
172 printf(" -detach detach each socket on accept\n");
173 printf(" -detach2 detach when line received\n");
176 printf(" -ssl run as ssl server, .pem file needed\n");
177 printf(" -file xx .pem filename, default is \"server.pem\"\n");
179 printf(" -pw xx private key password\n");
180 printf(" -log enable sockethandler debug log\n");
181 printf(" -queue nn listen queue size (default 20)\n");
182 printf(" -http enable http server with dummy response\n");
183 printf(" -nobuf echo raw data\n");
187 if (g_b_http
&& g_b_nobuf
)
189 printf("Nobuf and Http does not work together\n");
192 StdoutLog
*log
= enableLog
? new StdoutLog() : NULL
;
193 SocketHandler
h(log
);
194 ListenSocket
<MySocket
> l(h
);
195 ListenSocket
<HttpDebugSocket
> l2(h
);
198 if (l
.Bind(port
, queue
))
200 fprintf(stderr
, "Bind to port %d failed\n", port
);
203 fprintf(stderr
, "Listening on port %d\n", port
);
208 printf("Enable HttpDebugSocket\n");
209 if (l2
.Bind(port
, queue
))
211 fprintf(stderr
, "Bind to port %d failed\n", port
);
214 fprintf(stderr
, "Listening on port %d\n", port
);
219 fprintf(stderr
, "Will detach each incoming socket\n");
223 fprintf(stderr
, "Using SSL\n");
229 fprintf(stderr
, "\nExiting...\n");