示例#1
0
void MySocketConnection::write(const std::vector<unsigned char>& bytes, int length) {
	int result = SDLNet_TCP_Send(this->socket, &bytes[0], length);
	// _LOGDATA("Socket data send");
	if (result < length) {
		throw WAException(std::string(SDLNet_GetError()), WAException::SOCKET_EX, WAException::SOCKET_EX_SEND);
	}
}
示例#2
0
unsigned char MySocketConnection::read() {
	unsigned char c;
	int result = SDLNet_TCP_Recv(this->socket, &c, 1);
	if (result <= 0) {
		throw WAException(std::string(SDLNet_GetError()), WAException::SOCKET_EX, WAException::SOCKET_EX_RECV);
	}
	return c;
}
示例#3
0
int WASocketConnection::read(unsigned char *buf, int length)
{
    int result = Netlib_Recv(this->hConn, (char*)buf, length, MSG_NODUMP);
    if (result <= 0)
        throw WAException(getLastErrorMsg(), WAException::SOCKET_EX, WAException::SOCKET_EX_RECV);

    return result;
}
示例#4
0
void MySocketConnection::write(int i) {
	unsigned char buffer[1];
	buffer[0] = (unsigned char) i;
	int result = SDLNet_TCP_Send(this->socket, buffer, 1);
	if (result < 1) {
		std::cout << "joder, un error" << SDLNet_GetError() << " " <<  std::endl;
		std::cout.flush();
		throw WAException(std::string(SDLNet_GetError()), WAException::SOCKET_EX, WAException::SOCKET_EX_SEND);
	}
}
示例#5
0
unsigned char WASocketConnection::read()
{
    SetLastError(0);

    char c;
    int result = Netlib_Recv(this->hConn, &c, 1, 0);
    if (result <= 0)
        throw WAException(getLastErrorMsg(), WAException::SOCKET_EX, WAException::SOCKET_EX_RECV);

    return c;
}
示例#6
0
MySocketConnection::MySocketConnection(const std::string& dir, int port) throw (WAException){
	IPaddress ip;

	if (SDLNet_ResolveHost(&ip, dir.c_str(), port) == -1) {
		throw WAException(SDLNet_GetError(), WAException::SOCKET_EX, WAException::SOCKET_EX_RESOLVE_HOST);
	}

	this->socket = SDLNet_TCP_Open(&ip);
	if (!this->socket) {
		throw WAException(SDLNet_GetError(), WAException::SOCKET_EX, WAException::SOCKET_EX_OPEN);
	}

	// int no = 0;
	// setsockopt(this->socket->channel, SOL_SOCKET, SO_REUSEADDR, (char*)&no, sizeof(no));

	//	long b = 1;
	//	if (setsockopt(this->socket->channel, SOL_SOCKET, SO_KEEPALIVE, &b, sizeof(b)) == -1) {
	//		throw WAException("Error set socket option keepalive", WAException::SOCKET_EX, WAException::SOCKET_EX_OPEN);
	//	}

	this->connected = true;
}
unsigned char WASocketConnection::read() {
	char c;

	SetLastError(0);
	int result;
	//do {
		result = Netlib_Recv(this->hConn, &c, 1, 0 /*MSG_NOHTTPGATEWAYWRAP | MSG_NODUMP*/);
	//} while  (WSAGetLastError() == EINTR);
	if (result <= 0) {
		throw WAException(getLastErrorMsg(), WAException::SOCKET_EX, WAException::SOCKET_EX_RECV);
	}
	return c;
}
示例#8
0
void WASocketConnection::write(const std::vector<unsigned char> &bytes, int length)
{
    NETLIBBUFFER nlb;
    std::string tmpBuf = std::string(bytes.begin(), bytes.end());
    nlb.buf = (char*)&(tmpBuf.c_str()[0]);
    nlb.len = length;
    nlb.flags = MSG_NODUMP;

    int result = CallService(MS_NETLIB_SEND, WPARAM(hConn), LPARAM(&nlb));
    if (result < length) {
        throw WAException(getLastErrorMsg(), WAException::SOCKET_EX, WAException::SOCKET_EX_SEND);
    }
}
示例#9
0
WASocketConnection::WASocketConnection(const std::string &dir, int port) throw (WAException)
{
    NETLIBOPENCONNECTION	noc = { sizeof(noc) };
    noc.szHost = dir.c_str();
    noc.wPort = port;
    noc.flags = NLOCF_V2; // | NLOCF_SSL;
    this->hConn = (HANDLE)CallService(MS_NETLIB_OPENCONNECTION, reinterpret_cast<WPARAM>(this->hNetlibUser),
                                      reinterpret_cast<LPARAM>(&noc));
    if (this->hConn == NULL)
        throw WAException(getLastErrorMsg(), WAException::SOCKET_EX, WAException::SOCKET_EX_OPEN);

    this->connected = true;
}
示例#10
0
void BinTreeNodeReader::streamStart()
{
	this->getTopLevelStream();

	int tag = this->in->read();
	int size = readListSize(tag);
	tag = this->in->read();
	if (tag != 1)
		throw WAException("expecting STREAM_START in streamStart", WAException::CORRUPT_STREAM_EX, 0);

	int attribCount = (size - 2 + size % 2) / 2;
	std::map<string, string>* attributes = readAttributes(attribCount);
	delete attributes;
}
示例#11
0
void WASocketConnection::write(const std::vector<unsigned char>& bytes, int offset, int length)
{
	NETLIBBUFFER nlb;
	std::string tmpBuf = std::string(bytes.begin(), bytes.end()); 
	nlb.buf = (char*) &(tmpBuf.c_str()[offset]);
	nlb.len = length;
	nlb.flags = 0; //MSG_NOHTTPGATEWAYWRAP | MSG_NODUMP;

	int result = CallService(MS_NETLIB_SEND, reinterpret_cast<WPARAM>(this->hConn),
		reinterpret_cast<LPARAM>(&nlb));
	if (result < length) {
		throw WAException(getLastErrorMsg(), WAException::SOCKET_EX, WAException::SOCKET_EX_SEND);
	}
}
示例#12
0
void WASocketConnection::write(int i) {
	char buffer;
	buffer = (char) i;

	NETLIBBUFFER nlb;
	nlb.buf = &buffer;
	nlb.len = 1;
	nlb.flags = MSG_NOHTTPGATEWAYWRAP | MSG_NODUMP;

	int result = CallService(MS_NETLIB_SEND, reinterpret_cast<WPARAM>(this->hConn), reinterpret_cast<LPARAM>(&nlb));
	if (result < 1) {
		throw WAException(getLastErrorMsg(), WAException::SOCKET_EX, WAException::SOCKET_EX_SEND);
	}
}
示例#13
0
int WASocketConnection::read(std::vector<unsigned char>& b, int off, int length)
{
    if (off < 0 || length < 0)
        throw new WAException("Out of bounds", WAException::SOCKET_EX, WAException::SOCKET_EX_RECV);

    char* buffer = (char*)_alloca(length);
    int result = Netlib_Recv(this->hConn, buffer, length, MSG_NOHTTPGATEWAYWRAP | MSG_NODUMP);
    if (result <= 0)
        throw WAException(getLastErrorMsg(), WAException::SOCKET_EX, WAException::SOCKET_EX_RECV);

    for (int i = 0; i < result; i++)
        b[off + i] = buffer[i];

    return result;
}
示例#14
0
int MySocketConnection::read(std::vector<unsigned char>& b, int off, int length) {
	unsigned char buffer[length];
	if (off < 0 || length < 0) {
		throw new WAException("Out of bounds", WAException::SOCKET_EX, WAException::SOCKET_EX_RECV);
	}

	int result = SDLNet_TCP_Recv(this->socket, &buffer, length);

	if (result <= 0) {
		throw WAException(std::string(SDLNet_GetError()), WAException::SOCKET_EX, WAException::SOCKET_EX_RECV);
	}

	for (int i = 0; i < result; i++)
		b[off + i] = buffer[i];

	return result;
}
示例#15
0
void BinTreeNodeReader::decodeStream(int flags, int offset, int length)
{
	unsigned char *pData = (unsigned char*)&buf[0];

	if ((flags & 8) != 0) {
		if (length < 4)
			throw WAException("invalid length" + length, WAException::CORRUPT_STREAM_EX, 0);

		length -= 4;

		this->conn->inputKey.decodeMessage(pData, offset + length, 0, length);
	}

	if (this->in != NULL)
		delete this->in;
	this->in = new ByteArrayInputStream(&this->buf, offset, length);
}
示例#16
0
ProtocolTreeNode* BinTreeNodeReader::nextTreeInternal()
{
	int b = this->in->read();
	int size = readListSize(b);
	b = this->in->read();
	if (b == 2)
		return NULL;

	std::string* tag = this->readStringAsString(b);

	if ((size == 0) || (tag == NULL))
		throw WAException("nextTree sees 0 list or null tag", WAException::CORRUPT_STREAM_EX, -1);
	int attribCount = (size - 2 + size % 2) / 2;
	std::map<string, string>* attribs = readAttributes(attribCount);
	if (size % 2 == 1) {
		ProtocolTreeNode* ret = new ProtocolTreeNode(*tag); ret->attributes = attribs;
		delete tag;
		return ret;
	}
	b = this->in->read();
	if (isListTag(b)) {
		ProtocolTreeNode* ret = new ProtocolTreeNode(*tag, NULL, readList(b)); ret->attributes = attribs;
		delete tag;
		return ret;
	}

	ReadData* obj = this->readString(b);
	std::vector<unsigned char>* data;
	if (obj->type == STRING) {
		std::string* s = (std::string*) obj->data;
		data = new std::vector<unsigned char>(s->begin(), s->end());
		delete s;
	}
	else data = (std::vector<unsigned char>*) obj->data;

	ProtocolTreeNode* ret = new ProtocolTreeNode(*tag, data); ret->attributes = attribs;
	delete obj;
	delete tag;
	return ret;
}
示例#17
0
void MySocketConnection::initNetwork() throw (WAException) {
	if(SDLNet_Init() == -1) {
		throw WAException(SDLNet_GetError(), WAException::SOCKET_EX, WAException::SOCKET_EX_SDL_INIT);
	}
}
示例#18
0
void MySocketConnection::makeNonBlock() {
	if (fcntl(socket->channel, F_SETFL, O_NONBLOCK) == -1)
		throw WAException("Error setting socket nonblocking!",  WAException::SOCKET_EX, WAException::SOCKET_EX_OPEN);
}
示例#19
0
void WASocketConnection::makeNonBlock()
{
    throw WAException("Error setting socket nonblocking!", WAException::SOCKET_EX, WAException::SOCKET_EX_OPEN);
}
示例#20
0
void ProtocolTreeNode::require(ProtocolTreeNode *node, const string& tag)
{
    if (!tagEquals(node, tag))
        throw WAException("failed require. node:" + node->toString() + "tag: " + tag, WAException::CORRUPT_STREAM_EX, 0);
}
示例#21
0
ReadData* BinTreeNodeReader::readString(int token)
{
	if (token == -1)
		throw WAException("-1 token in readString", WAException::CORRUPT_STREAM_EX, -1);

	int bSize;
	ReadData *ret = new ReadData();

	if (token > 2 && token <= 236) {
		if (token != 236)
			ret->data = new std::string(dictionary[token]);
		else {
			token = readInt8(this->in);
			ret->data = new std::string(extended_dict[token]);
		}

		ret->type = STRING;
		return ret;
	}

	switch (token) {
	case 0:
		delete ret;
		return NULL;

	case 252:
		bSize = readInt8(this->in);
		{
			std::vector<unsigned char>* buf8 = new std::vector<unsigned char>(bSize);
			fillArray(*buf8, bSize, this->in);
			ret->type = ARRAY;
			ret->data = buf8;
		}
		return ret;

	case 253:
		bSize = readInt24(this->in);
		{
			std::vector<unsigned char>* buf24 = new std::vector<unsigned char>(bSize);
			fillArray(*buf24, bSize, this->in);
			ret->type = ARRAY;
			ret->data = buf24;
		}
		return ret;

	case 255:
		bSize = readInt8(this->in);
		{
			int size = bSize & 0x7f;
			int numnibbles = size * 2 - ((bSize & 0x80) ? 1 : 0);

			std::vector<unsigned char> tmp(size);
			fillArray(tmp, size, this->in);
			std::string s;
			for (int i = 0; i < numnibbles; i++) {
				char c = (tmp[i / 2] >> (4 - ((i & 1) << 2))) & 0xF;
				if (c < 10) s += (c + '0');
				else s += (c - 10 + '-');
			}

			ret->type = STRING;
			ret->data = new std::string(s);
		}
		return ret;

	case 250:
		std::string* user = readStringAsString();
		std::string* server = readStringAsString();
		if ((user != NULL) && (server != NULL)) {
			std::string* result = new std::string(*user + "@" + *server);
			delete user;
			delete server;
			ret->type = STRING;
			ret->data = result;
			return ret;
		}
		if (server != NULL) {
			ret->type = STRING;
			ret->data = server;
			return ret;
		}
		throw WAException("readString couldn't reconstruct jid", WAException::CORRUPT_STREAM_EX, -1);
	}
	throw WAException("readString couldn't match token" + (int)token, WAException::CORRUPT_STREAM_EX, -1);
}