Merge pull request #3 from yang-g/c++api

more implementation and all async signatures
pull/501/head
Craig Tiller 10 years ago
commit 8de8c646c5
  1. 236
      include/grpc++/stream.h

@ -177,6 +177,7 @@ class ClientWriter final : public ClientStreamingInterface,
virtual Status Finish() override { virtual Status Finish() override {
CallOpBuffer buf; CallOpBuffer buf;
Status status; Status status;
buf.AddRecvMessage(response_);
buf.AddClientRecvStatus(&status); buf.AddClientRecvStatus(&status);
call_.PerformOps(&buf, (void *)4); call_.PerformOps(&buf, (void *)4);
GPR_ASSERT(cq_.Pluck((void *)4)); GPR_ASSERT(cq_.Pluck((void *)4));
@ -252,110 +253,241 @@ class ServerReader final : public ReaderInterface<R> {
}; };
template <class W> template <class W>
class ServerWriter : public WriterInterface<W> { class ServerWriter final : public WriterInterface<W> {
public: public:
explicit ServerWriter(StreamContextInterface* context) : context_(context) { explicit ServerWriter(Call* call) : call_(call) {}
GPR_ASSERT(context_);
context_->Start(true);
context_->Read(context_->request());
}
virtual bool Write(const W& msg) { virtual bool Write(const W& msg) override {
return context_->Write(const_cast<W*>(&msg), false); CallOpBuffer buf;
buf.AddSendMessage(msg);
call_->PerformOps(&buf, (void *)2);
return call_->cq()->Pluck((void *)2);
} }
private: private:
StreamContextInterface* const context_; // not owned Call* call_;
}; };
// Server-side interface for bi-directional streaming. // Server-side interface for bi-directional streaming.
template <class W, class R> template <class W, class R>
class ServerReaderWriter : public WriterInterface<W>, class ServerReaderWriter final : public WriterInterface<W>,
public ReaderInterface<R> { public ReaderInterface<R> {
public: public:
explicit ServerReaderWriter(StreamContextInterface* context) explicit ServerReaderWriter(Call* call) : call_(call) {}
: context_(context) {
GPR_ASSERT(context_); virtual bool Read(R* msg) override {
context_->Start(true); CallOpBuffer buf;
buf.AddRecvMessage(msg);
call_->PerformOps(&buf, (void *)2);
return call_->cq()->Pluck((void *)2);
}
virtual bool Write(const W& msg) override {
CallOpBuffer buf;
buf.AddSendMessage(msg);
call_->PerformOps(&buf, (void *)3);
return call_->cq()->Pluck((void *)3);
} }
virtual bool Read(R* msg) { return context_->Read(msg); } private:
CompletionQueue* cq_;
Call* call_;
};
// Async interfaces
// Common interface for all client side streaming.
class ClientAsyncStreamingInterface {
public:
virtual ~ClientAsyncStreamingInterface() {}
virtual void Finish(Status* status, void* tag) = 0;
};
// An interface that yields a sequence of R messages.
template <class R>
class AsyncReaderInterface {
public:
virtual ~AsyncReaderInterface() {}
virtual bool Write(const W& msg) { virtual void Read(R* msg, void* tag) = 0;
return context_->Write(const_cast<W*>(&msg), false); };
// An interface that can be fed a sequence of W messages.
template <class W>
class AsyncWriterInterface {
public:
virtual ~Async WriterInterface() {}
virtual void Write(const W& msg, void* tag) = 0;
};
template <class R>
class ClientAsyncReader final : public ClientAsyncStreamingInterface,
public AsyncReaderInterface<R> {
public:
// Blocking create a stream and write the first request out.
ClientAsyncReader(ChannelInterface *channel, const RpcMethod &method,
ClientContext *context,
const google::protobuf::Message &request, void* tag)
: call_(channel->CreateCall(method, context, &cq_)) {
CallOpBuffer buf;
buf.AddSendMessage(request);
buf.AddClientSendClose();
call_.PerformOps(&buf, tag);
}
virtual void Read(R *msg, void* tag) override {
CallOpBuffer buf;
buf.AddRecvMessage(msg);
call_.PerformOps(&buf, tag);
}
virtual void Finish(Status* status, void* tag) override {
CallOpBuffer buf;
buf.AddClientRecvStatus(status);
call_.PerformOps(&buf, tag);
} }
private: private:
StreamContextInterface* const context_; // not owned CompletionQueue cq_;
Call call_;
}; };
template <class W> template <class W>
class ServerAsyncResponseWriter { class ClientWriter final : public ClientAsyncStreamingInterface,
public WriterInterface<W> {
public: public:
explicit ServerAsyncResponseWriter(StreamContextInterface* context) : context_(context) { // Blocking create a stream.
GPR_ASSERT(context_); ClientAsyncWriter(ChannelInterface *channel, const RpcMethod &method,
context_->Start(true); ClientContext *context,
context_->Read(context_->request()); google::protobuf::Message *response)
: response_(response),
call_(channel->CreateCall(method, context, &cq_)) {}
virtual void Write(const W& msg, void* tag) override {
CallOpBuffer buf;
buf.AddSendMessage(msg);
call_.PerformOps(&buf, tag);
} }
virtual bool Write(const W& msg) { virtual void WritesDone(void* tag) {
return context_->Write(const_cast<W*>(&msg), false); CallOpBuffer buf;
buf.AddClientSendClose();
call_.PerformOps(&buf, tag);
}
virtual void Finish(Status* status, void* tag) override {
CallOpBuffer buf;
buf.AddRecvMessage(response_);
buf.AddClientRecvStatus(status);
call_.PerformOps(&buf, tag);
} }
private: private:
StreamContextInterface* const context_; // not owned google::protobuf::Message *const response_;
CompletionQueue cq_;
Call call_;
}; };
template <class R> // Client-side interface for bi-directional streaming.
class ServerAsyncReader : public ReaderInterface<R> { template <class W, class R>
class ClientAsyncReaderWriter final : public ClientAsyncStreamingInterface,
public AsyncWriterInterface<W>,
public AsyncReaderInterface<R> {
public: public:
explicit ServerAsyncReader(StreamContextInterface* context) : context_(context) { ClientAsyncReaderWriter(ChannelInterface *channel,
GPR_ASSERT(context_); const RpcMethod &method, ClientContext *context)
context_->Start(true); : call_(channel->CreateCall(method, context, &cq_)) {}
virtual void Read(R *msg, void* tag) override {
CallOpBuffer buf;
buf.AddRecvMessage(msg);
call_.PerformOps(&buf, tag);
}
virtual void Write(const W& msg, void* tag) override {
CallOpBuffer buf;
buf.AddSendMessage(msg);
call_.PerformOps(&buf, tag);
}
virtual bool WritesDone(void* tag) {
CallOpBuffer buf;
buf.AddClientSendClose();
call_.PerformOps(&buf, tag);
} }
virtual bool Read(R* msg) { return context_->Read(msg); } virtual void Finish(Status* status, void* tag) override {
CallOpBuffer buf;
Status status;
buf.AddClientRecvStatus(status);
call_.PerformOps(&buf, tag);
}
private: private:
StreamContextInterface* const context_; // not owned CompletionQueue cq_;
Call call_;
}; };
// TODO(yangg) Move out of stream.h
template <class W> template <class W>
class ServerAsyncWriter : public WriterInterface<W> { class ServerAsyncResponseWriter final {
public: public:
explicit ServerAsyncWriter(StreamContextInterface* context) : context_(context) { explicit ServerAsyncResponseWriter(Call* call) : call_(call) {}
GPR_ASSERT(context_);
context_->Start(true); virtual void Write(const W& msg, void* tag) override {
context_->Read(context_->request()); CallOpBuffer buf;
buf.AddSendMessage(msg);
call_->PerformOps(&buf, tag);
} }
virtual bool Write(const W& msg) { private:
return context_->Write(const_cast<W*>(&msg), false); Call* call_;
};
template <class R>
class ServerAsyncReader : public AsyncReaderInterface<R> {
public:
explicit ServerAsyncReader(Call* call) : call_(call) {}
virtual void Read(R* msg, void* tag) {
// TODO
} }
private: private:
StreamContextInterface* const context_; // not owned Call* call_;
};
template <class W>
class ServerAsyncWriter : public AsyncWriterInterface<W> {
public:
explicit ServerAsyncWriter(Call* call) : call_(call) {}
virtual void Write(const W& msg, void* tag) {
// TODO
}
private:
Call* call_;
}; };
// Server-side interface for bi-directional streaming. // Server-side interface for bi-directional streaming.
template <class W, class R> template <class W, class R>
class ServerAsyncReaderWriter : public WriterInterface<W>, class ServerAsyncReaderWriter : public AsyncWriterInterface<W>,
public ReaderInterface<R> { public AsyncReaderInterface<R> {
public: public:
explicit ServerAsyncReaderWriter(StreamContextInterface* context) explicit ServerAsyncReaderWriter(Call* call) : call_(call) {}
: context_(context) {
GPR_ASSERT(context_);
context_->Start(true);
}
virtual bool Read(R* msg) { return context_->Read(msg); } virtual void Read(R* msg, void* tag) {
// TODO
}
virtual bool Write(const W& msg) { virtual void Write(const W& msg, void* tag) {
return context_->Write(const_cast<W*>(&msg), false); // TODO
} }
private: private:
StreamContextInterface* const context_; // not owned Call* call_;
}; };
} // namespace grpc } // namespace grpc

Loading…
Cancel
Save