1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef ECHO_SERVER_H_ 6 #define ECHO_SERVER_H_ 7 8 #include "ppapi/cpp/instance.h" 9 #include "ppapi/cpp/tcp_socket.h" 10 #include "ppapi/utility/completion_callback_factory.h" 11 12 static const int kBufferSize = 1024; 13 14 // Simple "echo" server based on a listening pp::TCPSocket. 15 // This server handles just one connection at a time and will 16 // echo back whatever bytes get sent to it. 17 class EchoServer { 18 public: 19 EchoServer(pp::Instance* instance, uint16_t port) 20 : instance_(instance), 21 callback_factory_(this) { 22 Start(port); 23 } 24 25 protected: 26 void Start(uint16_t port); 27 28 // Callback functions 29 void OnBindCompletion(int32_t result); 30 void OnListenCompletion(int32_t result); 31 void OnAcceptCompletion(int32_t result, pp::TCPSocket socket); 32 void OnReadCompletion(int32_t result); 33 void OnWriteCompletion(int32_t result); 34 35 void TryRead(); 36 void TryAccept(); 37 38 pp::Instance* instance_; 39 pp::CompletionCallbackFactory<EchoServer> callback_factory_; 40 pp::TCPSocket listening_socket_; 41 pp::TCPSocket incoming_socket_; 42 43 char receive_buffer_[kBufferSize]; 44 }; 45 46 #endif // ECHO_SERVER_H_ 47