Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Grpc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tci-gateway-module
Grpc
Commits
deba4a99
Commit
deba4a99
authored
9 years ago
by
Yang Gao
Browse files
Options
Downloads
Patches
Plain Diff
Update code according to comment
parent
be8ac56e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
cpp/helloworld/greeter_async_server.cc
+44
-37
44 additions, 37 deletions
cpp/helloworld/greeter_async_server.cc
with
44 additions
and
37 deletions
cpp/helloworld/greeter_async_server.cc
+
44
−
37
View file @
deba4a99
...
@@ -57,16 +57,13 @@ using helloworld::HelloRequest;
...
@@ -57,16 +57,13 @@ using helloworld::HelloRequest;
using
helloworld
::
HelloReply
;
using
helloworld
::
HelloReply
;
using
helloworld
::
Greeter
;
using
helloworld
::
Greeter
;
static
bool
got_sigint
=
false
;
class
ServerImpl
final
{
class
ServerImpl
final
{
public:
public:
ServerImpl
()
:
service_
(
&
service_
cq_
)
{}
ServerImpl
()
:
service_
(
&
cq_
)
{}
~
ServerImpl
()
{
~
ServerImpl
()
{
server_
->
Shutdown
();
server_
->
Shutdown
();
rpc_cq_
.
Shutdown
();
cq_
.
Shutdown
();
service_cq_
.
Shutdown
();
}
}
// There is no shutdown handling in this code.
// There is no shutdown handling in this code.
...
@@ -79,46 +76,56 @@ class ServerImpl final {
...
@@ -79,46 +76,56 @@ class ServerImpl final {
server_
=
builder
.
BuildAndStart
();
server_
=
builder
.
BuildAndStart
();
std
::
cout
<<
"Server listening on "
<<
server_address
<<
std
::
endl
;
std
::
cout
<<
"Server listening on "
<<
server_address
<<
std
::
endl
;
while
(
true
)
{
HandleRpcs
();
CallData
*
rpc
=
new
CallData
();
service_
.
RequestSayHello
(
&
rpc
->
ctx
,
&
rpc
->
request
,
&
rpc
->
responder
,
&
rpc_cq_
,
rpc
);
void
*
got_tag
;
bool
ok
;
service_cq_
.
Next
(
&
got_tag
,
&
ok
);
GPR_ASSERT
(
ok
);
GPR_ASSERT
(
got_tag
==
rpc
);
std
::
thread
t
(
&
ServerImpl
::
HandleRpc
,
this
,
rpc
);
t
.
detach
();
}
}
}
private
:
private
:
struct
CallData
{
class
CallData
{
CallData
()
:
responder
(
&
ctx
)
{}
public:
ServerContext
ctx
;
CallData
(
Greeter
::
AsyncService
*
service
,
CompletionQueue
*
cq
)
HelloRequest
request
;
:
service_
(
service
),
cq_
(
cq
),
responder_
(
&
ctx_
),
status_
(
CREATE
)
{
HelloReply
reply
;
Proceed
();
ServerAsyncResponseWriter
<
HelloReply
>
responder
;
}
void
Proceed
()
{
if
(
status_
==
CREATE
)
{
service_
->
RequestSayHello
(
&
ctx_
,
&
request_
,
&
responder_
,
cq_
,
this
);
status_
=
PROCESS
;
}
else
if
(
status_
==
PROCESS
)
{
new
CallData
(
service_
,
cq_
);
std
::
string
prefix
(
"Hello "
);
reply_
.
set_message
(
prefix
+
request_
.
name
());
responder_
.
Finish
(
reply_
,
Status
::
OK
,
this
);
status_
=
FINISH
;
}
else
{
delete
this
;
}
}
private
:
Greeter
::
AsyncService
*
service_
;
CompletionQueue
*
cq_
;
ServerContext
ctx_
;
HelloRequest
request_
;
HelloReply
reply_
;
ServerAsyncResponseWriter
<
HelloReply
>
responder_
;
enum
CallStatus
{
CREATE
,
PROCESS
,
FINISH
};
CallStatus
status_
;
};
};
// Runs in a detached thread, processes rpc then deletes data.
// This can be run in multiple threads if needed.
void
HandleRpc
(
CallData
*
rpc
)
{
void
HandleRpcs
()
{
std
::
string
prefix
(
"Hello "
);
new
CallData
(
&
service_
,
&
cq_
);
rpc
->
reply
.
set_message
(
prefix
+
rpc
->
request
.
name
());
void
*
tag
;
rpc
->
responder
.
Finish
(
rpc
->
reply
,
Status
::
OK
,
&
rpc
->
ctx
);
void
*
got_tag
;
bool
ok
;
bool
ok
;
rpc_cq_
.
Next
(
&
got_tag
,
&
ok
);
while
(
true
)
{
GPR_ASSERT
(
ok
);
cq_
.
Next
(
&
tag
,
&
ok
);
GPR_ASSERT
(
got_tag
==
&
rpc
->
ctx
);
GPR_ASSERT
(
ok
);
static_cast
<
CallData
*>
(
tag
)
->
Proceed
();
delete
rpc
;
}
}
}
CompletionQueue
service_cq_
;
CompletionQueue
cq_
;
CompletionQueue
rpc_cq_
;
Greeter
::
AsyncService
service_
;
Greeter
::
AsyncService
service_
;
std
::
unique_ptr
<
Server
>
server_
;
std
::
unique_ptr
<
Server
>
server_
;
};
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment