⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bpamain.c

📁 big pond WAN connect function
💻 C
字号:
/*
**	BPALogin v2.0 - lightweight portable BIDS2 login client
**	Copyright (c) 1999  Shane Hyde (shyde@trontech.com.au)
** 
**  This program is free software; you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation; either version 2 of the License, or
**  (at your option) any later version.
** 
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
** 
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software
**  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
*/ 

#include <stdio.h>
#include <bpalogin.h>
#include <knl_sup.h>

static struct session *s;
static  KNL_MSGQ_ID bpa_msgQ = 0;

typedef struct
{
    int op;
}
BPA_CMD_T;

#define BPA_SHUTDOWN        1



/*-----------------------------------------------------------------
* ROUTINE NAME - debug
*------------------------------------------------------------------
* FUNCTION: 
* INPUT:
* OUTPUT:
---------------------------------------------------------------*/
void debug (int l, char *s,...)
{
	va_list ap;
	va_start(ap, s);

    vprintf(s, ap);

	va_end(ap);
}

void onconnected(int i)
{
	if (strcmp(s->connectedprog,""))
	{
		char buf[500];
		sprintf(buf,"%.500s %d",s->connectedprog,s->listenport);

		debug(0, "Executing external command - %s\n",buf);
	}
}

void ondisconnected(int reason)
{
	if(strcmp(s->disconnectedprog,""))
	{
		char buf[500];
		sprintf(buf,"%.500s %d",s->disconnectedprog,reason);

		debug(0, "Executing external command - %s\n", buf);
	}
}


void critical(char *s)
{
	debug (0,"Critical error: %s", s);
}

void noncritical(char *s,...)
{
	debug (0,"Critical error: %s", s);
}

void socketerror(struct session *s, const char *str)
{
	debug (0, "Socket error: %s", s);
}

/*-----------------------------------------------------------------
* ROUTINE NAME - bpa_main
*------------------------------------------------------------------
* FUNCTION: 
* INPUT:
* OUTPUT:
---------------------------------------------------------------*/
int bpa_main(struct session *s)
{
	if (!strcmp(s->username, ""))
	{
		s->critical("Username has not been set");
        goto error;
	}

	if (!strcmp(s->password, ""))
	{
		s->critical("Password has not been set");
        goto error;
	}

    // Enter mainloop
    while (bpa_mainloop(s))
        ;

	s->ondisconnected(0);

error:
    // Kill message queue
    free(s);
    KNL_MSGQ_DELETE(bpa_msgQ);
    bpa_msgQ = 0;

    KNL_TASK_EXIT();
}


int bpa_start(char *username, char *password, char *server_ip)
{
    KNL_TASK_ID tid;
    UINT32  argv[1];

    if (bpa_msgQ != 0)
        return 0;

    // Initialize session 
    if ((s = (struct session *)malloc(sizeof(struct session))) == 0)
        return -1;

	memset(s, 0, sizeof(struct session));
    strcpy(s->authserver, server_ip);
    strcpy(s->authdomain, DEFAULT_AUTHDOMAIN);
    s->authport = DEFAULT_AUTHPORT;
    strcpy(s->username, username);
    strcpy(s->password, password);
	strcpy(s->connectedprog, "");
	strcpy(s->disconnectedprog, "");
	strcpy(s->localaddress, "");
	s->localport = DEFAULT_AUTHPORT;
	s->minheartbeat = 60;
	s->shutdown = 0;

	s->debug = debug;
	s->critical = critical;
	s->noncritical = noncritical;
	s->onconnected = onconnected;
	s->ondisconnected = ondisconnected;

    //
    // Create message queue
    //
    if (KNL_MSGQ_CREATE("bpa", &bpa_msgQ, 1, sizeof(BPA_CMD_T)) != 0)
    {
        free(s);

        bpa_msgQ = 0;
        return -1;
    }

    // 
    // Run bpa_main
    //
    argv[0] = (int)s;
    if (KNL_TASK_CREATE("tbpa", &tid, 0, (FUNCPTR)bpa_main, 1, argv) != 0)
    {
        free(s);

        KNL_MSGQ_DELETE(bpa_msgQ);
        bpa_msgQ = 0;
        return -1;
    }

    return 0;
}

int bpa_isshutdown(struct session *s)
{
    BPA_CMD_T cmd;

    if (KNL_MSGQ_RECV(bpa_msgQ, &cmd, sizeof(cmd), KNL_NOWAIT) != ERROR)
    {
        if (cmd.op == BPA_SHUTDOWN)
        {
            s->shutdown = 1;
            return TRUE;
        }
    }

    return FALSE;
}

int bpa_stop()
{
    BPA_CMD_T cmd;

    if (bpa_msgQ == 0)
        return 0;

    cmd.op = BPA_SHUTDOWN;
    KNL_MSGQ_SEND(bpa_msgQ, &cmd, sizeof(cmd), KNL_WAIT);

    // Wait until queue vanished
    while (bpa_msgQ)
        KNL_SLEEP(1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -