void McuMessageHandler::SendPOSTime(time_t *timep) { struct tm *t = localtime(timep); int year, month, day, hour, minute, second; vector<unsigned char> vyear, vmonth, vday, vhour, vminute, vsecond; year = 1900 + t->tm_year; month = t->tm_mon + 1; day = t->tm_mday; hour = t->tm_hour; minute = t->tm_min; second = t->tm_sec; GetCharSequenceFromInt(vyear, year, 4); GetCharSequenceFromInt(vmonth, month, 2); GetCharSequenceFromInt(vday, day, 2); GetCharSequenceFromInt(vhour, hour, 2); GetCharSequenceFromInt(vminute, minute, 2); GetCharSequenceFromInt(vsecond, second, 2); for(int i = 0; i < m_pos_num; i ++) { StruMcuPacket *thepacket = CreatePacket(); McuPacket &packet = thepacket->packet; PushByte(packet, ((m_pos_addr[i] << 4) & MCU_POS_SRC_ADDR)); PushByte(packet, 0x11); PushByte(packet, MCU_POS_FLAG); PushByte(packet, 0x30); PushByte(packet, 0x32); PushSequence(packet, vyear, 4); PushSequence(packet, vmonth, 2); PushSequence(packet, vday, 2); PushSequence(packet, m_pos_pass, 6); SendBody(thepacket, MCU_POS_DATE); } for(int i = 0; i < m_pos_num; i ++) { StruMcuPacket *thepacket = CreatePacket(); McuPacket &packet = thepacket->packet; PushByte(packet, ((m_pos_addr[i] << 4) & MCU_POS_SRC_ADDR)); PushByte(packet, 0x0F); PushByte(packet, MCU_POS_FLAG); PushByte(packet, 0x30); PushByte(packet, 0x33); PushSequence(packet, vhour, 2); PushSequence(packet, vmonth, 2); PushSequence(packet, vday, 2); PushSequence(packet, m_pos_pass, 6); SendBody(thepacket, MCU_POS_TIME); } }
void McuMessageHandler::SendPOSStation(unsigned char station, unsigned char call_station, unsigned char dir) { vector<unsigned char> vstation; vector<unsigned char> vcall_station; vector<unsigned char> vdir; GetCharSequenceFromInt(vstation, station, 2); GetCharSequenceFromInt(vcall_station, call_station, 1); GetCharSequenceFromInt(vdir, dir, 1); for(int i = 0; i < m_pos_num; i ++) { StruMcuPacket *thepacket = CreatePacket(); McuPacket &packet = thepacket->packet; PushByte(packet, ((m_pos_addr[i] << 4) & MCU_POS_SRC_ADDR)); PushByte(packet, 0x07); PushByte(packet, MCU_POS_FLAG); PushByte(packet, 0x30); PushByte(packet, 0x31); PushSequence(packet, vstation, 2); PushSequence(packet, vcall_station, 1); PushSequence(packet, vdir, 1); SendBody(thepacket, MCU_POS_STATION); } }
void McuMessageHandler::SendLEDStateByDir(int dir) { int send_dir; if(dir == 0) { send_dir = 0x01; } else { send_dir = 0x02; } for(int i = 0; i < (int)(sizeof(LEDID) / sizeof(short)); i ++) { if((LEDFLAG[i] & LED_FLAG_DIRECT) == 0) { continue; } StruMcuPacket *thepacket = CreatePacket(); thepacket->data = MCU_PACKET_LED_SUBTYPE_STATE; McuPacket &packet = thepacket->packet; PushByte(packet, MCU_PACKET_LED_SUBTYPE_STATE); PushByte(packet, send_dir); PushByte(packet, 0x03); SendBody(thepacket, LEDID[i]); } }
void McuMessageHandler::SendLEDState(int flag) { int dir = GpsLocationMessageHandler::Instance()->GetDirection(); if(dir == 0) { dir = 0x01; } else { dir = 0x02; } m_driving_state = flag; for(int i = 0; i < (int)(sizeof(LEDID) / sizeof(short)); i ++) { if((LEDFLAG[i] & LED_FLAG_STATE) == 0) { continue; } StruMcuPacket *thepacket = CreatePacket(); thepacket->data = MCU_PACKET_LED_SUBTYPE_STATE; McuPacket &packet = thepacket->packet; PushByte(packet, MCU_PACKET_LED_SUBTYPE_STATE); PushByte(packet, dir); PushByte(packet, flag); SendBody(thepacket, LEDID[i]); } }
Packet* PacketParser::ParsePacket(Buffer* raw) { if( raw->size() == 0 ) return 0L; uint8_t id = 0; // Packet ID LOG_NET("ParsePacket: %d bytes\n", raw->size()); //raw->dump(); *raw >> id; Packet* newPacket = CreatePacket(id); if(! newPacket ) { LOG_NET("Recieved invalid packet type %d\n", id); raw->clear(); return NULL; } try { LOG_NET("Parsing payload..\n"); (*newPacket) << *raw; } catch( ... ) { DeletePacket(newPacket); LOG_NET("Exception thrown!\n"); return NULL; } return newPacket; }
static std::shared_ptr<Packet> Recv(Connection *pConnection) { uint64_t length=0; uint32_t command=0, sentinelHeader=0, sentinelFooter=0; uint64_t beginOffset=pConnection->RecvOffset(); pConnection->Recv(length); pConnection->Recv(command); pConnection->Recv(sentinelHeader); //fprintf(stderr, "Recvd[length=%llu, command=%u, sentinel=%u\n", length, command, sentinelHeader); if(length<20) throw std::runtime_error("Packet::Recv - Received packet length of < 20 bytes, which is not possible."); std::shared_ptr<Packet> res=CreatePacket(command); res->RecvPayload(pConnection); pConnection->Recv(sentinelFooter); if(sentinelHeader!=sentinelFooter) throw std::runtime_error("Packet::Recv - trailing sentinel does not match leading sentinel."); uint64_t endOffset=pConnection->RecvOffset(); if(endOffset < beginOffset) throw std::runtime_error("Packet::Recv - Offset has wrapped, we don't support more than 2^64 bytes sent."); if(endOffset-beginOffset != length) throw std::runtime_error("Packet::Recv - Sent bytes does not match what we said in the header."); return res; }
int CLocalSearch::ExecuteSharedFiles(int nMaximum) { CQuickLock oLock( Library.m_pSection ); CPtrList* pFiles = Library.Search( m_pSearch, nMaximum ); if ( pFiles == NULL ) return 0; int nHits = pFiles->GetCount(); while ( pFiles->GetCount() ) { int nInThisPacket = min( pFiles->GetCount(), (int)Settings.Gnutella.HitsPerPacket ); CreatePacket( nInThisPacket ); int nHitB = 0; for ( int nHitA = 0 ; nHitA < nInThisPacket ; nHitA++ ) { CLibraryFile* pFile = (CLibraryFile*)pFiles->RemoveHead(); if ( AddHit( pFile, nHitB ) ) nHitB ++; } WriteTrailer(); if ( nHitB > 0 ) DispatchPacket(); else DestroyPacket(); } delete pFiles; return nHits; }
void McuMessageHandler::SendGetPosPassengerRecord(int i, EnumRecordFlag clear) { StruMcuPacket *thepacket = CreatePacket(); McuPacket &packet = thepacket->packet; PushByte(packet, ((m_pos_addr[i] << 4) & MCU_POS_SRC_ADDR)); if(clear == CLEAR_RECORD) { PushByte(packet, 0x03); } else { PushByte(packet, 0x05); } PushByte(packet, MCU_POS_FLAG); PushByte(packet, 0x38); PushByte(packet, 0x30); if(clear != CLEAR_RECORD) { PushByte(packet, 0x30); PushByte(packet, 0x31); } thepacket->data = (int)clear; thepacket->data2 = i; SendBody(thepacket, MCU_POS_GET_PASSENGER); }
void McuMessageHandler::SendGetCanData() { StruMcuPacket *thepacket = CreatePacket(); McuPacket &packet = thepacket->packet; PushByte(packet, 0x01); SendBody(thepacket, MCU_CAN_GET_DATA); }
void CutupProtocol::CreateEmptyPacket( ByteBuffer& data ) const { PCP_PACKET pPacket = CreatePacket(0, 0, CPCMD_NO_DATA, NULL, 0); DWORD dwPacketSize = PACKET_SIZE(pPacket); data.Alloc(dwPacketSize); memcpy(data, pPacket, dwPacketSize); FreePacket(pPacket); }
void CHSNetwork::OnReceive(const char *pcData, unsigned int uiDataLen, CHSSocket * pSocket) { // The first 16 bits of the data should be the packet type. unsigned short usType = 0; CHSDataStream stream; stream.WriteToStream((void *) pcData, uiDataLen); stream.SeekToBegin(); while (stream.GetStreamRemaining() > sizeof(unsigned short)) { stream.ReadFromStream(usType); CHSPacket *pPacket = CreatePacket((EHSPacketType) usType); if (!pPacket) { // Failure to create a packet. The data left in this stream // could contain anything. We're forced to throw it all out. return; } // We have a packet. Have the packet read from the stream. if (!pPacket->ReadFromStream(stream)) { // Bad read. return; } else { // Good packet ... onto the queue! pPacket->SetPacketAddress((unsigned int) pSocket); // Is this a packet we've been waiting for? if (m_eWaitingFor != PT_INVALID) { if (pPacket->GetPacketType() == m_eWaitingFor) { // Flip the flag, so the waiting function knows it's arrived. m_eWaitingFor = PT_INVALID; // This packet goes to the front. m_quePackets.push_front(pPacket); } else { m_quePackets.push_back(pPacket); } } else { m_quePackets.push_back(pPacket); } } } }
void CutupProtocol::GetDataFromSendMsg( SEND_MSG& sendMsg, DWORD dwMaxSize, ByteBuffer& byteData, COMM_NAME* pExpectedComm /*= NULL*/ ) { if (dwMaxSize == 0) dwMaxSize = sendMsg.dwDefaultPacketMaxSize; if (NULL != pExpectedComm) *pExpectedComm = sendMsg.expectedCommName; _time64(&sendMsg.lastGetDataTime); if (sendMsg.bData) { DWORD dwReadSize = min(dwMaxSize, sendMsg.byteData.Size() - sendMsg.params.dataParams.dwNextSendPos); DWORD dwNextSendPos = sendMsg.params.dataParams.dwNextSendPos + dwReadSize; BOOL bSentAll = (dwNextSendPos >= sendMsg.byteData.Size()); BYTE cmd = bSentAll ? CPCMD_DATA_END : CPCMD_DATA_MORE; PCP_PACKET pPacket = CreatePacket(sendMsg.serial, sendMsg.params.dataParams.dwNextIndex, cmd, (LPBYTE)sendMsg.byteData + sendMsg.params.dataParams.dwNextSendPos, dwReadSize); pPacket->ToByteBuffer(byteData); FreePacket(pPacket); if (! bSentAll) { //还有未发完的数据 sendMsg.params.dataParams.dwNextIndex++; sendMsg.params.dataParams.dwNextSendPos = dwNextSendPos; } else { //内容已经全部发出,记录时间,随后清除,保留一段时间以备重发的请求 sendMsg.bAllSent = TRUE; _time64(&sendMsg.finishTime); } } else { //如果是packet类型的数据,byteData字段不能过长,这里就不再切分了 PCP_PACKET pPacket = CreatePacket(sendMsg.serial, sendMsg.params.packetParams.index, sendMsg.params.packetParams.cmd, sendMsg.byteData, sendMsg.byteData.Size()); pPacket->ToByteBuffer(byteData); FreePacket(pPacket); //下次清理掉 sendMsg.bAllSent = TRUE; sendMsg.finishTime = 0; } }
bool NetServer::SendData(const void* data, int dataLength, ENetPeer* dest) { ENetPacket* packet = CreatePacket(data, dataLength); if(!packet) return false; if (enet_peer_send(dest, DEFAULT_CHANNEL, packet) < 0) return false; return true; }
Packet* ClonePacket(const Packet* inPacket) throw() { Packet* resultPacket = CreatePacket(inPacket->packetType, inPacket->packetLen); if (resultPacket) { // create could return NULL pdg::AutoMutex mutex(&sPacketMemMutex); std::memcpy((void*)resultPacket,(void*)inPacket, inPacket->packetLen); } return resultPacket; }
void CMLan::SendPacketExt(TPacket& pak, u_long addr) { int pakLen; u_char* buf = CreatePacket(pak, &pakLen); in_addr _addr; _addr.S_un.S_addr = addr; SendPacket(_addr, (u_char*)buf, pakLen); delete[] buf; }
void WritePacket(int Snaplen) { LPPACKET lpPacket = 0; // define a pointer to a PACKET structure if (Snaplen > sizeof(tx_buffer)) Snaplen = sizeof(tx_buffer); lpPacket = CreatePacket((char*)tx_buffer, Snaplen); PacketSetNumWrites(lpAdapter, 1); PacketSendPacket(lpAdapter, lpPacket, TRUE); ReleasePacket(lpPacket); }
void net_init( char *ip, int32_t remotePort, int32_t localPort ) { printf("Connecting to IP : %s , remote port %d , local port :%d\n", ip, remotePort,localPort); // Initialize SDL_net InitSDL_Net(); OpenPort( localPort ); SetIPAndPort( ip, remotePort ); CreatePacket( 512 ); }
U32 N64SimulationDataGenerator::GenerateSimulationData( U64 largest_sample_requested, U32 sample_rate, SimulationChannelDescriptor** simulation_channel ) { U64 adjusted_largest_sample_requested = AnalyzerHelpers::AdjustSimulationTargetSample( largest_sample_requested, sample_rate, mSimulationSampleRateHz ); while( mSimulationData.GetCurrentSampleNumber() < adjusted_largest_sample_requested ) { CreatePacket(); } *simulation_channel = &mSimulationData; return 1; }
void McuMessageHandler::SendGPSData(unsigned char *buf, int len) { StruMcuPacket *thepacket = CreatePacket(); McuPacket &packet = thepacket->packet; buf[1] = 'G'; buf[2] = 'P'; printf("GSPData: %s\n",buf); for(int i = 0; i < len; i++){ PushByte(packet, buf[i]); } SendBody(thepacket, MCU_PACKET_STATION_REPORT); }
void ConnectionSystem::Disconnect() { if(m_Connected || m_ConnectionAttempted) { sf::Packet* Quit = CreatePacket(PT_DISCONNECT); (*Quit) << (i8) DR_QUIT; m_Socket->Send(Quit, m_ServerAddress, m_ServerPort); delete Quit; m_Connected = false; m_ConnectionAttempted = false; m_Engine->Log("Disconnected from server at %s:%d.\n", m_ServerAddress.c_str(), m_ServerPort); } else { m_Engine->Log("Could not disconnect: no connection established.\n"); } }
INetPacket* INetPool::GetIncoming(void) { // Default to no packet returned (rarely should happen) INetPacket* anResult = NULL; // Is our incoming queue below our minimum threshold, then add some new packets if(mIncoming.size() < mIncomingMinimum) { // Add packets to our incoming queue (up to our minimum number of packets) for(std::size_t iloop = 0; iloop < mIncomingMinimum; iloop++) { // Attempt to create a packet with default minimum the same as capacity INetPacket* anPacket = CreatePacket(mIncomingCapacity, mIncomingCapacity); // Make sure one was returned if(anPacket) { // Obtain a lock on our queue mutex before modifying our queues sf::Lock anLock(mQueueMutex); // Add this packet to our list of all packets created mPackets.push_back(anPacket); // Also add this packet to our list of available outgoing packets mIncoming.push(anPacket); } else { WLOG() << "INetPool::GetIncoming() null packet returned by CreatePacket" << std::endl; } } } // Now attempt to retrieve a packet and return it to the caller if(mIncoming.size()) { // Obtain a lock on our queue mutex before modifying our queues sf::Lock anLock(mQueueMutex); // Grab the packet on the front of the queue anResult = mIncoming.front(); // Pop the packet off the queue mIncoming.pop(); } // Return the result provided above return anResult; }
void CLocalSearch::SendHits(const CList< T* >& oFiles) { CPacket* pPacket = NULL; CSchemaMap pSchemas; BYTE nHits = 0; for ( POSITION pos = oFiles.GetHeadPosition(); pos; ) { if ( ! pPacket ) pPacket = CreatePacket(); AddHit( pPacket, pSchemas, oFiles.GetNext( pos ), nHits ++ ); bool bSend = false; switch ( m_nProtocol ) { case PROTOCOL_G1: case PROTOCOL_G2: bSend = nHits >= Settings.Gnutella.HitsPerPacket || pPacket->m_nLength >= MAX_QUERY_PACKET_SIZE; break; case PROTOCOL_DC: // One hit per packet in DC++ protocol bSend = true; break; default: ASSERT( FALSE ); } // Send full packet if ( bSend ) { WriteTrailer( pPacket, pSchemas, nHits ); DispatchPacket( pPacket ); pPacket = NULL; nHits = 0; } } if ( nHits ) { WriteTrailer( pPacket, pSchemas, nHits ); DispatchPacket( pPacket ); pPacket = NULL; } ASSERT( pPacket == NULL ); ASSERT( pSchemas.IsEmpty() ); }
BasePacket* SFJsonProtocol::GetPacket(int& ErrorCode) { ErrorCode = PACKETIO_ERROR_NONE; SFJsonPacket* pPacket = (SFJsonPacket*)CreatePacket(); if (false == GetCompleteNode(pPacket)) { //ErrorCode = SFProtocol::eIncompletePacket; DisposePacket(pPacket); return NULL; } return pPacket; }
bool NetServer::SendMessage(const char* message, ENetPeer* peer) { ENetPacket* packet = CreatePacket(message, strlen(message)+1); if (!packet) return false; // Send the packet if (enet_peer_send(peer, DEFAULT_CHANNEL, packet) < 0) { // Failed to send return false; } return true; }
void ConnectionSystem::Connect(std::string ServerAddress, u16 ServerPort) { if(!m_ConnectionAttempted && !m_Connected) { m_ServerAddress = ServerAddress; m_ServerPort = ServerPort; m_LastMessageTime = m_Engine->GetElapsedTime(); sf::Packet* packet = CreatePacket(PT_CONNECT); //have to send it without Send() that requires us to be connected already. m_Socket->Send(packet, m_ServerAddress, m_ServerPort); delete packet; m_ConnectionAttempted = true; } else { m_Engine->Log("Cannot connect to server, a connection is already in progress.\n"); } }
void McuMessageHandler::SendLEDTime(time_t *timep) { struct tm *t = localtime(timep); int year, month, day, hour, minute, second, week; vector<unsigned char> vyear, vmonth, vday, vweek, vhour, vminute, vsecond; year = t->tm_year; month = t->tm_mon + 1; day = t->tm_mday; hour = t->tm_hour; minute = t->tm_min; second = t->tm_sec; week = t->tm_wday; IntToBcd(year % 100, vyear, 1); IntToBcd(month, vmonth, 1); IntToBcd(day, vday, 1); IntToBcd(hour, vhour, 1); IntToBcd(minute, vminute, 1); IntToBcd(second, vsecond, 1); IntToBcd(week, vweek, 1); for(int i = 0; i < (int)(sizeof(LEDID) / sizeof(short)); i ++) { if((LEDFLAG[i] & LED_FLAG_TIME) == 0) { continue; } StruMcuPacket *thepacket = CreatePacket(); thepacket->data = MCU_PACKET_LED_SUBTYPE_TIME; McuPacket &packet = thepacket->packet; PushByte(packet, MCU_PACKET_LED_SUBTYPE_TIME); PushSequence(packet, vyear, 1); PushSequence(packet, vmonth, 1); PushSequence(packet, vday, 1); PushSequence(packet, vweek, 1); PushSequence(packet, vhour, 1); PushSequence(packet, vminute, 1); PushSequence(packet, vsecond, 1); SendBody(thepacket, LEDID[i]); } }
void CCreatePATPacket::CreatePAT() { this->PSISize = 0; SAFE_DELETE_ARRAY(this->PSI); //まずPSI作成 //pointer_field + last_section_numberまで+PID+CRCのサイズ this->PSISize = 1 + 8 + (int)this->PIDMap.size()*4 + 4; this->PSI = new BYTE[this->PSISize]; memset( this->PSI, 0xFF, this->PSISize ); this->PSI[0] = 0; this->PSI[1] = 0; this->PSI[2] = (BYTE)(((this->PSISize-4)&0x00000F00)>>8); this->PSI[2] |= 0xB0; this->PSI[3] = (BYTE)((this->PSISize-4)&0x000000FF); this->PSI[4] = (BYTE)((this->TSID&0xFF00)>>8); this->PSI[5] = (BYTE)(this->TSID&0x00FF); this->PSI[6] = this->version<<1; this->PSI[6] |= 0xC1; this->PSI[7] = 0; this->PSI[8] = 0; DWORD dwCreateSize = 0; map<WORD, PROGRAM_PID_INFO>::iterator itr; for( itr = this->PIDMap.begin(); itr != this->PIDMap.end(); itr++ ){ this->PSI[9+dwCreateSize] = (BYTE)((itr->second.SID&0xFF00)>>8); this->PSI[9+dwCreateSize+1] = (BYTE)(itr->second.SID&0x00FF); this->PSI[9+dwCreateSize+2] = (BYTE)((itr->second.PMTPID&0xFF00)>>8); this->PSI[9+dwCreateSize+3] = (BYTE)(itr->second.PMTPID&0x00FF); dwCreateSize+=4; } unsigned long ulCrc = _Crc32(8+dwCreateSize,this->PSI+1); this->PSI[this->PSISize-4] = (BYTE)((ulCrc&0xFF000000)>>24); this->PSI[this->PSISize-3] = (BYTE)((ulCrc&0x00FF0000)>>16); this->PSI[this->PSISize-2] = (BYTE)((ulCrc&0x0000FF00)>>8); this->PSI[this->PSISize-1] = (BYTE)(ulCrc&0x000000FF); CreatePacket(); }
void McuMessageHandler::SendLEDStation(unsigned char mode, unsigned char speed, unsigned char times, unsigned char inout, unsigned char station) { for(int i = 0; i < (int)(sizeof(LEDID) / sizeof(short)); i ++) { if((LEDFLAG[i] & LED_FLAG_STATION) == 0) { continue; } StruMcuPacket *thepacket = CreatePacket(); thepacket->data = MCU_PACKET_LED_SUBTYPE_STATION; McuPacket &packet = thepacket->packet; PushByte(packet, MCU_PACKET_LED_SUBTYPE_STATION); PushByte(packet, mode); PushByte(packet, speed); PushByte(packet, times); PushByte(packet, inout); PushByte(packet, station); SendBody(thepacket, LEDID[i]); } }
void McuMessageHandler::SendGetDealRecord(int i, EnumRecordFlag clear, int index) { StruMcuPacket *thepacket = CreatePacket(); McuPacket &packet = thepacket->packet; PushByte(packet, ((m_pos_addr[i] << 4) & MCU_POS_SRC_ADDR)); PushByte(packet, 0x06); PushByte(packet, MCU_POS_FLAG); PushByte(packet, 0x38); PushByte(packet, 0x31); if(clear == CLEAR_RECORD) { PushByte(packet, 0x02); } else { PushByte(packet, 0x01); } PushWord(packet, index); thepacket->data = (int)clear; thepacket->data2 = i; SendBody(thepacket, MCU_POS_GET_DEAL); }
bool Universe::Run() { bool continueFlag; //Continue game or not char inPacket[256]; //Holds the input packet char outPacket[256]; //Holds the output packet int iResult; //The result of 'Receive' and 'Send' continueFlag = true; connectSocket = new ClientSocket(serverAddress, serverPort); printf("Connected to the server\n"); CreatePacket(outPacket, LogIn, "%s%s", login, password); connectSocket->Send(outPacket); game = NULL; currentCharacter = NULL; ClientGUIInit(); //variables for camera cameraY = 50.0f; ISceneNode* camPos=render->smgr->addEmptySceneNode(); camPos->setPosition(vector3df(50.0f,cameraY,10.0f)); camera=render->smgr->addCameraSceneNode(NULL, vector3df(50.0f, 50.0f, 10.0f), vector3df(50.0f, 0.0f, 40.0f)); scene::ISceneNode* lnode; lnode = render->smgr->addLightSceneNode(NULL, camPos->getPosition(), video::SColorf(1.0f, 1.0f, 1.0f, 1.0f), 800.0f); render->smgr->setAmbientLight(video::SColor(0, 60, 60, 60)); state = Continue; int lastUpdate = render->device->getTimer()->getTime(); while (render->device->run() && state == Continue) { //Receving packet from the server iResult = connectSocket->Receive(inPacket); if (iResult) { if (iResult > 0) { //Packet received switch (GetPacketType(inPacket)) { case LoggedIn: char gameName[256]; int locationId; ScanPacket(inPacket, "%s%i", gameName, &locationId); game = new Game(gameName, Client); printf("Game %s initialized\n", game->name); currentLocation = game->data->GetLocation(locationId); DrawScene(); break; case NPCSpawned: currentLocation->SpawnNPC(new CurrentNPC(inPacket)); break; case StaticSpawned: { CurrentStatic* currentStatic = new CurrentStatic(inPacket); currentLocation->SpawnStatic(currentStatic); break; } case ItemSpawned: char spawnType; //TODO: SpawnType as char ScanPacket(inPacket, "%i%i%f%f%b", NULL, NULL, NULL, NULL, &spawnType); switch(spawnType) { case Ground: currentLocation->SpawnItem(new CurrentItem(inPacket)); break; case Inventory: currentCharacter->SpawnItem(new CurrentItem(inPacket)); break; } break; case CharacterSpawned: if (!currentCharacter) { currentCharacter = new CurrentCharacter(inPacket); currentLocation->SpawnCharacter(currentCharacter); } else { currentLocation->SpawnCharacter(new CurrentCharacter(inPacket)); } break; case SkillSpawned: currentCharacter->SpawnSkill(new CurrentSkill(inPacket)); break; case NPCUnspawned: currentLocation->UnSpawnNPC(currentLocation->GetNPC(PacketGetInt(inPacket, 1))); break; case StaticUnspawned: currentLocation->UnSpawnStatic(currentLocation->GetStatic(PacketGetInt(inPacket, 1))); break; case ItemUnspawned: switch(PacketGetByte(inPacket, 5)) { case Ground: currentLocation->UnSpawnItem(currentLocation->GetItem(PacketGetInt(inPacket, 1))); break; case Inventory: //currentCharacter->UnSpawnItem(currentCharacter->GetItem(PacketGetInt(inPacket, 1))); break; } break; case CharacterUnspawned: currentLocation->UnSpawnCharacter(currentLocation->GetCharacter(PacketGetInt(inPacket, 1))); break; case Say: { IGUIElement* eb = guienv->getRootGUIElement()->getElementFromId(ChatBox)->getElementFromId(ChatEditBox); char messageType; //TODO: MessageType as char int senderCurrentCharacterId; wchar_t messageText[CHAT_MESSAGE_MAX_LENGTH]; ScanPacket(inPacket, "%b%i%ws", &messageType, &senderCurrentCharacterId, messageText); CurrentCharacter* sender = game->data->GetCharacter(senderCurrentCharacterId); wchar_t wLogin[64]; mbstowcs(wLogin, sender->login, 63); int offset = wcslen(eb->getText()) + wcslen(wLogin) + wcslen(messageText) + 3 - (CHAT_MAX_LENGTH - 1); if (offset < 0) offset = 0; else if (offset >= CHAT_MAX_LENGTH) offset = CHAT_MAX_LENGTH - 1; wchar_t wstr[CHAT_MAX_LENGTH]; swprintf(wstr, L"%ls\n%ls: %ls", eb->getText() + offset, wLogin, messageText); eb->setText(wstr); //delete wstr; break; } case CharacterMoving: { int currentCharacterId; f32 x, y; ScanPacket(inPacket, "%i%f%f", ¤tCharacterId, &x, &y); CurrentCharacter* movingCurrentCharacter = currentLocation->GetCharacter(currentCharacterId); movingCurrentCharacter->setAnimation(EMAT_RUN); render->moveNode(movingCurrentCharacter->node, vector3df(x * CELL_SIZE, 0, y * CELL_SIZE), movingCurrentCharacter->base->speed); //TEST movingCurrentCharacter->x = x; movingCurrentCharacter->y = y; break; } case HpChanged: { int characterId, changedHp; CurrentCharacter *character; ScanPacket(inPacket, "%i%i", &characterId, &changedHp); if (character = currentLocation->GetCharacter(characterId)) character->hp = changedHp; break; } case CharacterDied: { //okay break; } case CharacterMoved: { int characterId; f32 whereX, whereY; ScanPacket(inPacket, "%i%f%f", &characterId, &whereX, &whereY); printf("CLIENT CHAR ID: %d\n", characterId); printf("CLIENT WHERE X: %.f\n", whereX); printf("CLIENT WHERE Y: %.f\n", whereY); CurrentCharacter *character = currentLocation->GetCharacter(characterId); if (character) { printf("CLIENT TEST 1\n"); //character->node->setPosition(vector3df(whereX * CELL_SIZE, character->node->getPosition().Y, whereY * CELL_SIZE)); //TODO: Why setPosition is not working?!! render->moveNode(character->node, vector3df(whereX * CELL_SIZE, character->node->getPosition().Y, whereY * CELL_SIZE), 1000000.0f); printf("CLIENT TEST 2\n"); character->x = whereX; character->y = whereY; } break; } case DialogOpened: { char title[256]; char text[4096]; wchar_t wstr[512]; //npcID = PacketGetInt(inPacket, 1); //strcpy(title, PacketGetString(inPacket, 5)); sprintf(title, "[%d] %s", PacketGetInt(inPacket, 1), PacketGetString(inPacket, 5)); strcpy(text, PacketGetString(inPacket, strlen(PacketGetString(inPacket, 5)) + 5 + 1)); mbstowcs(wstr, title, 255); IGUIWindow* wnd = guienv->addWindow(rect<s32>(256, 128, 256 + 256, 128 + 320), false, wstr, NULL, -1); char patterns[][256] = { "<p\\s+rect\\s*=\\s*\\\"(.*?);(.*?);(.*?);(.*?)\\\">(.*?)</p>", "<button\\s+rect\\s*=\\s*\\\"(.*?);(.*?);(.*?);(.*?)\\\"\\s+onclick\\s*=\\s*\\\"(.*?)\\\">(.*?)</button>", /* "<p>(.*?)</p>", "<p>(.*?)</p>", "<p>(.*?)</p>",*/ }; char** result; int patternsCount = 2; const char *error; int erroffset; int count; int ovector[30]; const unsigned char *tables = NULL; tables = pcre_maketables(); for (int i = 0; i < patternsCount; i++) { pcre *re = pcre_compile ((char*)patterns[i], 0, &error, &erroffset, NULL); count = pcre_exec(re, NULL, (char*)text, strlen(text), 0, NULL, ovector, 30); if (count > 0) { result = new char*[count]; for (int c = 0; c < 2 * count; c += 2) { if (ovector[c] >= 0) { result[c / 2] = new char[ovector[c + 1] - ovector[c] + 1]; memcpy(result[c / 2], text + ovector[c], ovector[c + 1] - ovector[c]); result[c / 2][ovector[c + 1] - ovector[c]] = '\0'; //printf("%d, %d\n", ovector[c], ovector[c + 1]); //printf("%s\n", result[c / 2]); } else { result[c / 2] = NULL; } } switch (i) { case 0: //p { wchar_t wstr[1024]; mbstowcs(wstr, result[5], 1023); guienv->addStaticText(wstr, rect<s32>(atoi(result[1]), atoi(result[2]), atoi(result[1]) + atoi(result[3]), atoi(result[2]) + atoi(result[4])), false, true, wnd, DialogElement, false); break; } case 1: //button { wchar_t wstr[256]; mbstowcs(wstr, result[6], 255); guienv->addButton(rect<s32>(atoi(result[1]), atoi(result[2]), atoi(result[1]) + atoi(result[3]), atoi(result[2]) + atoi(result[4])), wnd, DialogElement + atoi(result[5]), wstr, NULL); break; } } for (int j = 0; j < count; j++) if (result[j]) delete result[j]; delete result; } } break; } case PlayEffect: { CurrentMapObject<MapObject>* currentMapObject; int currentMapObjectId = PacketGetInt(inPacket, 2); int skillId = PacketGetInt(inPacket, 6); switch (PacketGetByte(inPacket, 1)) { case 0: //NPC currentMapObject = (CurrentMapObject<MapObject>*)currentLocation->GetNPC(currentMapObjectId); break; case 3: //Character currentMapObject = (CurrentMapObject<MapObject>*)currentLocation->GetCharacter(currentMapObjectId); break; } render->PlayEffect(currentMapObject->node, game->resources->GetSkill(skillId)->effectTextures); break; } case PlayAdvancedEffect: int skillId = PacketGetInt(inPacket, 1); f32 xStart = PacketGetInt(inPacket, 5) * CELL_SIZE; f32 yStart = PacketGetInt(inPacket, 9) * CELL_SIZE; f32 xEnd = PacketGetInt(inPacket, 13) * CELL_SIZE; f32 yEnd = PacketGetInt(inPacket, 17) * CELL_SIZE; render->Effect2(vector3df(xStart, 5.0f, yStart), vector3df(xEnd, 5.0f, yEnd)); //TEST break; } } else if (iResult == -1) { //Disconnected from the server printf("Disconnected from the server\n"); delete Universe::instance->login; delete Universe::instance->password; Universe::instance->login = NULL; Universe::instance->password = NULL; Universe::instance->state = NextLevel; } else { //Wrong packet from the client printf("Warning! Wrong packet from server. Error code: %d\n", iResult); } } //Drawing if ((render->device->getTimer()->getTime() - lastUpdate) > 30) { lastUpdate = render->device->getTimer()->getTime(); if (game) { if (currentCharacter) { render->Km = camPos->getPosition(); render->Kt = camera->getTarget(); //Kt.X = currentCharacter->x * CELL_SIZE; //Kt.Z = currentCharacter->y * CELL_SIZE; vector3df pos = currentCharacter->node->getPosition(); render->Kt.X = pos.X; render->Kt.Z = pos.Z; render->Km.X = render->Kt.X; render->Km.Z = render->Kt.Z - 30; render->Km.Y = cameraY; camera->setPosition(render->Km); camera->setTarget(render->Kt); //vector3df lPos = camera->getPosition(); vector3df lPos = currentCharacter->node->getPosition(); lPos.Y = 15; //lPos.Z += 20; lnode->setPosition(lPos); } render->driver->beginScene(true, true, SColor(255,100,101,140)); render->smgr->drawAll(); guienv->drawAll(); render->driver->endScene(); } } } ClientGUIDestroy(); delete connectSocket; if (game) { delete game; render->smgr->clear(); } if (state == NextLevel) return false; return true; }