trainer.cpp

来自「RoboCup 3D 仿真组清华大学2005的源代码」· C++ 代码 · 共 121 行

CPP
121
字号
/*************************************************************************** *   Copyright (C) 2005 by Rujia Liu                                       * *   rujialiu@hotmail.com                                                  * *                                                                         * *   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.,                                       * *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * ***************************************************************************/#include <sys/socket.h>#include <netdb.h>#include "trainer.h"Trainer::Trainer(){    mConnected = false;}Trainer::~Trainer(){}bool Trainer::IsConnected(){    return mConnected;}bool Trainer::Connect(const char* ip){    struct sockaddr_in server;    struct hostent *hp;                                                                                                     if((sock = socket(AF_INET,SOCK_STREAM,0)) < 0)    {        log.Log("Error in opening stream socket.\n");        return false;    }      if((hp = gethostbyname(ip)) == 0)    {        log.Log("Error in gethostbyname()\n");        return false;    }      server.sin_family=AF_INET;    server.sin_port=htons(12001);    memcpy((char*)&server.sin_addr, (char*)hp->h_addr, hp->h_length);      if(connect(sock,(struct sockaddr *)&server, sizeof(server)) < 0)    {        log.Log("Error in connecting to stream socket.\n");        return false;    }      mConnected = true;    return true;}// check if data is a well-formed s-expressionbool Trainer::match(const char* data){    int p = 0;    for(int i = 0; i < strlen(data); i++)        if(data[i] == '(') p++; else if(data[i] == ')') p--;    return (p == 0);}bool Trainer::send_data(const char* data){    int n, c = 0;    int len = strlen(data);    unsigned long l = htonl(strlen(data));      log.Log("Sending %s\n", data);      if((n = write(sock, &l, sizeof(unsigned long))) < 0)    {        log.Log("Error in writing data length\n");        return false;     }        while(1){  // send data. may need more than one call      if((n = write(sock, data + c, len - c)) < 0)      {          log.Log("Error in writing data\n");          return false;       }      c += n;      if(c == len) break;    }    return true;}bool Trainer::get_data(char* data){  int n, c = 0;  while(1){    if((n = read(sock, data + c, 1)) < 0)    {        log.Log("Error in reading socket\n");        return false;     }    data[c += n] = '\0';    if(c > 1 && match(data)) break;  }  return true;}void Trainer::send_command(const char* str){    char s[1024];     sprintf(s, "W%s", str);     send_data(s); }

⌨️ 快捷键说明

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