summaryrefslogtreecommitdiff
path: root/server/src/tcpsocket.h
blob: 45d94eeda08ff72c6acc4253c5aaa839083e8922 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            tcpsocket.h
 *
 *  Thu Oct 19 10:24:25 CEST 2006
 *  Copyright  2006 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of Artefact.
 *
 *  Artefact is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Artefact is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Artefact; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#ifndef __ARTEFACT_TCPSOCKET_H__
#define __ARTEFACT_TCPSOCKET_H__

#include <string>

#include "exception.h"

/**
 * This exception is thrown by TCPSocket when the socket creation fails.
 */
class TCPSocketException: public Exception {
public:
  TCPSocketException(std::string reason) : 
    Exception("Could not create socket: " + reason) {}
};

/**
 * This exception is thrown by TCPSocket when listen fails.
 */
class TCPListenException: public Exception {
public:
  TCPListenException(std::string reason) : 
    Exception("Listen failed: " + reason) {}
};

/**
 * This exception is thrown by TCPSocket when accept fails.
 */
class TCPAcceptException: public Exception {
public:
  TCPAcceptException(std::string reason) : 
    Exception("Accept failed: " + reason) {}
};

/**
 * This exception is thrown by TCPSocket when connection fails.
 */
class TCPConnectException: public Exception {
public:
  TCPConnectException(std::string host, std::string port, std::string reason) : 
    Exception("Could not connect to " + host + ":" + port + ": " + reason) {}
};

/**
 * This exception is thrown by TCPSocket when reading fails.
 */
class TCPReadException: public Exception {
public:
  TCPReadException(std::string reason) : 
    Exception("TCPSocket could not read data: " + reason) {}
};

/**
 * This exception is thrown by TCPSocket when writing fails.
 */
class TCPWriteException: public Exception {
public:
  TCPWriteException(std::string reason) : 
    Exception("TCPSocket could not write data: " + reason) {}
};

/**
 * This exception is thrown by TCPSocket when there is an error in name lookup.
 */
class TCPNameException: public Exception {
public:
  TCPNameException(std::string reason) : 
    Exception("TCPSocket could not get name: " + reason) {}
};

/**
 * This class is used to commumicate through a TCP/IP connection, wether it
 * is a server (listening) or a client (transmitting).
 */
class TCPSocket {
public:
  /**
   * Constructor. Creates a new tcp socket.
   */
  TCPSocket() throw(TCPSocketException);

  /**
   * Destructor. Closes the tcp socket.
   */
  ~TCPSocket();
    
  /**
   * Sets the socket in listen mode.\n
   * @param port The port number on which to listen.
   */
  void listen(unsigned short int port) throw(TCPListenException);
    
  /**
   * Accept an incoming connection.\n
   * The call is blocking and returns only when an incoming connection is received.\n
   * The socket must be in listen mode in order for this call to work.\n
   * Multiple accepts can be made on the same listening socket.
   * @return A connected TCPSocket ready to communicate.
   */
  TCPSocket accept() throw(TCPAcceptException);
    
  /**
   * Connects to a host for data transmission.
   * @param addr The address of the host to connect to.
   * @param port The portnumber of the host to connect to.
   */
  void connect(std::string addr, unsigned short int port) throw(TCPConnectException);
    
  /**
   * Disconnect the socket.
   */
  void disconnect();
    
  /**
   * Tells whether the socket is connected or not.
   * @return true if the socket is connected, false if not.
   */ 
  bool connected();
    
  /**
   * Reads bytes from the socket into a buffer.
   * @param buf The buffer into which the data will be written.
   * @param size The maximum number of bytes to read in (the size of the buffer).
   * @return The actual number of bytes read.
   */
  int read(char *buf, int size) throw(TCPReadException);
    
  /**
   * Writes bytes from a buffer to the socket.
   * @param data The buffer from which the data will be read.
   * @param size The number of bytes to write.
   * @return The actual number of bytes written.
   */
  int write(char *data, int size) throw(TCPWriteException);
    
  /**
   * Get the source address of the socket (IP address not DNS name).
   * @return An STL string containing the source address.
   */
  std::string srcaddr() throw(TCPNameException);

  /**
   * Get the destination address of the socket (IP address not DNS name).
   * @return An STL string containing the destination address.
   */
  std::string dstaddr() throw(TCPNameException);

private:
  bool isconnected;
  int sock;
};


#endif/*__ARTEFACT_TCPSOCKET_H__*/