summaryrefslogtreecommitdiffstats
path: root/net/curvecp/test_server.h
blob: 3ece594cbd9591d80cb00f64cc6eef0f84848532 (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
67
68
69
70
71
72
73
74
// 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_
#pragma once

#include "base/task.h"
#include "net/base/completion_callback.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 OldCompletionCallback,
                   public CurveCPServerSocket::Acceptor {
 public:
  TestServer();
  virtual ~TestServer();

  bool Start(int port);

  // OldCompletionCallback methods:
  virtual void RunWithParams(const Tuple1<int>& params);

  // CurveCPServerSocket::Acceptor methods:
  virtual void OnAccept(CurveCPServerSocket* new_socket);

  // 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_;
  OldCompletionCallbackImpl<EchoServer> read_callback_;
  OldCompletionCallbackImpl<EchoServer> write_callback_;
};

}  // namespace net

#endif  // NET_CURVECP_TEST_SERVER_H_