summaryrefslogtreecommitdiffstats
path: root/net/curvecp/test_server.h
blob: 13a39c39bbe3bbb9d7b1656d7d42e9782af2764e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_CURVECP_TEST_SERVER_H_
#define NET_CURVECP_TEST_SERVER_H_

#include "base/compiler_specific.h"
#include "net/base/test_data_stream.h"
#include "net/curvecp/curvecp_server_socket.h"

namespace net {

class DrainableIOBuffer;
class EchoServer;
class IOBuffer;

// TestServer is the server which processes the listen socket.
// It will create an EchoServer instance to handle each connection.
class TestServer : public CurveCPServerSocket::Acceptor {
 public:
  TestServer();
  virtual ~TestServer();

  bool Start(int port);

  // CurveCPServerSocket::Acceptor implementation.
  virtual void OnAccept(CurveCPServerSocket* new_socket) OVERRIDE;

  // Returns the number of errors this server encountered.
  int error_count() { return errors_; }

 private:
  CurveCPServerSocket* socket_;
  int errors_;
};


// EchoServer does the actual server work for a connection.
// This object self destructs after finishing its work.
class EchoServer {
 public:
  EchoServer();
  ~EchoServer();

  // Start the Echo Server
  void Start(CurveCPServerSocket* socket);

 private:
  void OnReadComplete(int result);
  void OnWriteComplete(int result);

  void ReadData();

 private:
  static const int kMaxMessage = 1024;
  CurveCPServerSocket* socket_;
  scoped_refptr<IOBuffer> read_buffer_;
  scoped_refptr<DrainableIOBuffer> write_buffer_;
  TestDataStream received_stream_;
  int bytes_received_;
};

}  // namespace net

#endif  // NET_CURVECP_TEST_SERVER_H_