summaryrefslogtreecommitdiff
path: root/grid/message.cpp
blob: 40b6af6f7cc5077141e7aa9eb095e9bbc057b105 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* message

   todo: authenticate; encrypt?
*/

#include "stdafx.h"
#include "message.h"
#include <time.h>
#include "../util/goodies.h"

// if you want trace output:
#define mmm(x)

/* listener ------------------------------------------------------------------- */

void Listener::listen() {
	SockAddr me(port);
	int sock = socket(AF_INET, SOCK_STREAM, 0);
	if( sock == INVALID_SOCKET ) {
		cout << "ERROR: listen(): invalid socket? " << errno << endl;
		return;
	}
	prebindOptions( sock );
	if( bind(sock, (sockaddr *) &me.sa, me.addressSize) != 0 ) { 
		cout << "listen(): bind() failed errno:" << errno << endl;
		if( errno == 98 )
			cout << "98 == addr already in use" << endl;
		closesocket(sock);
		return;
	}

	if( ::listen(sock, 128) != 0 ) { 
		cout << "listen(): listen() failed " << errno << endl;
		closesocket(sock);
		return;
	}

	SockAddr from;
	while( 1 ) { 
		int s = accept(sock, (sockaddr *) &from.sa, &from.addressSize);
		if( s < 0 ) {
			cout << "Listener: accept() returns " << s << " errno:" << errno << endl;
			continue;
		}
		disableNagle(s);
		cout << "Listener: connection accepted from " << from.toString() << endl;
		accepted( new MessagingPort(s, from) );
	}
}

/* messagingport -------------------------------------------------------------- */

MSGID NextMsgId;
struct MsgStart {
	MsgStart() {
		NextMsgId = (((unsigned) time(0)) << 16) ^ curTimeMillis();
		assert(MsgDataHeaderSize == 16);
	}
} msgstart;

set<MessagingPort*> ports;

void closeAllSockets() { 
	for( set<MessagingPort*>::iterator i = ports.begin(); i != ports.end(); i++ )
		(*i)->shutdown();
}

MessagingPort::MessagingPort(int _sock, SockAddr& _far) : sock(_sock), farEnd(_far) { 
	ports.insert(this);
}

MessagingPort::MessagingPort() {
	ports.insert(this);
	sock = -1;
}

void MessagingPort::shutdown() { 
	if( sock >= 0 ) { 
		closesocket(sock);
		sock = -1;
	}
}

MessagingPort::~MessagingPort() { 
	shutdown();
	ports.erase(this);
}

bool MessagingPort::connect(SockAddr& _far)
{
	farEnd = _far;

	sock = socket(AF_INET, SOCK_STREAM, 0);
	if( sock == INVALID_SOCKET ) {
		cout << "ERROR: connect(): invalid socket? " << errno << endl;
		return false;
	}
	if( ::connect(sock, (sockaddr *) &farEnd.sa, farEnd.addressSize) ) { 
		cout << "ERROR: connect(): connect() failed" << errno << endl;
		closesocket(sock); sock = -1;
		return false;
	}
	disableNagle(sock);
	return true;
}

bool MessagingPort::recv(Message& m) {
	mmm( cout << "*  recv() sock:" << this->sock << endl; )
	int len = -1;

	char *lenbuf = (char *) &len;
	int lft = 4;
	while( 1 ) {
		int x = ::recv(sock, lenbuf, lft, 0);
		if( x == 0 ) {
			cout << "MessagingPort::recv(): conn closed? " << farEnd.toString() << endl;
			m.reset();
			return false;
		}
		if( x < 0 ) { 
			cout << "MessagingPort::recv(): recv() error " << errno << ' ' << farEnd.toString()<<endl;
			m.reset();
			return false;
		}
		lft -= x;
		if( lft == 0 )
			break;
		lenbuf += x;
		cout << "MessagingPort::recv(): got " << x << " bytes wanted 4, lft=" << lft << endl;
		assert( lft > 0 );
	}

//	assert( x == 4 );

	if( len < 0 || len > 16000000 ) { 
		cout << "bad recv() len: " << len << '\n';
		return false;
	}
        
	int z = (len+1023)&0xfffffc00; assert(z>=len);
	MsgData *md = (MsgData *) malloc(z);
	md->len = len;
        
	if ( len <= 0 ){
		cout << "got a length of 0, something is wrong" << endl;
		return false;
	}

	char *p = (char *) &md->id;
	int left = len -4;
	while( 1 ) {
		int x = ::recv(sock, p, left, 0);
		if( x == 0 ) {
			cout << "MessagingPort::recv(): conn closed? " << farEnd.toString() << endl;
			m.reset();
			return false;
		}
		if( x < 0 ) { 
			cout << "MessagingPort::recv(): recv() error " << errno << ' ' << farEnd.toString() << endl;
			m.reset();
			return false;
		}
		left -= x;
		p += x;
		if( left <= 0 )
			break;
	}

	m.setData(md, true);
	return true;
}

void MessagingPort::reply(Message& received, Message& response) {
	say(received.from, response, received.data->id);
}

void MessagingPort::reply(Message& received, Message& response, MSGID responseTo) {
	say(received.from, response, responseTo);
}

bool MessagingPort::call(SockAddr& to, Message& toSend, Message& response) {
	mmm( cout << "*call()" << endl; )
	MSGID old = toSend.data->id;
	say(to, toSend);
	while( 1 ) {
		bool ok = recv(response);
		if( !ok )
			return false;
		//cout << "got response: " << response.data->responseTo << endl;
		if( response.data->responseTo == toSend.data->id ) 
			break;
		cout << "********************" << endl;
		cout << "ERROR: MessagingPort::call() wrong id got:" << response.data->responseTo << " expect:" << toSend.data->id << endl;
		cout << "  old:" << old << endl;
		cout << "  response msgid:" << response.data->id << endl;
		cout << "  response len:  " << response.data->len << endl;
		assert(false);
		response.reset();
	}
	mmm( cout << "*call() end" << endl; )
	return true;
}

void MessagingPort::say(SockAddr& to, Message& toSend, int responseTo) {
	mmm( cout << "*  say() sock:" << this->sock << " thr:" << GetCurrentThreadId() << endl; )
	MSGID msgid = NextMsgId;
	++NextMsgId;
	toSend.data->id = msgid;
	toSend.data->responseTo = responseTo;
	int x = ::send(sock, (char *) toSend.data, toSend.data->len, 0);
	if( x <= 0 ) { 
		cout << "MessagingPort::say: send() error " << errno << ' ' << farEnd.toString() << endl;
	}
}