aboutsummaryrefslogtreecommitdiff
path: root/lib/network/socket.h
blob: 08a191f6f1edd3553ace2eb290278876c4778813 (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
#ifndef __socket_h
#define __socket_h

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>

#include <lib/base/ebase.h>
#include <lib/base/eerror.h>
#include <string>
#include <libsig_comp.h>
#include <lib/base/buffer.h>

class eSocket: public Object
{
	int mystate;
	int issocket;
	unsigned int last_break;
private:
	int socketdesc;
	eIOBuffer readbuffer;
	eIOBuffer writebuffer;
	int writebusy;
	sockaddr_in  serv_addr;
protected:
	eSocketNotifier	*rsn;
	virtual void notifier(int);
public:
	eSocket(eMainloop *ml);
	eSocket(int socket, int issocket, eMainloop *ml);
	~eSocket();
	int connectToHost(std::string hostname, int port);
	int getDescriptor();
	int writeBlock(const char *data, unsigned int len);
	int setSocket(int socketfd, int issocket, eMainloop *ml);
	int bytesToWrite();
	int readBlock(char *data, unsigned int maxlen);
	int bytesAvailable();
	bool canReadLine();
	std::string readLine();
	void close();
			// flow control: start/stop data delivery into read buffer.
	void enableRead();
	void disableRead();
	
	void inject(const char *data, int len);
	
	enum State {	Idle, HostLookup, Connecting,
			Listening, Connection, Closing };
	int state();
	
	Signal0<void> connectionClosed_;
	Signal0<void> connected_;
	Signal0<void> readyRead_;
	Signal0<void> hangup;
	Signal1<void,int> bytesWritten_;
	Signal1<void,int> error_;
};

#endif /* __socket_h */