Subversion Repositories mdb

Rev

Rev 23 | Details | Compare with Previous | 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
		{
25 andreas 89
			while (find_string(sBuffer, "$0a", 1) || find_string(sBuffer, ';', 1))
90
			{
91
			stack_var char buf[4096];
92
			stack_var integer pos1;
93
			stack_var integer pos2;
23 andreas 94
 
25 andreas 95
				pos1 = find_string(sBuffer, "$0a", 1);
96
				pos2 = find_string(sBuffer, ';', 1);
97
 
98
				if ((pos1 && pos2 && pos1 < pos2) || (pos1 && !pos2))
99
				{
100
					buf = remove_string(sBuffer, "$0a", 1);
101
					set_length_string(buf, length_string(buf) - 1);
102
					send_string vdvMDB,"'INFO:',buf,';'";
103
				}
104
				else if (pos2)
105
					send_string vdvMDB, remove_string(sBuffer, ';', 1);
106
			}
23 andreas 107
		}
108
	}
109
 
110
	data_event[vdvMDB]
111
	{
112
		command:
113
		{
114
			send_string dvMDB, DATA.TEXT;
115
			clear_buffer DATA.TEXT;
116
		}
117
	}
118
 
119
	timeline_event[nID]
120
	{
121
		connect();
122
	}