proxygen
Main Page
Related Pages
Namespaces
Classes
Files
Examples
File List
File Members
TelnetServer.cpp
Go to the documentation of this file.
1
/*
2
* Copyright 2017-present Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
17
#include <gflags/gflags.h>
18
19
#include <
folly/init/Init.h
>
20
#include <
wangle/bootstrap/ServerBootstrap.h
>
21
#include <
wangle/channel/AsyncSocketHandler.h
>
22
#include <
wangle/codec/LineBasedFrameDecoder.h
>
23
#include <
wangle/codec/StringCodec.h
>
24
25
using namespace
folly
;
26
using namespace
wangle
;
27
28
DEFINE_int32
(port, 23,
"test telnet server port"
);
29
30
typedef
Pipeline<IOBufQueue&, std::string>
TelnetPipeline
;
31
32
class
TelnetHandler
:
public
HandlerAdapter
<std::string> {
33
public
:
34
void
read
(
Context
* ctx,
std::string
msg)
override
{
35
if
(msg.empty()) {
36
write
(ctx,
"Please type something.\r\n"
);
37
}
else
if
(msg ==
"bye"
) {
38
write
(ctx,
"Have a fabulous day!\r\n"
).thenValue([ctx,
this
](
auto
&&) {
39
close
(ctx);
40
});
41
}
else
{
42
write
(ctx,
"Did you say '"
+ msg +
"'?\r\n"
);
43
}
44
}
45
46
void
transportActive
(
Context
* ctx)
override
{
47
auto
sock = ctx->
getTransport
();
48
SocketAddress
localAddress;
49
sock->getLocalAddress(&localAddress);
50
write
(ctx,
"Welcome to "
+ localAddress.
describe
() +
"!\r\n"
);
51
write
(ctx,
"Type 'bye' to disconnect.\r\n"
);
52
}
53
};
54
55
class
TelnetPipelineFactory
:
public
PipelineFactory
<TelnetPipeline> {
56
public
:
57
TelnetPipeline::Ptr
newPipeline
(
58
std::shared_ptr<AsyncTransportWrapper> sock)
override
{
59
auto
pipeline =
TelnetPipeline::create
();
60
pipeline->addBack(
AsyncSocketHandler
(sock));
61
pipeline->addBack(
LineBasedFrameDecoder
(8192));
62
pipeline->addBack(
StringCodec
());
63
pipeline->addBack(
TelnetHandler
());
64
pipeline->finalize();
65
66
return
pipeline;
67
}
68
};
69
70
int
main
(
int
argc,
char
**
argv
) {
71
folly::Init
init
(&argc, &argv);
72
73
ServerBootstrap<TelnetPipeline>
server;
74
server.
childPipeline
(std::make_shared<TelnetPipelineFactory>());
75
server.
bind
(FLAGS_port);
76
server.
waitForStop
();
77
78
return
0;
79
}
main
int main(int argc, char **argv)
Definition:
TelnetServer.cpp:70
TelnetPipelineFactory
Definition:
TelnetClient.cpp:51
AsyncSocketHandler.h
fizz::detail::write
void write(const T &in, folly::io::Appender &appender)
Definition:
Types-inl.h:112
TelnetPipeline
Pipeline< IOBufQueue &, std::string > TelnetPipeline
Definition:
TelnetServer.cpp:30
folly::Init
Definition:
Init.h:47
wangle::HandlerContext::getTransport
std::shared_ptr< folly::AsyncTransport > getTransport()
Definition:
HandlerContext.h:45
wangle::ServerBootstrap::bind
void bind(folly::AsyncServerSocket::UniquePtr s)
Definition:
ServerBootstrap.h:163
TelnetPipelineFactory::newPipeline
TelnetPipeline::Ptr newPipeline(std::shared_ptr< AsyncTransportWrapper > sock) override
Definition:
TelnetServer.cpp:57
wangle::Pipeline
Definition:
Pipeline.h:170
wangle::HandlerContext< std::string, std::string >
wangle::ServerBootstrap
Definition:
ServerBootstrap.h:42
folly::SocketAddress
Definition:
SocketAddress.h:32
LineBasedFrameDecoder.h
folly
—— Concurrent Priority Queue Implementation ——
Definition:
AtomicBitSet.h:29
folly::SocketAddress::describe
std::string describe() const
Definition:
SocketAddress.cpp:482
wangle::ServerBootstrap::childPipeline
ServerBootstrap * childPipeline(std::shared_ptr< PipelineFactory< Pipeline >> factory)
Definition:
ServerBootstrap.h:91
wangle
Definition:
Acceptor.cpp:49
TelnetHandler
Definition:
TelnetClient.cpp:36
folly::init
void init(int *argc, char ***argv, bool removeFlags)
Definition:
Init.cpp:34
argv
char ** argv
Definition:
SmallLocksBenchmark.cpp:430
wangle::HandlerAdapter
Definition:
Handler.h:159
wangle::AsyncSocketHandler
Definition:
AsyncSocketHandler.h:29
ServerBootstrap.h
wangle::StringCodec
Definition:
StringCodec.h:26
TelnetHandler::transportActive
void transportActive(Context *ctx) override
Definition:
TelnetServer.cpp:46
string
const char * string
Definition:
Conv.cpp:212
wangle::Pipeline::create
static Ptr create()
Definition:
Pipeline.h:174
StringCodec.h
wangle::LineBasedFrameDecoder
Definition:
LineBasedFrameDecoder.h:30
wangle::Pipeline::Ptr
std::shared_ptr< Pipeline > Ptr
Definition:
Pipeline.h:172
DEFINE_int32
DEFINE_int32(port, 23,"test telnet server port")
TelnetHandler::read
void read(Context *ctx, std::string msg) override
Definition:
TelnetServer.cpp:34
folly::netops::close
int close(NetworkSocket s)
Definition:
NetOps.cpp:90
Init.h
wangle::PipelineFactory
Definition:
Pipeline.h:244
wangle::ServerBootstrap::waitForStop
void waitForStop()
Definition:
ServerBootstrap.h:297
proxygen
wangle
wangle
example
telnet
TelnetServer.cpp
Generated by
1.8.11