void Socket::OnString(string s) { cout << "Length:" << s.length() << '\n'; BeginWrite(s); cout << "I got the message" << '\n'; }
void ConnectSocketChannel::Connect() { RETURN_IF_EQUAL(mState, ConnectionState::Connected); mState = ConnectionState::Connecting; mLoop->AssertInLoopThread(); mSocket.CreateSocket(); mSocket.EnableAsync(true); mSocket.EnableTcpNoDelay(true); ChannelEventResult connectResult = mSocket.Connect(); //connect timeout switch (connectResult) { case ChannelEventResult::Success: mState = ConnectionState::Connected; return; case ChannelEventResult::InProgress: mState = ConnectionState::Connecting; BeginWrite(); SubmitToLoop(); break; case ChannelEventResult::Retry: Retry(); break; case ChannelEventResult::Fault: default: OnError(); break; } }
CMultiReadExclusiveWriteSynchronizer::~CMultiReadExclusiveWriteSynchronizer(void) { BeginWrite(); CloseHandle(m_WriteSignal); CloseHandle(m_ReadSignal[0]); CloseHandle(m_ReadSignal[1]); }
NearestFunc SamplerJitCache::Compile(const SamplerID &id) { BeginWrite(); const u8 *start = AlignCode16(); // Early exit on !srcPtr. FixupBranch zeroSrc; if (id.hasInvalidPtr) { CMP(PTRBITS, R(srcReg), Imm8(0)); FixupBranch nonZeroSrc = J_CC(CC_NZ); XOR(32, R(RAX), R(RAX)); zeroSrc = J(true); SetJumpTarget(nonZeroSrc); } if (!Jit_ReadTextureFormat(id)) { EndWrite(); SetCodePtr(const_cast<u8 *>(start)); return nullptr; } if (id.hasInvalidPtr) { SetJumpTarget(zeroSrc); } RET(); EndWrite(); return (NearestFunc)start; }
bool Pickle::WriteBytes(const void* data, int data_len) { DCHECK(capacity_ != kCapacityReadOnly) << "oops: pickle is readonly"; char* dest = BeginWrite(data_len); if(!dest) { return false; } memcpy(dest, data, data_len); EndWrite(dest, data_len); return true; }
void MP4FreeAtom::Write() { ASSERT(m_pFile); bool use64 = (GetSize() > (0xFFFFFFFF - 8)); BeginWrite(use64); #if 1 for (uint64_t ix = 0; ix < GetSize(); ix++) { m_pFile->WriteUInt8(0); } #else m_pFile->SetPosition(m_pFile->GetPosition() + GetSize()); #endif FinishWrite(use64); }
BOOL CPackFiles::Init(CAbstractFile* pcFile, EPackFileMode eMode) { mcFile.Init(pcFile); meMode = eMode; mpsLastAccessed = NULL; miPosition = 0; miNodes = 0; miNextNodesPtr = 0; mcNames.Init(); if (eMode == PFM_Write) { return BeginWrite(); } else if (eMode == PFM_Read) { return BeginRead(); } else { return FALSE; } }
char* Pickle::BeginWriteData(int length) { DCHECK_EQ(variable_buffer_offset_, 0U) << "There can only be one variable buffer in a Pickle"; if(length<0 || !WriteInt(length)) { return NULL; } char* data_ptr = BeginWrite(length); if(!data_ptr) { return NULL; } variable_buffer_offset_ = data_ptr - reinterpret_cast<char*>(header_) - sizeof(int); // 数据写入后不必要再调用EndWrite, 所以在这里调用进行数据对齐. EndWrite(data_ptr, length); return data_ptr; }
static GLFallbackArrayBuffer * MakeCircleBuffer(unsigned n) { assert(INT_ANGLE_RANGE % n == 0); auto buffer = new GLFallbackArrayBuffer(); FloatPoint2D *const p0 = (FloatPoint2D *)buffer->BeginWrite(sizeof(*p0) * n); auto *p = p0, *p2 = p + n / 2; for (unsigned i = 0; i < n / 2; ++i, ++p, ++p2) { float x = ISINETABLE[(i * (INT_ANGLE_RANGE / n) + 1024) & INT_ANGLE_MASK] / 1024.; float y = ISINETABLE[i * (INT_ANGLE_RANGE / n)] / 1024.; p->x = x; p->y = y; p2->x = -x; p2->y = -y; } buffer->CommitWrite(sizeof(*p0) * n, p0); return buffer; }
void MP4FreeAtom::Write() { if(m_File.GetRealTimeMode() > MP4_NORMAL) { bool use64 = (GetSize() > (0xFFFFFFFF - 8)); BeginWrite(use64); #if 1 bool bIsInFlag = true; bool bIsInFlag2 = true; bool bIsInFlag3 = true; bool bIsInFlag4 = true; bool bIsInFlag5 = true; if(m_File.m_ExeFreeTimes%2 == 0) { for (uint64_t ix = 0; ix < GetSize(); ix++) { if(3 == ix) {//版本 m_File.WriteUInt8(1); } else if(7 == ix) {//加密标识 m_File.WriteUInt8(m_File.m_encryptionFlag); } else if((8 <= ix) && (ix <= 15)) {//mdat大小 if(bIsInFlag) { m_File.WriteUInt64(m_File.GetMdatSize()); bIsInFlag = false; } } else { m_File.WriteUInt8(0); } } } else { for (uint64_t ix = 0; ix < GetSize(); ix++) { if(3 == ix) {//版本 m_File.WriteUInt8(1); } else if((4 <= ix) && (ix <= 11)) {//休庭信息 if(bIsInFlag) { m_File.WriteUInt64(m_File.m_AdjournPos); bIsInFlag = false; } } else if((12 <= ix) && (ix <= 15)) {//加密标识 if(bIsInFlag3) { m_File.WriteBytes((uint8_t*)(&m_File.m_Encryption), 4); bIsInFlag3 = false; } } else if((16 <= ix) && (ix <= 19)) {//音频类型 if(bIsInFlag4) { m_File.WriteBytes((uint8_t*)(&m_File.m_AudioEncode), 4); bIsInFlag4 = false; } } else if((20 <= ix) && (ix <= 27)) {//优化使用 if(bIsInFlag5) { m_File.WriteUInt64(m_File.m_NormalVirtualFramePos); bIsInFlag5 = false; } } else if((120 <= ix) && (ix <= 127)) {//优化使用 if(bIsInFlag2) { m_File.WriteUInt64(m_File.m_MoovPos); bIsInFlag2 = false; } } else{ m_File.WriteUInt8(0); } } } m_File.m_ExeFreeTimes++; if(10 == m_File.m_ExeFreeTimes) { m_File.m_ExeFreeTimes = 0; } #else m_File.SetPosition(m_File.GetPosition() + GetSize()); #endif FinishWrite(use64); } else { bool use64 = (GetSize() > (0xFFFFFFFF - 8)); BeginWrite(use64); #if 1 for (uint64_t ix = 0; ix < GetSize(); ix++) { m_File.WriteUInt8(0); } #else m_File.SetPosition(m_File.GetPosition() + GetSize()); #endif FinishWrite(use64); } }
void Socket::BeginWrite(const string& s) { uv_buf_t buf = uv_buf_init((char*) malloc(s.length()), s.length()); s.copy(buf.base,s.length()); BeginWrite(buf); }
HRESULT CGraphics::Render(void) { if(!m_Device) return E_FAIL; if(m_NeedReset) return S_FALSE; HRESULT hr; if(FAILED(hr = m_Device->TestCooperativeLevel())) { TRACE(TEXT("Warning: Failed on test cooperative level.\n")); // The device has been lost but cannot be reset at this time. // Therefore, rendering is not possible and we'll have to return // and try again at a later time. if(hr == D3DERR_DEVICELOST) { TRACE(TEXT("Warning: Display device lost.\n")); return S_FALSE; } // The device has been lost but it can be reset at this time. if(hr == D3DERR_DEVICENOTRESET) { // Try to reset the device m_NeedReset = TRUE; return S_FALSE; } } if(SUCCEEDED(m_Device->BeginScene())) { if(SUCCEEDED(BeginWrite())) { //m_Direct3DBeginEvent(NULL,TEXT("Rendering")); if(!m_Blur) { m_Device->Clear(NULL,NULL,D3DCLEAR_TARGET,m_Background,1.0f,NULL); if(GetVisualizationCount() && GetVisualization(m_Visualization)) GetVisualization(m_Visualization)->Draw(*this); } else { LPDIRECT3DSURFACE9 backbufferTarget = NULL; m_Device->GetRenderTarget(0,&backbufferTarget); LPDIRECT3DSURFACE9 blurTarget = NULL; m_BlurTexture->GetSurfaceLevel(0,&blurTarget); m_Device->SetRenderTarget(0,blurTarget); m_Device->Clear(NULL,NULL,D3DCLEAR_TARGET,Color(0.0f,0.0f,0.0f,0.0f),1.0f,NULL); // Maybe this is faster //SpriteWrite(Rect(0.0f,0.0f,GetWidth()/2,GetHeight()/2),Color(1.0f,0.0f,0.0f,0.0f),NULL); SpriteWrite(Rect(0.0f,0.0f,GetWidth()/2,GetHeight()/2),Color(1.0f,1.0f,1.0f,1.0f),m_VisualizationTexture); SpriteFlush(); LineFlush(); LPDIRECT3DSURFACE9 visualizationTarget = NULL; m_VisualizationTexture->GetSurfaceLevel(0,&visualizationTarget); m_Device->SetRenderTarget(0,visualizationTarget); SpriteWrite(Rect(0.0f,0.0f,GetWidth(),GetHeight()),Color(1.0f,1.0f,1.0f,1.0f),m_BlurTexture); SpriteFlush(); m_Device->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA); m_Device->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE); m_Device->SetRenderState(D3DRS_BLENDOP,D3DBLENDOP_REVSUBTRACT); SpriteWrite(Rect(0.0f,0.0f,GetWidth(),GetHeight()),Color(max(0.0f,min(1.0f,GetElapsedTime()/50.0f)),1.0f,1.0f,1.0f),NULL); SpriteFlush(); m_Device->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA); m_Device->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA); m_Device->SetRenderState(D3DRS_BLENDOP,D3DBLENDOP_ADD); if(GetVisualizationCount() && GetVisualization(m_Visualization)) GetVisualization(m_Visualization)->Draw(*this); SpriteFlush(); LineFlush(); m_Device->SetRenderTarget(0,backbufferTarget); visualizationTarget->Release(); blurTarget->Release(); backbufferTarget->Release(); SpriteWrite(Rect(0.0f,0.0f,GetWidth(),GetHeight()),Color(1.0f,1.0f,1.0f,1.0f),m_VisualizationTexture); } EndWrite(); //m_Direct3DEndEvent(); } else { TRACE(TEXT("Error: Failed to begin sprite write.\n")); } m_Device->EndScene(); } else { TRACE(TEXT("Error: Failed to begin scene.\n")); } if(FAILED(m_Device->Present(NULL,NULL,NULL,NULL))) { TRACE(TEXT("Error: Failed to present scene.\n")); } return S_OK; }
LinearFunc SamplerJitCache::CompileLinear(const SamplerID &id) { _assert_msg_(G3D, id.linear, "Linear should be set on sampler id"); BeginWrite(); // We'll first write the nearest sampler, which we will CALL. // This may differ slightly based on the "linear" flag. const u8 *nearest = AlignCode16(); if (!Jit_ReadTextureFormat(id)) { EndWrite(); SetCodePtr(const_cast<u8 *>(nearest)); return nullptr; } RET(); // Now the actual linear func, which is exposed externally. const u8 *start = AlignCode16(); // NOTE: This doesn't use the general register mapping. // POSIX: arg1=uptr, arg2=vptr, arg3=frac_u, arg4=frac_v, arg5=src, arg6=bufw, stack+8=level // Win64: arg1=uptr, arg2=vptr, arg3=frac_u, arg4=frac_v, stack+40=src, stack+48=bufw, stack+56=level // // We map these to nearest CALLs, with order: u, v, src, bufw, level // Let's start by saving a bunch of registers. PUSH(R15); PUSH(R14); PUSH(R13); PUSH(R12); // Won't need frac_u/frac_v for a while. PUSH(arg4Reg); PUSH(arg3Reg); // Extra space to restore alignment and save resultReg for lerp. // TODO: Maybe use XMMs instead? SUB(64, R(RSP), Imm8(24)); MOV(64, R(R12), R(arg1Reg)); MOV(64, R(R13), R(arg2Reg)); #ifdef _WIN32 // First arg now starts at 24 (extra space) + 48 (pushed stack) + 8 (ret address) + 32 (shadow space) const int argOffset = 24 + 48 + 8 + 32; MOV(64, R(R14), MDisp(RSP, argOffset)); MOV(32, R(R15), MDisp(RSP, argOffset + 8)); // level is at argOffset + 16. #else MOV(64, R(R14), R(arg5Reg)); MOV(32, R(R15), R(arg6Reg)); // level is at 24 + 48 + 8. #endif // Early exit on !srcPtr. FixupBranch zeroSrc; if (id.hasInvalidPtr) { CMP(PTRBITS, R(R14), Imm8(0)); FixupBranch nonZeroSrc = J_CC(CC_NZ); XOR(32, R(RAX), R(RAX)); zeroSrc = J(true); SetJumpTarget(nonZeroSrc); } // At this point: // R12=uptr, R13=vptr, stack+24=frac_u, stack+32=frac_v, R14=src, R15=bufw, stack+X=level auto doNearestCall = [&](int off) { MOV(32, R(uReg), MDisp(R12, off)); MOV(32, R(vReg), MDisp(R13, off)); MOV(64, R(srcReg), R(R14)); MOV(32, R(bufwReg), R(R15)); // Leave level, we just always load from RAM. Separate CLUTs is uncommon. CALL(nearest); MOV(32, MDisp(RSP, off), R(resultReg)); }; doNearestCall(0); doNearestCall(4); doNearestCall(8); doNearestCall(12); // Convert TL, TR, BL, BR to floats for easier blending. if (!cpu_info.bSSE4_1) { PXOR(XMM0, R(XMM0)); } MOVD_xmm(fpScratchReg1, MDisp(RSP, 0)); MOVD_xmm(fpScratchReg2, MDisp(RSP, 4)); MOVD_xmm(fpScratchReg3, MDisp(RSP, 8)); MOVD_xmm(fpScratchReg4, MDisp(RSP, 12)); if (cpu_info.bSSE4_1) { PMOVZXBD(fpScratchReg1, R(fpScratchReg1)); PMOVZXBD(fpScratchReg2, R(fpScratchReg2)); PMOVZXBD(fpScratchReg3, R(fpScratchReg3)); PMOVZXBD(fpScratchReg4, R(fpScratchReg4)); } else { PUNPCKLBW(fpScratchReg1, R(XMM0)); PUNPCKLBW(fpScratchReg2, R(XMM0)); PUNPCKLBW(fpScratchReg3, R(XMM0)); PUNPCKLBW(fpScratchReg4, R(XMM0)); PUNPCKLWD(fpScratchReg1, R(XMM0)); PUNPCKLWD(fpScratchReg2, R(XMM0)); PUNPCKLWD(fpScratchReg3, R(XMM0)); PUNPCKLWD(fpScratchReg4, R(XMM0)); } CVTDQ2PS(fpScratchReg1, R(fpScratchReg1)); CVTDQ2PS(fpScratchReg2, R(fpScratchReg2)); CVTDQ2PS(fpScratchReg3, R(fpScratchReg3)); CVTDQ2PS(fpScratchReg4, R(fpScratchReg4)); // Okay, now multiply the R sides by frac_u, and L by (256 - frac_u)... MOVD_xmm(fpScratchReg5, MDisp(RSP, 24)); CVTDQ2PS(fpScratchReg5, R(fpScratchReg5)); SHUFPS(fpScratchReg5, R(fpScratchReg5), _MM_SHUFFLE(0, 0, 0, 0)); if (RipAccessible(by256)) { MULPS(fpScratchReg5, M(by256)); // rip accessible } else { Crash(); // TODO } MOVAPS(XMM0, M(ones)); SUBPS(XMM0, R(fpScratchReg5)); MULPS(fpScratchReg1, R(XMM0)); MULPS(fpScratchReg2, R(fpScratchReg5)); MULPS(fpScratchReg3, R(XMM0)); MULPS(fpScratchReg4, R(fpScratchReg5)); // Now set top=fpScratchReg1, bottom=fpScratchReg3. ADDPS(fpScratchReg1, R(fpScratchReg2)); ADDPS(fpScratchReg3, R(fpScratchReg4)); // Next, time for frac_v. MOVD_xmm(fpScratchReg5, MDisp(RSP, 32)); CVTDQ2PS(fpScratchReg5, R(fpScratchReg5)); SHUFPS(fpScratchReg5, R(fpScratchReg5), _MM_SHUFFLE(0, 0, 0, 0)); MULPS(fpScratchReg5, M(by256)); MOVAPS(XMM0, M(ones)); SUBPS(XMM0, R(fpScratchReg5)); MULPS(fpScratchReg1, R(XMM0)); MULPS(fpScratchReg3, R(fpScratchReg5)); // Still at the 255 scale, now we're interpolated. ADDPS(fpScratchReg1, R(fpScratchReg3)); // Time to convert back to a single 32 bit value. CVTPS2DQ(fpScratchReg1, R(fpScratchReg1)); PACKSSDW(fpScratchReg1, R(fpScratchReg1)); PACKUSWB(fpScratchReg1, R(fpScratchReg1)); MOVD_xmm(R(resultReg), fpScratchReg1); if (id.hasInvalidPtr) { SetJumpTarget(zeroSrc); } ADD(64, R(RSP), Imm8(24)); POP(arg3Reg); POP(arg4Reg); POP(R12); POP(R13); POP(R14); POP(R15); RET(); EndWrite(); return (LinearFunc)start; }