summaryrefslogtreecommitdiff
path: root/include/socket.h
blob: 6e4abf1ebd3f913abfb765ac30a51b1b0ad60148 (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
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
 *                       E-mail:
 *                <brain@chatspike.net>
 *           	  <Craig@chatspike.net>
 *     
 * Written by Craig Edwards, Craig McLure, and others.
 * This program is free but copyrighted software; see
 *            the file COPYING for details.
 *
 * ---------------------------------------------------
 */

#ifndef __INSP_SOCKET_H__
#define __INSP_SOCKET_H__

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <poll.h>
#include <sstream>
#include <string>

enum InspSocketState { I_DISCONNECTED, I_CONNECTING, I_CONNECTED, I_LISTENING, I_ERROR };
enum InspSocketError { I_ERR_TIMEOUT, I_ERR_SOCKET, I_ERR_CONNECT, I_ERR_BIND };

class InspSocket
{
private:
        int fd;
	std::string host;
	int port;
	InspSocketState state;
        sockaddr_in addr;
        in_addr addy;
        time_t timeout_end;
        bool timeout;
	pollfd polls;
	char ibuf[16384];
	std::string IP;
	sockaddr_in client;
	sockaddr_in server;
	socklen_t length;
public:
	InspSocket();
	InspSocket(int newfd, char* ip);
	InspSocket(std::string host, int port, bool listening, unsigned long maxtime);
	virtual bool OnConnected();
	virtual void OnError(InspSocketError e);
	virtual int OnDisconnect();
	virtual bool OnDataReady();
	virtual void OnTimeout();
	virtual void OnClose();
	virtual char* Read();
	std::string GetIP();
	bool Timeout(time_t current);
	virtual int Write(std::string data);
	virtual int OnIncomingConnection(int newfd, char* ip);
	void SetState(InspSocketState s);
	InspSocketState GetState();
	bool Poll();
	int GetFd();
	virtual void Close();
	virtual ~InspSocket();
};

#endif