Subversion Repositories mdb

Rev

Rev 23 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright (C) 2015 by Andreas Theofilu <andreas@theosys.at>
 *
 * All rights reserved. No warranty, explicit or implicit, provided.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Andreas Theofilu and his suppliers, if any.
 * The intellectual and technical concepts contained
 * herein are proprietary to Andreas Theofilu and its suppliers and
 * may be covered by European and Foreign Patents, patents in process,
 * and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Andreas Theofilu.
 */
MODULE_NAME='MDB_Comm'  (dev dvMDB, dev vdvMDB, char IP[], integer nPort)

DEFINE_CONSTANT
        constant char MDB_VERSION[] = '1.0.0';

        constant long nID = 1;
        constant long nTick[] = { 10000, 10000 };

    constant char iperror[17][32] = {
                'undefined',                                    // 1 - N/U
                'General failure',                              // 2
                'undefined',                                    // 3 - N/U
                'Unknown Host',                                 // 4
                'undefined',                                    // 5 - N/U
                'connection refused',                   // 6
                'connection timed out',                 // 7
            'Unknown connection error',         // 8
                'Port already closed',                  // 9
                'Binding error',                                // 10
                'Listening error',                              // 11
                'Socket not connected',                 // 12
                'Send to Socket unknown',               // 13
            'Local port already used',          // 14 - Do not try to reconnect!
                'UDP Socket already listening', // 15
                'too many open sockets',                // 16
                'Local Port not open'                   // 17
        };

DEFINE_VARIABLE
        volatile char sBuffer[32767];
        volatile integer nOnline;

define_function connect()
{
        if (nOnline)
                return;

        ip_client_open(dvMDB.PORT, IP, nPort, IP_TCP);
        nOnline = 2;
}

DEFINE_START
        // Print out driver and version
        send_string 0,"'TheoSys MDB v',MDB_VERSION";
        send_string 0,"'(C) 2015 by Andreas Theofilu. All rights reserved!'";

        create_buffer dvMDB, sBuffer;
        timeline_create(nID, nTick, 1, TIMELINE_ABSOLUTE, TIMELINE_REPEAT);

DEFINE_EVENT
        data_event[dvMDB]
        {
                online:
                {
                        nOnline = 1;
                }

                offline:
                {
                        nOnline = 0;

                }

                onerror:
                {
                        amx_log(AMX_ERROR, "'MDB <',IP,'> error: ',iperror[DATA.NUMBER]");

                        if (DATA.NUMBER != 14)
                                nOnline = 0;
                }

                string:
                {
                        while (find_string(sBuffer, "$0a", 1) || find_string(sBuffer, ';', 1))
                        {
                        stack_var char buf[4096];
                        stack_var integer pos1;
                        stack_var integer pos2;

                                pos1 = find_string(sBuffer, "$0a", 1);
                                pos2 = find_string(sBuffer, ';', 1);

                                if ((pos1 && pos2 && pos1 < pos2) || (pos1 && !pos2))
                                {
                                        buf = remove_string(sBuffer, "$0a", 1);
                                        set_length_string(buf, length_string(buf) - 1);
                                        send_string vdvMDB,"'INFO:',buf,';'";
                                }
                                else if (pos2)
                                        send_string vdvMDB, remove_string(sBuffer, ';', 1);
                        }
                }
        }

        data_event[vdvMDB]
        {
                command:
                {
                        send_string dvMDB, DATA.TEXT;
                        clear_buffer DATA.TEXT;
                }
        }

        timeline_event[nID]
        {
                connect();
        }