Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / tests / nacl_io_socket_test / echo_server.h
blob435160d4d4b61ffe201dac8ae6a4a59b1808e831
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.
5 #ifndef ECHO_SERVER_H_
6 #define ECHO_SERVER_H_
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.
17 class EchoServer {
18 typedef void (*LogFunction)(const char*);
20 public:
21 EchoServer(pp::Instance* instance,
22 uint16_t port,
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),
28 ready_(false),
29 ready_cond_(ready_cond),
30 ready_lock_(ready_lock),
31 log_function_(log_function) {
32 Start(port);
35 protected:
36 void Start(uint16_t port);
38 // Callback functions
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);
45 void TryRead();
46 void TryAccept();
48 void Log(const char* msg) {
49 if (log_function_)
50 log_function_(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];
59 bool ready_;
60 pthread_cond_t* ready_cond_;
61 pthread_mutex_t* ready_lock_;
62 LogFunction log_function_;
65 #endif // ECHO_SERVER_H_