Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Tuesday, April 16, 2024

File: [Platon] / libplaton / platon / net / Connection.cpp (download)

Revision 1.7, Mon Aug 14 10:03:21 2006 UTC (17 years, 8 months ago) by nepto


Changes since 1.6: +44 -41 lines

Very old, forgotten, most likely not working, but seems to do something

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#ifdef __MSDOS__
#  include <io.h>
#  include <fcntl.h>
#  include <dos.h>
#  define sleep(__x) delay(__x*100)
#else
#  include <sys/time.h>
#  include <unistd.h>
#  include <sys/socket.h>
#  include <netinet/in.h>
#  include <netdb.h>
#  include <arpa/inet.h>
#endif

#include <platon/net/Connection.h>

#ifdef __cplusplus

netConnection::netConnection(const char*host,const int port)
{
    handle=-1;
    __rbuf=NULL;
    setHost(host,port);
#if NET_CONNECTION_DEBUG
    fprintf(stderr,"netConnection::netConnection() STATUS: Object created.\n");
    fprintf(stderr,"  host: %s, port: %d, handle: %d, __rbuf: %p\n",host,port,handle,__rbuf);
#endif
}

netConnection::~netConnection(void)
{
    disconnect();
#if NET_CONNECTION_DEBUG
    fprintf(stderr,"netConnection::~netConnection() STATUS: Object destroyed.\n");
#endif
}

int netConnection::isConnected(void)
{
    return(handle<0?0:1);
}

void netConnection::setPort(const int newport)
{
    port=newport;
}

void netConnection::setHost(const char*newhost,const int newport)
{
    if(newhost!=NULL){
        strncpy(host,newhost,NET_CONNECTION_MAX_HOST_SIZE);
        host[NET_CONNECTION_MAX_HOST_SIZE]='\0';
    }
    if(newport)setPort(newport);
}

int netConnection::connect(void)
{

#ifdef __MSDOS__
    handle=open("TEST.$$$",O_CREAT|O_TEXT);
#else
    struct sockaddr_in sin;
    struct hostent*he;

    he=gethostbyname(host);
#if NET_CONNECTION_DEBUG
    fprintf(stderr,"netConnection::Connect()\n");
    if(he==NULL)
        fprintf(stderr,"  ERROR: Unknown host. [%s]\n",host);
    else
        fprintf(stderr,"  OK: Host found. [%s]\n",host);
#endif
    if(he==NULL)return(0);

    if(port<=0){
#if NET_CONNECTION_DEBUG
        fprintf(stderr,"  WARNING: Bad port (%d) changed to default (%d).\n",port,NET_CONNECTION_PORT);
#endif
        port=NET_CONNECTION_PORT;
    }

    sin.sin_family=AF_INET;
    sin.sin_addr.s_addr=*((unsigned int*)he->h_addr);
    sin.sin_port=htons(port);
    handle=socket(PF_INET,SOCK_STREAM,0);
#if NET_CONNECTION_DEBUG
    if(handle==-1)fprintf(stderr,"  ERROR: Can't create socket.\n");
    else
        fprintf(stderr,"  OK: Socket created.\n");
#endif
    if(handle==-1)return(0);

#if NET_CONNECTION_DEBUG
    fprintf(stderr,"  STATUS: Trying to connect...\n");
#endif
    /* We want connect() syscall, not netConnection method */
    if(::connect(handle,(struct sockaddr*)&sin,sizeof(struct sockaddr_in))==-1){
#if NET_CONNECTION_DEBUG
        fprintf(stderr,"  ERROR: Cannot connect.\n");
#endif
        return(0);
    }
#if NET_CONNECTION_DEBUG
    fprintf(stderr,"  OK: Connection estabilished.\n");
#endif
#endif

    return(isConnected());
}

void netConnection::disconnect(void){
    if(handle<0){
#if NET_CONNECTION_DEBUG
        fprintf(stderr,"netConnection::disconnect() WARNING: Handle not opened, already closed. [%d]\n",handle);
#endif
    }
    else{
        if(close(handle)==-1){
#if NET_CONNECTION_DEBUG
            fprintf(stderr,"netConnection::disconnect() WARNING: Cannot close handle. [%d]\n",handle);
#endif
        }
        else{
#if NET_CONNECTION_DEBUG
            fprintf(stderr,"netConnection::disconnect() OK: Handle closed. [%d]\n",handle);
#endif
        }
        handle=-1;
    }

    if(__rbuf!=NULL){
        free(__rbuf);
        __rbuf=NULL;
    }
}

int netConnection::writeLine(void){
    return(write(handle,"\n",1));
}

int netConnection::writeLine(const char *s){
    register int ret;

    if ((ret = writeStr(s)) != strlen(s))
        return ret;

    ret += writeLine();

    return(ret<0?-1:ret);
}

int netConnection::writeStr(const char*s){
    return(write(handle,s,strlen(s)));
}

int netConnection::writeData(const void*p,int len){
    return(write(handle,p,len));
}

int netConnection::readData(char*buffer, const int size)
{
    register int ret, to_read, i;
    struct timeval tv;
    fd_set read_set;

    if (! isConnected())
        return 0;

    for (to_read = size; to_read > 0; ) {

        FD_ZERO(&read_set);
        FD_SET(handle, &read_set);
        tv.tv_sec  = 0;
        tv.tv_usec = 0;

        ret = select(handle + 1, &read_set, NULL, NULL, &tv);

        if (ret < 0) {
            disconnect();
            break;
        }

        if (ret == 0)
            break;

        i = read(handle, buffer + size - to_read, to_read);

        if (i <= 0) {
            disconnect();
            break;
        }

        to_read -= i;
    }

    return size - to_read;
}

int netConnection::readLine(char*buffer,const int size){
    register int ret;

    if(__rbuf!=NULL){
        strncpy(buffer,__rbuf,size-1);
        buffer[size-1]='\0';
    }
    else buffer[0]='\0';

    if((int)strlen(buffer)<(size-1)){
        ret=readData(strchr(buffer,'\0'),size-strlen(buffer)-1);
    }
    else ret=1;

    if(ret>=0){
        if(strstr(buffer,NET_CONNECTION_EOL)!=NULL){
            __rbuf=(char*)realloc(__rbuf,(strlen(strstr(buffer,NET_CONNECTION_EOL)+strlen(NET_CONNECTION_EOL))+1)*sizeof(char));
            strcpy(__rbuf,strstr(buffer,NET_CONNECTION_EOL)+strlen(NET_CONNECTION_EOL));
            *(strstr(buffer,NET_CONNECTION_EOL)+strlen(NET_CONNECTION_EOL))='\0';
        }
        else{
            if(__rbuf!=NULL){
                if(strlen(buffer)<strlen(__rbuf)){
                    strcpy(__rbuf,__rbuf+strlen(buffer));
                    __rbuf=(char*)realloc(__rbuf,strlen(__rbuf)+1);
                }
                else{
                    free(__rbuf);
                    __rbuf=NULL;
                }
            }
        }
        ret=strlen(buffer);
    }
    else{
        free(__rbuf);
        __rbuf=NULL;
    }

    return(ret);
}

#endif /* #ifdef _cplusplus */


Platon Group <platon@platon.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top