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

📄 linksocket.c

📁 openmap java写的开源数字地图程序. 用applet实现,可以像google map 那样放大缩小地图.
💻 C
📖 第 1 页 / 共 2 页
字号:
/* ********************************************************************** *  * <copyright> *  *  BBN Technologies, a Verizon Company *  10 Moulton Street *  Cambridge, MA 02138 *  (617) 873-8000 *  *  Copyright (C) BBNT Solutions LLC. All rights reserved. *  * </copyright> * ********************************************************************** *  * $Source: /cvs/distapps/openmap/src/cserver/link/src/LinkSocket.c,v $ * $RCSfile: LinkSocket.c,v $ * $Revision: 1.2 $ * $Date: 2004/01/26 19:07:10 $ * $Author: dietrick $ *  * ********************************************************************** */#include <stdlib.h>#include <stdio.h>#include "LinkSocket.h"#include "GlobalConstants.h"#define DEBUG_ME "LINKSERVER"#include <toolLib/debugging.h>DebugVariable(LINK,"LINK",0x01);DebugVariable(LINKSOCKET,"LINKSOCKET",0x04);/*  flushes the socket's writebuffer.  returns OK if it successfully wrote the entire buffer to socket,  NOK otherwise.*/int Socketflush(LinkSocket *socket){        if(socket->currentbuffer > 0){        int nbyteswritten;        int nbytestowrite = socket->currentbuffer;        if (Debug(LINKSOCKET))            printf("flushing socket writing %d bytes \n", nbytestowrite);                nbyteswritten = socket_send(socket->sd, socket->writebuffer, nbytestowrite);                if(nbytestowrite == nbyteswritten){            socket->currentbuffer = 0;        } else {            socket->error = LINK_TRUE;            return NOK;        }    }    socket->error = LINK_FALSE;    return OK;}/*  copies all from parent to child except sd, since it is new for every child. */int SocketCopyParentToChild(LinkSocket *parent, LinkSocket *child){        child->host = parent->host;    child->mainsd = parent->mainsd;    child->port = parent->port;    child->error = LINK_FALSE;    child->maxbuffer = MAX_SOCKET_BUFFER_SIZE;    child->writebuffer = (char *)malloc(child->maxbuffer);    if (child->writebuffer == NULL)      return -1; /* Memory allocation error. */    child->currentbuffer = 0;    child->isBuffered = parent->isBuffered;    return OK;}int InitSocket(LinkSocket *socket){    set_socket_domain(AF_INET);    socket->host = NULL; /*Keep it Null for the time being*/    if(-1 == open_socket_port(&socket->mainsd, socket->port, NULL, SERVER))    {        socket->error = LINK_TRUE;        return NOK;    }        socket->error = LINK_FALSE;    socket->currentbuffer = 0;    socket->maxbuffer = MAX_SOCKET_BUFFER_SIZE;    socket->isBuffered = LINK_TRUE;    socket->writebuffer = (char *)malloc(socket->maxbuffer);    if (socket->writebuffer == NULL)      return -1; /* Memory allocation error */    return OK;}void FreeSocket(LinkSocket *socket){    if(NULL != socket->host)        free(socket->host);    socket->host = NULL;    if(NULL != socket->writebuffer)        free(socket->writebuffer);    socket->writebuffer = NULL;}/*Checks if the error flag is set return OK or NOK*/int CheckSocket(LinkSocket* socket){    if (socket->error){        if (Debug(LINK)) printf("LinkSocket: problem with link socket...\n");    }    return (socket->error);}/*  returns NOK if datasize is more than maxbuffer and flushes the socket.  returns OK if datasize + socket->currentbuffer is less than maxbuffer   Flushes the buffer and returns OK,   if socket->currentbuffer + datasize(obviously < maxbuffer) is greater than maxbuffer*//*Used internally by WriteInteger etc*/static int WriteToBuffer(LinkSocket *socket, int datasize){    if(datasize > socket->maxbuffer){        Socketflush(socket);        return NOK;    }    if((datasize + socket->currentbuffer) < socket->maxbuffer){        return OK;    } else {        return Socketflush(socket);    }}void IntegerToBytes(char *toBuffer, int ivalue){    toBuffer[0] = (toBuffer[0] & 0x00) | (ivalue >> 24); /*MSB goes first*/    toBuffer[1] = (toBuffer[1] & 0x00) | (ivalue >> 16);     toBuffer[2] = (toBuffer[2] & 0x00) | (ivalue >> 8);    toBuffer[3] = (toBuffer[3] & 0x00) | (ivalue);       /*LSB goes last*/}void BytesToInteger(int *ptrIntegerBuffer, char *fromBuffer){    *ptrIntegerBuffer = 0;    *ptrIntegerBuffer = ((fromBuffer[0] & 0xFF) << 24) | ((fromBuffer[1] & 0xFF) << 16 ) |        ((fromBuffer[2] & 0xFF) << 8) | ((fromBuffer[3] & 0xFF));    }/*  Read an integer value in to provided buffer*/int ReadInteger(LinkSocket *socket, int *ptrIntegerBuffer){        char intbuff[N_BYTES_PER_INTEGER]; /*Read integer in buffer*/    int bytesRead;    /*socket_receive takes void* as 2nd paramater and its ok to pass a char*  */    bytesRead = socket_receive(socket->sd, intbuff, N_BYTES_PER_INTEGER);         if(N_BYTES_PER_INTEGER == bytesRead)    {           /* All right, we got it*/        BytesToInteger(ptrIntegerBuffer,intbuff);            socket->error = LINK_FALSE;          return OK;    }    /* Oops..some error while receiving*/    socket->error = LINK_TRUE;    return NOK;}int WriteInteger(LinkSocket *socket, int iValue){    char intbuff[N_BYTES_PER_INTEGER];    int nbyteswritten;        if(socket->isBuffered == LINK_TRUE && OK == WriteToBuffer(socket, N_BYTES_PER_INTEGER)){        nbyteswritten = BufferedWriteInteger(&(socket->writebuffer[socket->currentbuffer]),                                             iValue);        socket->currentbuffer += nbyteswritten;    } else {        IntegerToBytes(intbuff,iValue);        nbyteswritten = socket_send(socket->sd, intbuff, N_BYTES_PER_INTEGER);    }        if(N_BYTES_PER_INTEGER == nbyteswritten){        socket->error = LINK_FALSE;        return OK;    }    socket->error = LINK_TRUE;    return NOK;}int BufferedWriteInteger(char *toBuffer, int iValue){    IntegerToBytes(toBuffer, iValue);    return N_BYTES_PER_INTEGER;}int ReadFloat(LinkSocket *socket, double *ptrDoubleBuffer){        char buff[N_BYTES_PER_FLOAT]; /*Read float in this buffer*/    int bytesRead;    /*socket_receive takes void* as 2nd paramater and its ok to pass char*  */    bytesRead = socket_receive(socket->sd, buff, N_BYTES_PER_FLOAT);        if(N_BYTES_PER_FLOAT == bytesRead)    {           /* All right, we got it*/                float *f;        int i = 0;        /*do it on integer first. */        BytesToInteger(&i,buff);                /*type cast just the pointer*/        f = (float *)&i;                /*type cast the float value to double*/        *ptrDoubleBuffer = (double)*f;                socket->error = LINK_FALSE;        return OK;    }    /* Oops..some error while receiving*/    socket->error = LINK_TRUE;    return NOK;    }int WriteFloat(LinkSocket *socket, float fvalue){        char buff[N_BYTES_PER_FLOAT];    int nbyteswritten;    int *iptr;    iptr = (int *)&fvalue;        if(socket->isBuffered == LINK_TRUE && OK == WriteToBuffer(socket,N_BYTES_PER_FLOAT)){        nbyteswritten = BufferedWriteFloat(&(socket->writebuffer[socket->currentbuffer]), fvalue);        socket->currentbuffer += nbyteswritten;    } else {        IntegerToBytes(buff, *iptr);        nbyteswritten = socket_send(socket->sd, buff, N_BYTES_PER_FLOAT);    }        if(N_BYTES_PER_FLOAT == nbyteswritten){        socket->error = LINK_FALSE;        return OK;    }        socket->error = LINK_TRUE;    return NOK;}int BufferedWriteFloat(char *toBuffer, float fvalue){    int *iptr;    iptr = (int *)&fvalue;    IntegerToBytes(toBuffer, *iptr);    return N_BYTES_PER_FLOAT;}int ReadChars(LinkSocket *socket, char buffer[], int nchartoread){        int nbytesread;        nbytesread = socket_receive(socket->sd, buffer, nchartoread);         if(nchartoread == nbytesread)      {        buffer[nchartoread] = '\0';        socket->error = LINK_FALSE;        return OK;      }    /*system error. */    socket->error = LINK_TRUE;    return NOK;}

⌨️ 快捷键说明

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