📄 communit.cpp
字号:
/*************************************************************************** * Copyright (C) 2004 - 2006 by ZJUBase *
* National Lab of Industrial Control Tech. * * Zhejiang University, China * * * * Team members: *
* Currently the team leader is, * * Hao JIANG (jianghao@iipc.zju.edu.cn; riveria@gmail.com) *
* In the next season, the leader will be * * Yifeng ZHANG (yfzhang@iipc.zju.edu.cn) *
* ZJUBase 3D agent is created by * * Dijun LUO (djluo@iipc.zju.edu.cn) *
* All the members who has ever contributed: * * Jun JIANG *
* Xinfeng DU (xfdu@iipc.zju.edu.cn) *
* Yang ZHOU (yzhou@iipc.zju.edu.cn) *
* Zhipeng YANG *
* Xiang FAN *
* *
* Team Manager: *
* Ms. Rong XIONG (rxiong@iipc.zju.edu.cn) *
* *
* If you met any problems or you have something to discuss about * * ZJUBase. Please feel free to contact us through EMails given below. * * * * 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 "communit.h"
#include "types.h"
//#include <netinet/in.h>
#include <iostream>
#include <sstream>
//#include <rcssnet/exception.hpp>
#include <cerrno>
using namespace std;
//CommUnit::CommUnit() :
// mStreamBuf(mSocket),mInStream(&mStreamBuf)
CommUnit::CommUnit()
{
mSockfd = 0;
}
CommUnit::~CommUnit()
{
CloseConnection();
}
void
CommUnit::CloseConnection()
{
// mSocket.close();
closesocket(mSockfd);
mSockfd = 0;
}
bool
CommUnit::OpenConnection(const std::string& server, int port)
{
struct sockaddr_in cli_addr;
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
// init winsock
if (WSAStartup(wVersionRequested, &wsaData) != 0 ) {
cerr << "Winsock error" << endl;
return false;
}
if (LOBYTE(wsaData.wVersion) != 2 ||
HIBYTE(wsaData.wVersion) != 2 ) {
WSACleanup();
cerr << "Winsock error" << endl;
return false;
}
// init socket
int sockfd;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
cerr << "Socket creation error" << endl;
return false;
}
memset((char *) &cli_addr,0,sizeof(cli_addr));
cli_addr.sin_family = AF_INET;
cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
cli_addr.sin_port = htons(0);
if(bind(sockfd, (struct sockaddr *)&cli_addr,
sizeof(cli_addr)) < 0){
cerr << "Socket binding error" << endl;
return false; // Can't bind local address
}
// connect
struct sockaddr_in addr;
struct hostent *host = gethostbyname(server.data());
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr = *((struct in_addr*)host->h_addr);
memset(&(addr.sin_zero), 0, 8);
if(connect(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr)) < 0) {
cerr << "Connection failed" << endl;
closesocket(sockfd);
return false;
}
mSockfd = sockfd;
return true;
/*
mSocket.open();
Addr local(INADDR_ANY,INADDR_ANY);
mSocket.bind(local);
try
{
Addr server(port,string(host));
mSocket.connect(server);
}
catch (ConnectErr error)
{
cerr << "(CommUnit) connection failed with: \n\t"
<< strerror(errno) << endl;
return false;
}
if (mSocket.isConnected())
{
return true;
}
return false;
*/
}
bool
CommUnit::SelectInput(bool wait)
{
int fd = mSockfd;
if (fd <= 0) {
return false;
}
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(fd,&readfds);
timeval time;
time.tv_sec = 0;
time.tv_usec = 0;
return select
(
fd+1, &readfds, 0, 0,
wait ? 0:&time
) > 0;
}
string
CommUnit::GetMessage()
{
if (!SelectInput(false)) {
return "";
}
static char line[MAX_MSG_LEN];
static int size;
// mInStream.getline(line,MAX_MSG_LEN);
do {
if ((size = recv(mSockfd, line, MAX_MSG_LEN - 1, 0)) > 0) {
line[size] = 0;
mBuffer << line;
}
} while(SelectInput(false));
if (mBuffer.getline(line, MAX_MSG_LEN - 1)) {
return line;
}
mBuffer << line;
return "";
}
void
CommUnit::PutMessage(const string& message)
{
// mSocket.send(message.c_str(),message.size());
const char *ptr = message.data();
int left, n, len = message.size();
for (left = len; left > 0; left -= n, ptr += n) {
if ((n = send(mSockfd, ptr, left, 0)) <= 0) {
return;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -