Subversion Repositories mdb

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
23 andreas 1
/*
2
 * Copyright (C) 2015 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * All rights reserved. No warranty, explicit or implicit, provided.
5
 *
6
 * NOTICE:  All information contained herein is, and remains
7
 * the property of Andreas Theofilu and his suppliers, if any.
8
 * The intellectual and technical concepts contained
9
 * herein are proprietary to Andreas Theofilu and its suppliers and
10
 * may be covered by European and Foreign Patents, patents in process,
11
 * and are protected by trade secret or copyright law.
12
 * Dissemination of this information or reproduction of this material
13
 * is strictly forbidden unless prior written permission is obtained
14
 * from Andreas Theofilu.
15
 */
16
MODULE_NAME='MDB_Comm'	(dev dvMDB, dev vdvMDB, char IP[], integer nPort)
17
 
18
DEFINE_CONSTANT
19
	constant char MDB_VERSION[] = '1.0.0';
20
 
21
	constant long nID = 1;
22
	constant long nTick[] = { 10000, 10000 };
23
 
24
    constant char iperror[17][32] = {
25
		'undefined',					// 1 - N/U
26
		'General failure',				// 2
27
		'undefined',					// 3 - N/U
28
		'Unknown Host',					// 4
29
		'undefined',					// 5 - N/U
30
		'connection refused',			// 6
31
		'connection timed out',			// 7
32
	    'Unknown connection error',		// 8
33
		'Port already closed',			// 9
34
		'Binding error',				// 10
35
		'Listening error',				// 11
36
		'Socket not connected',			// 12
37
		'Send to Socket unknown',		// 13
38
	    'Local port already used',		// 14 - Do not try to reconnect!
39
		'UDP Socket already listening',	// 15
40
		'too many open sockets',		// 16
41
		'Local Port not open'			// 17
42
	};
43
 
44
DEFINE_VARIABLE
45
	volatile char sBuffer[32767];
46
	volatile integer nOnline;
47
 
48
define_function connect()
49
{
50
	if (nOnline)
51
		return;
52
 
53
	ip_client_open(dvMDB.PORT, IP, nPort, IP_TCP);
54
	nOnline = 2;
55
}
56
 
57
DEFINE_START
58
	// Print out driver and version
59
	send_string 0,"'TheoSys MDB v',MDB_VERSION";
60
	send_string 0,"'(C) 2015 by Andreas Theofilu. All rights reserved!'";
61
 
62
	create_buffer dvMDB, sBuffer;
63
	timeline_create(nID, nTick, 1, TIMELINE_ABSOLUTE, TIMELINE_REPEAT);
64
 
65
DEFINE_EVENT
66
	data_event[dvMDB]
67
	{
68
		online:
69
		{
70
			nOnline = 1;
71
		}
72
 
73
		offline:
74
		{
75
			nOnline = 0;
76
 
77
		}
78
 
79
		onerror:
80
		{
81
			amx_log(AMX_ERROR, "'MDB <',IP,'> error: ',iperror[DATA.NUMBER]");
82
 
83
			if (DATA.NUMBER != 14)
84
				nOnline = 0;
85
		}
86
 
87
		string:
88
		{
89
			if (find_string(sBuffer, '#', 0) && find_string(sBuffer, "$0d", 0))
90
				remove_string(sBuffer, "$0d", 0);
91
 
92
			if (length_string(sBuffer))
93
				send_string vdvMDB, sBuffer;
94
		}
95
	}
96
 
97
	data_event[vdvMDB]
98
	{
99
		command:
100
		{
101
			send_string dvMDB, DATA.TEXT;
102
			clear_buffer DATA.TEXT;
103
		}
104
	}
105
 
106
	timeline_event[nID]
107
	{
108
		connect();
109
	}