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

📄 socketsuite.cpp

📁 unix socket c++ 封装
💻 CPP
字号:
#include "SocketSuite.h"
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/errno.h>
#include <stdio.h>

extern int errno;

using namespace acceptor_common::acceptor;
using namespace std;

SocketSuite::SocketSuite():_localFd(-1),_rmtFd(-1)
{}


SocketSuite::~SocketSuite()
{
	destroy();
}

int SocketSuite::ready(unsigned short port)
{
	if ((_localFd = socket(AF_INET , SOCK_STREAM , 0)) == -1)
	{
		if (errno == EMFILE)
			ERROR("File descriptor has been up to the upper limit.")
		else if (errno == ENOMEM)
			ERROR("User space memory is not enough.")
		else
			DEFAULT_ERROR(errno);
		
		return -1;
	}	

	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	addr.sin_addr.s_addr = htonl(INADDR_ANY);
	
	if (bind(_localFd , (struct sockaddr *)&addr , sizeof(struct sockaddr)) == -1)
	{
		if (errno == EADDRINUSE)
			ERROR("The address is already in use.")
		else if (errno == EINVAL)
			ERROR("The socket is already bound to an address.")
		else if (errno == ENOTSOCK)
			ERROR("The descriptor for a file, not a socket.")
		else
			DEFAULT_ERROR(errno);

		destroy();
		
		return -1;
	}

	if (listen(_localFd , MAX_CONCURRENCE_REQUEST) == -1)
	{
		if (errno == EBADF)
			ERROR("Bad descriptor.")
		else
			DEFAULT_ERROR(errno)

		destroy();

		return -1;
	}	

	return 0;
}


int SocketSuite::service()
{
	struct sockaddr_in rmtAddr;	
	int size = sizeof(struct sockaddr);
	if ((_rmtFd = accept(_localFd , (struct sockaddr *)&rmtAddr , &size)) == -1)
	{
		if (errno == ECONNABORTED)
			ERROR("The remote peer has closed the socket before local accepting.")
		else if (errno == EMFILE)
			ERROR("The file descriptor has been full in the table.")
		else if (errno == EWOULDBLOCK)
			ERROR("Un-blocking way.")
		else
			DEFAULT_ERROR(errno);

		destroy();
			
		return -1;
	}
	
	return 0;
}


int SocketSuite::setup(string ipAddress , unsigned short port , bool isBlocked)
{
	if ((_rmtFd = socket(AF_INET , SOCK_STREAM , 0)) == -1)
	{
		if (errno == EMFILE)
			ERROR("File descriptor has been up to the upper limit.")
		else if (errno == ENOMEM)
			ERROR("User space memory is not enough.")
		else
			DEFAULT_ERROR(errno);
		
		return -1;
	}	
	
	if (isBlocked)
		setFDFlag(_rmtFd , O_NONBLOCK , CMD_CLEAR);
	else
		setFDFlag(_rmtFd , O_NONBLOCK , CMD_SET);	

	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	addr.sin_addr.s_addr = inet_addr(ipAddress.c_str());
	
	if (connect(_rmtFd , (struct sockaddr *)&addr , sizeof(struct sockaddr)) == -1)
	{
		if (errno == EINPROGRESS)
			ERROR("NON-BLOCKING")
		else if (errno == EISCONN)
			ERROR("SOCKET has been connected.")
		else
			DEFAULT_ERROR(errno);

		return -1;
	}
	
	return 0;
}


int SocketSuite::send(char * buffer , int length , bool isBlocked)
{
	if (isBlocked)
		setFDFlag(_rmtFd , O_NONBLOCK , CMD_CLEAR);
	else
		setFDFlag(_rmtFd , O_NONBLOCK , CMD_SET);

	if (write(_rmtFd , buffer , length) < 0)
	{
		if (errno == EAGAIN)
			ERROR("NON-BLOCK Set")
		else
			DEFAULT_ERROR(errno)
		
		return -1;
	}
		
	return 0;
}


int SocketSuite::receive(char * buffer , int length , bool isBlocked)
{
	if (isBlocked)
		setFDFlag(_rmtFd , O_NONBLOCK , CMD_CLEAR);
	else
		setFDFlag(_rmtFd , O_NONBLOCK , CMD_SET);

	if (read(_rmtFd , buffer , length) < 0)
	{
		if (errno == EAGAIN)
			ERROR("NON-BLOCK Set")
		else
			DEFAULT_ERROR(errno);
		
		return -1;
	}
		
	return 0;
}


int SocketSuite::destroy()
{
	if (_localFd != -1)
	{
		close(_localFd);
		_localFd = -1;
	}
	
	if (_rmtFd != -1)
	{
		close(_rmtFd);
		_rmtFd = -1;
	}

	return 0;
}


int SocketSuite::setFDFlag(int fd , int flag , int cmd)
{
	int curFlag = 0;
	if ((curFlag = fcntl(fd , F_GETFL , 0)) == -1)
	{
		DEFAULT_ERROR(errno);
		
		return -1;
	}

	if (cmd == CMD_CLEAR)
		curFlag &= ~flag;
	else
		curFlag |= flag;

	if (fcntl(fd , F_SETFL , curFlag) == -1)
	{
		DEFAULT_ERROR(errno);

		return -1;
	}
	
	return 0;
}



⌨️ 快捷键说明

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