Пример #1
0
void TSockSys::OnRead(uv_stream_t* SockHnd, ssize_t BufferLen, uv_buf_t Buffer) {
	//TODO: check if we need to close _SockHnd
	//uv_tcp_t* _SockHnd = (uv_tcp_t*)SockHnd;
	// get socket handle
	IAssert(SockSys.IsSockHnd((uint64)SockHnd));
	// get socket id
	const uint64 SockId = SockSys.SockHndToIdH.GetDat((uint64)SockHnd);
	IAssert(SockSys.IsSock(SockId));
	// get socket event
	const uint64 SockEventId = SockSys.SockHndToEventIdH.GetDat((uint64)SockHnd);
	PSockEvent SockEvent;
	if (SockSys.IsSockEvent(SockEventId)) {
		SockEvent = SockSys.GetSockEvent(SockEventId);
	} else {
		// cleanup (using delete as it was created in OnAlloc with new)
		delete[] Buffer.base;
		SaveToErrLog("SockSys.OnRead: Socket without SockEvent");
		return;
	}
	// execute callback
	if (BufferLen > 0) {
		// we got data, move the ownership of buffer to TMIn
		PSIn SIn = TMIn::New(Buffer.base, (int)BufferLen, true);
		// send
		SockEvent->OnRead(SockId, SIn);		
	} else {
		uv_err_code Status = uv_last_error(SockSys.Loop).code;
		// no data, might be error or end of stream
		if (Status == UV_EOF) {
			// no more data, close the socket handle
			SockEvent->OnReadEof(SockId);
			SockSys.DelIfSockTimer(SockId);
		} else if (Status == UV_EAGAIN) {
     		// we'll wait
        } else {
			// error
			TStr ErrMsg = "SockSys.OnRead: " + SockSys.GetLastErr();
			SockEvent->OnError(SockId, Status, ErrMsg);
		}
		// cleanup (using delete as it was created in OnAlloc with new)
		delete[] Buffer.base;
	}
}
Пример #2
0
void TSockSys::OnClose(uv_handle_t* SockHnd) {
	// check if we know about the socket
	if (SockSys.IsSockHnd((uint64)SockHnd)) {
		// get socket id
		const uint64 SockId = SockSys.SockHndToIdH.GetDat((uint64)SockHnd);
		// execute callback
		const uint64 SockEventId = SockSys.SockHndToEventIdH.GetDat((uint64)SockHnd);
		if (SockSys.IsSockEvent(SockEventId)) {
			PSockEvent SockEvent = SockSys.GetSockEvent(SockEventId);
			SockEvent->OnClose(SockId);
		}
		// delete socket shortcuts
		SockSys.DelIfSockTimer(SockId);
		SockSys.SockIdToHndH.DelKey(SockId);
		SockSys.SockHndToIdH.DelKey((uint64)SockHnd);
		SockSys.SockHndToEventIdH.DelKey((uint64)SockHnd);
		// marke note that it's already closed
		SockSys.ClosedSockIdSet.DelIfKey(SockId);
	}
}