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.
8 #include "ppapi/cpp/instance.h"
9 #include "ppapi/cpp/tcp_socket.h"
10 #include "ppapi/utility/completion_callback_factory.h"
12 static const int kBufferSize = 1024;
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.
18 typedef void (*LogFunction)(const char*);
21 EchoServer(pp::Instance* instance,
23 LogFunction log_function=NULL,
24 pthread_cond_t* ready_cond=NULL,
25 pthread_mutex_t* ready_lock=NULL)
26 : instance_(instance),
27 callback_factory_(this),
29 ready_cond_(ready_cond),
30 ready_lock_(ready_lock),
31 log_function_(log_function) {
36 void Start(uint16_t port);
39 void OnBindCompletion(int32_t result);
40 void OnListenCompletion(int32_t result);
41 void OnAcceptCompletion(int32_t result, pp::TCPSocket socket);
42 void OnReadCompletion(int32_t result);
43 void OnWriteCompletion(int32_t result);
48 void Log(const char* msg) {
53 pp::Instance* instance_;
54 pp::CompletionCallbackFactory<EchoServer> callback_factory_;
55 pp::TCPSocket listening_socket_;
56 pp::TCPSocket incoming_socket_;
58 char receive_buffer_[kBufferSize];
60 pthread_cond_t* ready_cond_;
61 pthread_mutex_t* ready_lock_;
62 LogFunction log_function_;
65 #endif // ECHO_SERVER_H_