diff --git a/common/net/daybreak_connection.cpp b/common/net/daybreak_connection.cpp index a4b793a45..1cdcc23ea 100644 --- a/common/net/daybreak_connection.cpp +++ b/common/net/daybreak_connection.cpp @@ -47,7 +47,7 @@ void EQ::Net::DaybreakConnectionManager::Attach(uv_loop_t *loop) m_socket.data = this; struct sockaddr_in recv_addr; uv_ip4_addr("0.0.0.0", m_options.port, &recv_addr); - int rc = uv_udp_bind(&m_socket, (const struct sockaddr *)&recv_addr, 0); + int rc = uv_udp_bind(&m_socket, (const struct sockaddr *)&recv_addr, UV_UDP_REUSEADDR); rc = uv_udp_recv_start(&m_socket, [](uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) { @@ -65,13 +65,6 @@ void EQ::Net::DaybreakConnectionManager::Attach(uv_loop_t *loop) char endpoint[16]; uv_ip4_name((const sockaddr_in*)addr, endpoint, 16); auto port = ntohs(((const sockaddr_in*)addr)->sin_port); - - //raw data in - if(nread > 0) { - EQ::Net::StaticPacket wire((void*)buf->base, (uint64_t)nread); - Log.OutF(Logs::Detail, Logs::Netcode, "Raw Packet in\n{0}", wire.ToString()); - } - c->ProcessPacket(endpoint, port, buf->base, nread); delete[] buf->base; }); @@ -92,14 +85,14 @@ void EQ::Net::DaybreakConnectionManager::Detach() void EQ::Net::DaybreakConnectionManager::Connect(const std::string &addr, int port) { //todo dns resolution - + auto connection = std::shared_ptr(new DaybreakConnection(this, addr, port)); connection->m_self = connection; - + if (m_on_new_connection) { m_on_new_connection(connection); } - + m_connections.insert(std::make_pair(std::make_pair(addr, port), connection)); } @@ -125,7 +118,8 @@ void EQ::Net::DaybreakConnectionManager::Process() connection->ChangeStatus(StatusDisconnecting); continue; } - } else if (status == StatusConnected) { + } + else if (status == StatusConnected) { auto time_since_last_recv = std::chrono::duration_cast(now - connection->m_last_recv); if ((size_t)time_since_last_recv.count() > m_options.stale_connection_ms) { iter = m_connections.erase(iter); @@ -142,7 +136,7 @@ void EQ::Net::DaybreakConnectionManager::Process() connection->SendConnect(); } } - break; + break; case StatusConnected: { if (m_options.keepalive_delay_ms != 0) { auto time_since_last_send = std::chrono::duration_cast(now - connection->m_last_send); @@ -183,11 +177,9 @@ void EQ::Net::DaybreakConnectionManager::ProcessResend() } } -void EQ::Net::DaybreakConnectionManager::ProcessPacket(const std::string &endpoint, int port, const char *data, uint64_t size) +void EQ::Net::DaybreakConnectionManager::ProcessPacket(const std::string &endpoint, int port, const char *data, size_t size) { - Log.OutF(Logs::Detail, Logs::Netcode, "Recv {0:#x} from {1}:{2}.", data[1], endpoint, port); if (m_options.simulated_in_packet_loss && m_options.simulated_in_packet_loss >= m_rand.Int(0, 100)) { - Log.OutF(Logs::Detail, Logs::Netcode, "Dropped a packet from {0}:{1} because of simulated packet loss", endpoint, port); return; } @@ -216,8 +208,7 @@ void EQ::Net::DaybreakConnectionManager::ProcessPacket(const std::string &endpoi m_connections.insert(std::make_pair(std::make_pair(endpoint, port), connection)); connection->ProcessPacket(p); } - else if(data[1] != OP_OutOfSession) { - Log.OutF(Logs::Detail, Logs::Netcode, "Recv {0:#x} from {1}:{2} but there is no identified connection from this address.", data[1], endpoint, port); + else if (data[1] != OP_OutOfSession) { SendDisconnect(endpoint, port); } } @@ -242,29 +233,24 @@ void EQ::Net::DaybreakConnectionManager::SendDisconnect(const std::string &addr, { DaybreakDisconnect header; header.zero = 0; - header.opcode = OP_OutOfSession; + header.opcode = OP_SessionDisconnect; header.connect_code = 0; - + DynamicPacket out; out.PutSerialize(0, header); - - uv_udp_send_t *send_req = new uv_udp_send_t; - memset(send_req, 0, sizeof(*send_req)); + uv_udp_send_t *send_req = new uv_udp_send_t; sockaddr_in send_addr; uv_ip4_addr(addr.c_str(), port, &send_addr); uv_buf_t send_buffers[1]; - char *data = new char[(size_t)out.Length()]; - memcpy(data, out.Data(), (size_t)out.Length()); - send_buffers[0] = uv_buf_init(data, (unsigned int)out.Length()); + char *data = new char[out.Length()]; + memcpy(data, out.Data(), out.Length()); + send_buffers[0] = uv_buf_init(data, out.Length()); send_req->data = send_buffers[0].base; - - Log.OutF(Logs::Detail, Logs::Netcode, "Sending disconnect to {0}:{1}", addr, port); - int ret = uv_udp_send(send_req, &m_socket, send_buffers, 1, (sockaddr*)&send_addr, [](uv_udp_send_t* req, int status) { - delete[] (char*)req->data; + delete[](char*)req->data; delete req; }); } @@ -280,7 +266,7 @@ EQ::Net::DaybreakConnection::DaybreakConnection(DaybreakConnectionManager *owner m_port = port; m_connect_code = NetworkToHost(connect.connect_code); m_encode_key = m_owner->m_rand.Int(std::numeric_limits::min(), std::numeric_limits::max()); - m_max_packet_size = (uint32_t)std::min((uint32_t)owner->m_options.max_packet_size, (uint32_t)NetworkToHost(connect.max_packet_size)); + m_max_packet_size = (uint32_t)std::min(owner->m_options.max_packet_size, (size_t)NetworkToHost(connect.max_packet_size)); m_crc_bytes = (uint32_t)owner->m_options.crc_length; m_encode_passes[0] = owner->m_options.encode_passes[0]; m_encode_passes[1] = owner->m_options.encode_passes[1]; @@ -292,7 +278,6 @@ EQ::Net::DaybreakConnection::DaybreakConnection(DaybreakConnectionManager *owner m_combined[0] = 0; m_combined[1] = OP_Combined; m_last_session_stats = Clock::now(); - m_last_stats = Clock::now(); } //new connection made as client @@ -316,7 +301,6 @@ EQ::Net::DaybreakConnection::DaybreakConnection(DaybreakConnectionManager *owner m_combined[0] = 0; m_combined[1] = OP_Combined; m_last_session_stats = Clock::now(); - m_last_stats = Clock::now(); } EQ::Net::DaybreakConnection::~DaybreakConnection() @@ -325,7 +309,6 @@ EQ::Net::DaybreakConnection::~DaybreakConnection() void EQ::Net::DaybreakConnection::Close() { - Log.OutF(Logs::Detail, Logs::Netcode, "Closing daybreak connection, status was {0}", (int)m_status); if (m_status == StatusConnected) { DaybreakDisconnect disconnect; disconnect.zero = 0; @@ -372,20 +355,20 @@ void EQ::Net::DaybreakConnection::ResetStats() void EQ::Net::DaybreakConnection::Process() { try { - m_resend_delay = (uint64_t)(m_stats.last_stat_ping * m_owner->m_options.resend_delay_factor) + m_owner->m_options.resend_delay_ms; + m_resend_delay = (size_t)(m_stats.last_stat_ping * m_owner->m_options.resend_delay_factor) + m_owner->m_options.resend_delay_ms; if (m_resend_delay > 1000) { m_resend_delay = 1000; } auto now = Clock::now(); - auto time_since_hold = (uint64_t)std::chrono::duration_cast(now - m_hold_time).count(); + auto time_since_hold = (size_t)std::chrono::duration_cast(now - m_hold_time).count(); if (time_since_hold >= m_owner->m_options.hold_length_ms) { FlushBuffer(); } ProcessQueue(); - auto time_since_stats = (uint64_t)std::chrono::duration_cast(now - m_last_stats).count(); + auto time_since_stats = (size_t)std::chrono::duration_cast(now - m_last_stats).count(); if (m_owner->m_options.stats_delay_ms > 0 && time_since_stats >= m_owner->m_options.stats_delay_ms) { SendStatSync(); m_last_stats = now; @@ -403,7 +386,6 @@ void EQ::Net::DaybreakConnection::ProcessPacket(Packet &p) m_stats.recv_bytes += p.Length(); if (p.Length() < 1) { - Log.OutF(Logs::Detail, Logs::Netcode, "Error parsing packet, length was < 1"); return; } @@ -411,10 +393,9 @@ void EQ::Net::DaybreakConnection::ProcessPacket(Packet &p) Log.OutF(Logs::Detail, Logs::Netcode, "Error parsing packet, did not start with a 0 frame, not a valid protocol packet."); return; } - + auto opcode = p.GetInt8(1); if (opcode == OP_KeepAlive || opcode == OP_OutboundPing) { - Log.OutF(Logs::Detail, Logs::Netcode, "Discard packet of type {0:#x}", opcode); return; } @@ -453,12 +434,12 @@ void EQ::Net::DaybreakConnection::ProcessQueue() for (int i = 0; i < 4; ++i) { auto stream = &m_streams[i]; for (;;) { - + auto iter = stream->packet_queue.find(stream->sequence_in); if (iter == stream->packet_queue.end()) { break; } - + auto packet = iter->second; stream->packet_queue.erase(iter); ProcessDecodedPacket(*packet); @@ -494,24 +475,18 @@ void EQ::Net::DaybreakConnection::ProcessDecodedPacket(const Packet &p) { if (p.GetInt8(0) == 0) { if (p.Length() < 2) { - Log.OutF(Logs::Detail, Logs::Netcode, "Failed to process decoded packet, length < 2\n{0}", p.ToString()); return; } - Log.OutF(Logs::Detail, Logs::Netcode, "Processing decoded packet of type {0:#x}", p.GetInt8(1)); switch (p.GetInt8(1)) { case OP_Combined: { - Log.OutF(Logs::Detail, Logs::Netcode, "Processing OP_Combined of length {0}", p.Length()); char *current = (char*)p.Data() + 2; char *end = (char*)p.Data() + p.Length(); while (current < end) { uint8_t subpacket_length = *(uint8_t*)current; current += 1; - Log.OutF(Logs::Detail, Logs::Netcode, "Subpacket length {0}", subpacket_length); - if (end < current + subpacket_length) { - Log.OutF(Logs::Detail, Logs::Netcode, "Subpacket length out of range, bailing"); return; } @@ -520,10 +495,9 @@ void EQ::Net::DaybreakConnection::ProcessDecodedPacket(const Packet &p) } break; } - + case OP_AppCombined: { - Log.OutF(Logs::Detail, Logs::Netcode, "Processing OP_AppCombined of length {0}", p.Length()); uint8_t *current = (uint8_t*)p.Data() + 2; uint8_t *end = (uint8_t*)p.Data() + p.Length(); @@ -532,20 +506,18 @@ void EQ::Net::DaybreakConnection::ProcessDecodedPacket(const Packet &p) if (*current == 0xFF) { if (end < current + 3) { - Log.OutF(Logs::Detail, Logs::Netcode, "Error in OP_AppCombined, end < current + 3"); throw std::out_of_range("Error in OP_AppCombined, end < current + 3"); } if (*(current + 1) == 0xFF && *(current + 2) == 0xFF) { if (end < current + 7) { - Log.OutF(Logs::Detail, Logs::Netcode, "Error in OP_AppCombined, end < current + 7"); throw std::out_of_range("Error in OP_AppCombined, end < current + 7"); } subpacket_length = (uint32_t)( - (*(current + 3) << 24) | - (*(current + 4) << 16) | - (*(current + 5) << 8) | + (*(current + 3) << 24) | + (*(current + 4) << 16) | + (*(current + 5) << 8) | (*(current + 6)) ); current += 7; @@ -563,7 +535,6 @@ void EQ::Net::DaybreakConnection::ProcessDecodedPacket(const Packet &p) current += 1; } - Log.OutF(Logs::Detail, Logs::Netcode, "Processing subpacket of length {0}", subpacket_length); ProcessDecodedPacket(StaticPacket(current, subpacket_length)); current += subpacket_length; } @@ -571,7 +542,6 @@ void EQ::Net::DaybreakConnection::ProcessDecodedPacket(const Packet &p) case OP_SessionRequest: { - Log.OutF(Logs::Detail, Logs::Netcode, "OP_SessionRequest recieved current status is {0}", m_status); if (m_status == StatusConnected) { auto request = p.GetSerialize(0); @@ -598,7 +568,6 @@ void EQ::Net::DaybreakConnection::ProcessDecodedPacket(const Packet &p) case OP_SessionResponse: { - Log.OutF(Logs::Detail, Logs::Netcode, "OP_SessionResponse recieved current status is {0}", m_status); if (m_status == StatusConnecting) { auto reply = p.GetSerialize(0); @@ -624,20 +593,15 @@ void EQ::Net::DaybreakConnection::ProcessDecodedPacket(const Packet &p) auto stream_id = header.opcode - OP_Packet; auto stream = &m_streams[stream_id]; - Log.OutF(Logs::Detail, Logs::Netcode, "Recv Reliable Packet for stream {0} with sequence {1}", stream_id, sequence); - auto order = CompareSequence(stream->sequence_in, sequence); if (order == SequenceFuture) { - Log.OutF(Logs::Detail, Logs::Netcode, "Reliable packet resolved to be a future packet, adding to the packet queue"); SendOutOfOrderAck(stream_id, sequence); AddToQueue(stream_id, sequence, p); } else if (order == SequencePast) { - Log.OutF(Logs::Detail, Logs::Netcode, "Reliable packet resolved to be a past packet, sending an ack"); SendAck(stream_id, stream->sequence_in - 1); } else { - Log.OutF(Logs::Detail, Logs::Netcode, "Reliable packet is the one we want, removing it from queue and processing it."); RemoveFromQueue(stream_id, sequence); SendAck(stream_id, stream->sequence_in); stream->sequence_in++; @@ -658,25 +622,20 @@ void EQ::Net::DaybreakConnection::ProcessDecodedPacket(const Packet &p) auto stream_id = header.opcode - OP_Fragment; auto stream = &m_streams[stream_id]; - Log.OutF(Logs::Detail, Logs::Netcode, "Recv Reliable Fragment Packet for stream {0} with sequence {1}", stream_id, sequence); - auto order = CompareSequence(stream->sequence_in, sequence); - + if (order == SequenceFuture) { - Log.OutF(Logs::Detail, Logs::Netcode, "Reliable fragment packet resolved to be a future packet, adding to the packet queue"); SendOutOfOrderAck(stream_id, sequence); AddToQueue(stream_id, sequence, p); } else if (order == SequencePast) { - Log.OutF(Logs::Detail, Logs::Netcode, "Reliable fragment packet resolved to be a past packet, sending an ack"); SendAck(stream_id, stream->sequence_in - 1); } else { - Log.OutF(Logs::Detail, Logs::Netcode, "Reliable fragment packet is the one we want, removing it from queue and processing it."); RemoveFromQueue(stream_id, sequence); SendAck(stream_id, stream->sequence_in); stream->sequence_in++; - + if (stream->fragment_total_bytes == 0) { auto fragheader = p.GetSerialize(0); stream->fragment_total_bytes = NetworkToHost(fragheader.total_size); @@ -687,8 +646,6 @@ void EQ::Net::DaybreakConnection::ProcessDecodedPacket(const Packet &p) (char*)p.Data() + DaybreakReliableFragmentHeader::size(), p.Length() - DaybreakReliableFragmentHeader::size()); stream->fragment_current_bytes += (uint32_t)(p.Length() - DaybreakReliableFragmentHeader::size()); - - Log.OutF(Logs::Detail, Logs::Netcode, "Fragment packet created {0}/{1} bytes", stream->fragment_current_bytes, stream->fragment_total_bytes); } else { stream->fragment_packet.PutData( @@ -696,15 +653,13 @@ void EQ::Net::DaybreakConnection::ProcessDecodedPacket(const Packet &p) (char*)p.Data() + DaybreakReliableHeader::size(), p.Length() - DaybreakReliableHeader::size()); stream->fragment_current_bytes += (uint32_t)(p.Length() - DaybreakReliableHeader::size()); - + if (stream->fragment_current_bytes >= stream->fragment_total_bytes) { ProcessDecodedPacket(stream->fragment_packet); stream->fragment_packet.Clear(); stream->fragment_total_bytes = 0; stream->fragment_current_bytes = 0; } - - Log.OutF(Logs::Detail, Logs::Netcode, "Fragment packet accumulated {0}/{1} bytes", stream->fragment_current_bytes, stream->fragment_total_bytes); } } @@ -719,8 +674,6 @@ void EQ::Net::DaybreakConnection::ProcessDecodedPacket(const Packet &p) auto header = p.GetSerialize(0); auto sequence = NetworkToHost(header.sequence); auto stream_id = header.opcode - OP_Ack; - - Log.OutF(Logs::Detail, Logs::Netcode, "Ack recv for stream {0} and sequence {1}", stream_id, sequence); Ack(stream_id, sequence); break; } @@ -733,15 +686,12 @@ void EQ::Net::DaybreakConnection::ProcessDecodedPacket(const Packet &p) auto header = p.GetSerialize(0); auto sequence = NetworkToHost(header.sequence); auto stream_id = header.opcode - OP_OutOfOrderAck; - - Log.OutF(Logs::Detail, Logs::Netcode, "Out of Order Ack recv for stream {0} and sequence {1}", stream_id, sequence); OutOfOrderAck(stream_id, sequence); break; } case OP_SessionDisconnect: { - Log.OutF(Logs::Detail, Logs::Netcode, "OP_SessionDisconnect recieved current status is {0}", m_status); if (m_status == StatusConnected || m_status == StatusDisconnecting) { DaybreakDisconnect disconnect; disconnect.zero = 0; @@ -758,7 +708,6 @@ void EQ::Net::DaybreakConnection::ProcessDecodedPacket(const Packet &p) case OP_Padding: { - Log.OutF(Logs::Detail, Logs::Netcode, "OP_Padding recv, processing packet"); auto self = m_self.lock(); if (m_owner->m_on_packet_recv && self) { m_owner->m_on_packet_recv(self, StaticPacket((char*)p.Data() + 1, p.Length() - 1)); @@ -767,14 +716,13 @@ void EQ::Net::DaybreakConnection::ProcessDecodedPacket(const Packet &p) } case OP_SessionStatRequest: { - Log.OutF(Logs::Detail, Logs::Netcode, "OP_SessionStatRequest recv"); auto request = p.GetSerialize(0); DaybreakSessionStatResponse response; response.zero = 0; response.opcode = OP_SessionStatResponse; response.timestamp = request.timestamp; - response.our_timestamp = (uint32_t)EQ::Net::HostToNetwork(std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count()); + response.our_timestamp = EQ::Net::HostToNetwork(std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count()); response.client_sent = request.packets_sent; response.client_recv = request.packets_recv; response.server_sent = EQ::Net::HostToNetwork(m_stats.sent_packets); @@ -785,7 +733,6 @@ void EQ::Net::DaybreakConnection::ProcessDecodedPacket(const Packet &p) break; } case OP_SessionStatResponse: - Log.OutF(Logs::Detail, Logs::Netcode, "OP_SessionStatResponse recv, not yet implemented."); break; default: Log.OutF(Logs::Detail, Logs::Netcode, "Unhandled opcode {0:#x}", p.GetInt8(1)); @@ -793,7 +740,6 @@ void EQ::Net::DaybreakConnection::ProcessDecodedPacket(const Packet &p) } } else { - Log.OutF(Logs::Detail, Logs::Netcode, "Recv raw app packet, processing"); auto self = m_self.lock(); if (m_owner->m_on_packet_recv && self) { m_owner->m_on_packet_recv(self, p); @@ -807,7 +753,7 @@ bool EQ::Net::DaybreakConnection::ValidateCRC(Packet &p) return true; } - if (p.Length() < (uint64_t)m_crc_bytes) { + if (p.Length() < (size_t)m_crc_bytes) { return false; } @@ -816,12 +762,12 @@ bool EQ::Net::DaybreakConnection::ValidateCRC(Packet &p) int actual = 0; switch (m_crc_bytes) { case 2: - actual = NetworkToHost(*(int16_t*)&data[p.Length() - (uint64_t)m_crc_bytes]) & 0xffff; - calculated = Crc32(data, (int)(p.Length() - (uint64_t)m_crc_bytes), m_encode_key) & 0xffff; + actual = NetworkToHost(*(int16_t*)&data[p.Length() - (size_t)m_crc_bytes]) & 0xffff; + calculated = Crc32(data, (int)(p.Length() - (size_t)m_crc_bytes), m_encode_key) & 0xffff; break; case 4: - actual = NetworkToHost(*(int32_t*)&data[p.Length() - (uint64_t)m_crc_bytes]); - calculated = Crc32(data, (int)(p.Length() - (uint64_t)m_crc_bytes), m_encode_key); + actual = NetworkToHost(*(int32_t*)&data[p.Length() - (size_t)m_crc_bytes]); + calculated = Crc32(data, (int)(p.Length() - (size_t)m_crc_bytes), m_encode_key); break; default: return false; @@ -839,7 +785,7 @@ void EQ::Net::DaybreakConnection::AppendCRC(Packet &p) if (m_crc_bytes == 0U) { return; } - + int calculated = 0; switch (m_crc_bytes) { case 2: @@ -861,7 +807,6 @@ void EQ::Net::DaybreakConnection::ChangeStatus(DbProtocolStatus new_status) } } - Log.OutF(Logs::Detail, Logs::Netcode, "Status changed from {0} to {1}", (int)m_status, (int)new_status); m_status = new_status; } @@ -879,12 +824,12 @@ bool EQ::Net::DaybreakConnection::PacketCanBeEncoded(Packet &p) const return true; } -void EQ::Net::DaybreakConnection::Decode(Packet &p, uint64_t offset, uint64_t length) +void EQ::Net::DaybreakConnection::Decode(Packet &p, size_t offset, size_t length) { int key = m_encode_key; char *buffer = (char*)p.Data() + offset; - uint64_t i = 0; + size_t i = 0; for (i = 0; i + 4 <= length; i += 4) { int pt = (*(int*)&buffer[i]) ^ (key); @@ -899,12 +844,12 @@ void EQ::Net::DaybreakConnection::Decode(Packet &p, uint64_t offset, uint64_t le } } -void EQ::Net::DaybreakConnection::Encode(Packet &p, uint64_t offset, uint64_t length) +void EQ::Net::DaybreakConnection::Encode(Packet &p, size_t offset, size_t length) { int key = m_encode_key; char *buffer = (char*)p.Data() + offset; - uint64_t i = 0; + size_t i = 0; for (i = 0; i + 4 <= length; i += 4) { int pt = (*(int*)&buffer[i]) ^ (key); @@ -988,7 +933,7 @@ uint32_t Deflate(const uint8_t* in, uint32_t in_len, uint8_t* out, uint32_t out_ } } -void EQ::Net::DaybreakConnection::Decompress(Packet &p, uint64_t offset, uint64_t length) +void EQ::Net::DaybreakConnection::Decompress(Packet &p, size_t offset, size_t length) { if (length < 2) { return; @@ -1002,7 +947,7 @@ void EQ::Net::DaybreakConnection::Decompress(Packet &p, uint64_t offset, uint64_ new_length = Inflate(buffer + 1, (uint32_t)length - 1, new_buffer, 4096); } else if (buffer[0] == 0xa5) { - memcpy(new_buffer, buffer + 1, (size_t)length - 1); + memcpy(new_buffer, buffer + 1, length - 1); new_length = (uint32_t)length - 1; } else { @@ -1013,7 +958,7 @@ void EQ::Net::DaybreakConnection::Decompress(Packet &p, uint64_t offset, uint64_ p.PutData(offset, new_buffer, new_length); } -void EQ::Net::DaybreakConnection::Compress(Packet &p, uint64_t offset, uint64_t length) +void EQ::Net::DaybreakConnection::Compress(Packet &p, size_t offset, size_t length) { uint8_t new_buffer[2048] = { 0 }; uint8_t *buffer = (uint8_t*)p.Data() + offset; @@ -1024,9 +969,9 @@ void EQ::Net::DaybreakConnection::Compress(Packet &p, uint64_t offset, uint64_t new_buffer[0] = 0x5a; } else { - memcpy(new_buffer + 1, buffer, (size_t)length); + memcpy(new_buffer + 1, buffer, length); new_buffer[0] = 0xa5; - new_length = (size_t)length + 1; + new_length = length + 1; } p.Resize(offset); @@ -1046,17 +991,15 @@ void EQ::Net::DaybreakConnection::ProcessResend(int stream) auto s = &m_streams[stream]; for (auto &entry : s->sent_packets) { auto time_since_last_send = std::chrono::duration_cast(now - entry.second.last_sent); - if(entry.second.times_resent == 0) { - if ((uint64_t)time_since_last_send.count() > m_resend_delay) { - Log.OutF(Logs::Detail, Logs::Netcode, "Resending reliable packet\n{0}", entry.second.packet.ToString()); + if (entry.second.times_resent == 0) { + if ((size_t)time_since_last_send.count() > m_resend_delay) { InternalBufferedSend(entry.second.packet); entry.second.last_sent = now; entry.second.times_resent++; } } else { - if ((uint64_t)time_since_last_send.count() > std::min(m_resend_delay / (entry.second.times_resent + 1), (uint64_t)50ULL)) { - Log.OutF(Logs::Detail, Logs::Netcode, "Resending reliable packet\n{0}", entry.second.packet.ToString()); + if ((size_t)time_since_last_send.count() > std::min(m_resend_delay / (entry.second.times_resent + 1), (size_t)5ULL)) { InternalBufferedSend(entry.second.packet); entry.second.last_sent = now; entry.second.times_resent++; @@ -1133,22 +1076,19 @@ void EQ::Net::DaybreakConnection::SendStatSync() DaybreakSessionStatRequest request; request.zero = 0; request.opcode = OP_SessionStatRequest; - request.timestamp = (uint32_t)EQ::Net::HostToNetwork(std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count() & 0xFFFFLL); - request.stat_ping = (uint32_t)m_stats.last_stat_ping; + request.timestamp = EQ::Net::HostToNetwork(std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count() & 0xFFFFLL); + request.stat_ping = m_stats.last_stat_ping; if (m_stats.total_stat_count > 0) - request.avg_ping = (uint32)(m_stats.total_stat_ping / m_stats.total_stat_count); + request.avg_ping = m_stats.total_stat_ping / m_stats.total_stat_count; else request.avg_ping = 0; - request.min_ping = (uint32_t)m_stats.min_stat_ping; - request.max_ping = (uint32_t)m_stats.max_stat_ping; - request.last_ping = (uint32_t)m_stats.last_stat_ping; - request.packets_sent = (uint32_t)m_stats.sent_packets + 1; - request.packets_recv = (uint32_t)m_stats.recv_packets; + request.min_ping = m_stats.min_stat_ping; + request.max_ping = m_stats.max_stat_ping; + request.last_ping = m_stats.last_stat_ping; + request.packets_sent = m_stats.sent_packets + 1; + request.packets_recv = m_stats.recv_packets; m_last_session_stats = Clock::now(); - - EQ::Net::DynamicPacket p; - p.PutSerialize(0, request); - InternalSend(p); + //InternalSend() } void EQ::Net::DaybreakConnection::InternalBufferedSend(Packet &p) @@ -1160,7 +1100,7 @@ void EQ::Net::DaybreakConnection::InternalBufferedSend(Packet &p) } //we could add this packet to a combined - uint64_t raw_size = DaybreakHeader::size() + (uint64_t)m_crc_bytes + m_buffered_packets_length + m_buffered_packets.size() + 1 + p.Length(); + size_t raw_size = DaybreakHeader::size() + (size_t)m_crc_bytes + m_buffered_packets_length + m_buffered_packets.size() + 1 + p.Length(); if (raw_size > m_max_packet_size) { FlushBuffer(); } @@ -1227,25 +1167,24 @@ void EQ::Net::DaybreakConnection::InternalSend(Packet &p) break; } } - + AppendCRC(out); uv_udp_send_t *send_req = new uv_udp_send_t; memset(send_req, 0, sizeof(*send_req)); - sockaddr_in send_addr; uv_ip4_addr(m_endpoint.c_str(), m_port, &send_addr); uv_buf_t send_buffers[1]; - char *data = new char[(size_t)out.Length()]; - memcpy(data, out.Data(), (size_t)out.Length()); - send_buffers[0] = uv_buf_init(data, (size_t)out.Length()); + char *data = new char[out.Length()]; + memcpy(data, out.Data(), out.Length()); + send_buffers[0] = uv_buf_init(data, out.Length()); send_req->data = send_buffers[0].base; m_stats.sent_bytes += out.Length(); m_stats.sent_packets++; if (m_owner->m_options.simulated_out_packet_loss && m_owner->m_options.simulated_out_packet_loss >= m_owner->m_rand.Int(0, 100)) { - delete[] (char*)send_req->data; + delete[](char*)send_req->data; delete send_req; return; } @@ -1255,24 +1194,20 @@ void EQ::Net::DaybreakConnection::InternalSend(Packet &p) } uv_udp_send_t *send_req = new uv_udp_send_t; - memset(send_req, 0, sizeof(*send_req)); - sockaddr_in send_addr; uv_ip4_addr(m_endpoint.c_str(), m_port, &send_addr); uv_buf_t send_buffers[1]; - - char *data = new char[(size_t)p.Length()]; - memcpy(data, p.Data(), (size_t)p.Length()); - send_buffers[0] = uv_buf_init(data, (size_t)p.Length()); + + char *data = new char[p.Length()]; + memcpy(data, p.Data(), p.Length()); + send_buffers[0] = uv_buf_init(data, p.Length()); send_req->data = send_buffers[0].base; - + m_stats.sent_bytes += p.Length(); m_stats.sent_packets++; - Log.OutF(Logs::Detail, Logs::Netcode, "Sending reliable packet\n{0}", p.ToString()); - if (m_owner->m_options.simulated_out_packet_loss && m_owner->m_options.simulated_out_packet_loss >= m_owner->m_rand.Int(0, 100)) { - delete[] (char*)send_req->data; + delete[](char*)send_req->data; delete send_req; return; } @@ -1283,24 +1218,18 @@ void EQ::Net::DaybreakConnection::InternalSend(Packet &p) void EQ::Net::DaybreakConnection::InternalQueuePacket(Packet &p, int stream_id, bool reliable) { if (!reliable) { - Log.OutF(Logs::Detail, Logs::Netcode, "Queue non-reliable packet for stream {0}\n{1}", p.ToString()); - auto max_raw_size = m_max_packet_size - m_crc_bytes; if (p.Length() > max_raw_size) { InternalQueuePacket(p, stream_id, true); return; } - Log.OutF(Logs::Detail, Logs::Netcode, "Non-reliable packet was too large, queuing as a reliable"); InternalBufferedSend(p); - return; } - Log.OutF(Logs::Detail, Logs::Netcode, "Queue reliable packet for stream {0}\n{1}", stream_id, p.ToString()); - auto stream = &m_streams[stream_id]; auto max_raw_size = m_max_packet_size - m_crc_bytes - DaybreakReliableHeader::size(); - uint64_t length = p.Length(); + size_t length = p.Length(); if (length > max_raw_size) { DaybreakReliableFragmentHeader first_header; first_header.reliable.zero = 0; @@ -1308,10 +1237,8 @@ void EQ::Net::DaybreakConnection::InternalQueuePacket(Packet &p, int stream_id, first_header.reliable.sequence = HostToNetwork(stream->sequence_out); first_header.total_size = (uint32_t)HostToNetwork((uint32_t)length); - Log.OutF(Logs::Detail, Logs::Netcode, "Packet too large, fragmenting {0} total size", first_header.total_size); - - uint64_t used = 0; - uint64_t sublen = m_max_packet_size - m_crc_bytes - DaybreakReliableFragmentHeader::size(); + size_t used = 0; + size_t sublen = m_max_packet_size - m_crc_bytes - DaybreakReliableFragmentHeader::size(); DynamicPacket first_packet; first_packet.PutSerialize(0, first_header); first_packet.PutData(DaybreakReliableFragmentHeader::size(), (char*)p.Data() + used, sublen); @@ -1325,8 +1252,6 @@ void EQ::Net::DaybreakConnection::InternalQueuePacket(Packet &p, int stream_id, stream->sent_packets.insert(std::make_pair(stream->sequence_out, sent)); stream->sequence_out++; - Log.OutF(Logs::Detail, Logs::Netcode, "Fragmented packet {0}/{1}", used, first_header.total_size); - InternalBufferedSend(first_packet); while (used < length) { @@ -1355,8 +1280,6 @@ void EQ::Net::DaybreakConnection::InternalQueuePacket(Packet &p, int stream_id, stream->sent_packets.insert(std::make_pair(stream->sequence_out, sent)); stream->sequence_out++; - Log.OutF(Logs::Detail, Logs::Netcode, "Fragmented packet {0}/{1}", used, first_header.total_size); - InternalBufferedSend(packet); } } @@ -1377,8 +1300,6 @@ void EQ::Net::DaybreakConnection::InternalQueuePacket(Packet &p, int stream_id, stream->sent_packets.insert(std::make_pair(stream->sequence_out, sent)); stream->sequence_out++; - Log.OutF(Logs::Detail, Logs::Netcode, "Reliable packet was able to be sent without fragmenting"); - InternalBufferedSend(packet); } } @@ -1390,10 +1311,8 @@ void EQ::Net::DaybreakConnection::FlushBuffer() } if (m_buffered_packets.size() > 1) { - Log.OutF(Logs::Detail, Logs::Netcode, "Flushing buffer of size {0} packets and {1} bytes", m_buffered_packets.size(), m_buffered_packets_length); - StaticPacket out(m_combined.get(), 512); - uint64_t length = 2; + size_t length = 2; for (auto &p : m_buffered_packets) { out.PutUInt8(length, (uint8_t)p.Length()); out.PutPacket(length + 1, p); @@ -1404,8 +1323,6 @@ void EQ::Net::DaybreakConnection::FlushBuffer() InternalSend(out); } else { - Log.OutF(Logs::Detail, Logs::Netcode, "Flushing buffer of size 1 packet and {0} bytes", m_buffered_packets_length); - auto &front = m_buffered_packets.front(); InternalSend(front); } @@ -1417,18 +1334,18 @@ void EQ::Net::DaybreakConnection::FlushBuffer() EQ::Net::SequenceOrder EQ::Net::DaybreakConnection::CompareSequence(uint16_t expected, uint16_t actual) const { int diff = (int)actual - (int)expected; - + if (diff == 0) { return SequenceCurrent; } - + if (diff > 0) { return SequenceFuture; } - + if (diff < -10000) { return SequenceFuture; } - + return SequencePast; -} +} \ No newline at end of file diff --git a/common/net/daybreak_connection.h b/common/net/daybreak_connection.h index d38ced5c7..8f0d9410d 100644 --- a/common/net/daybreak_connection.h +++ b/common/net/daybreak_connection.h @@ -13,7 +13,7 @@ namespace EQ { - namespace Net + namespace Net { enum DaybreakProtocolOpcode { @@ -124,7 +124,7 @@ namespace EQ void QueuePacket(Packet &p, int stream, bool reliable); const DaybreakConnectionStats& GetStats() const { return m_stats; } void ResetStats(); - uint64_t GetRollingPing() const { return m_rolling_ping; } + size_t GetRollingPing() const { return m_rolling_ping; } DbProtocolStatus GetStatus() { return m_status; } private: DaybreakConnectionManager *m_owner; @@ -141,20 +141,20 @@ namespace EQ DbProtocolStatus m_status; Timestamp m_hold_time; std::list m_buffered_packets; - uint64_t m_buffered_packets_length; + size_t m_buffered_packets_length; std::unique_ptr m_combined; Timestamp m_last_stats; DaybreakConnectionStats m_stats; Timestamp m_last_session_stats; - uint64_t m_resend_delay; - uint64_t m_rolling_ping; + size_t m_resend_delay; + size_t m_rolling_ping; struct DaybreakSentPacket { DynamicPacket packet; Timestamp last_sent; Timestamp first_sent; - uint64_t times_resent; + size_t times_resent; }; struct DaybreakStream @@ -169,7 +169,7 @@ namespace EQ uint16_t sequence_in; uint16_t sequence_out; std::map packet_queue; - + DynamicPacket fragment_packet; uint32_t fragment_current_bytes; uint32_t fragment_total_bytes; @@ -179,7 +179,7 @@ namespace EQ DaybreakStream m_streams[4]; std::weak_ptr m_self; - + void Process(); void ProcessPacket(Packet &p); void ProcessQueue(); @@ -190,10 +190,10 @@ namespace EQ bool ValidateCRC(Packet &p); void AppendCRC(Packet &p); bool PacketCanBeEncoded(Packet &p) const; - void Decode(Packet &p, uint64_t offset, uint64_t length); - void Encode(Packet &p, uint64_t offset, uint64_t length); - void Decompress(Packet &p, uint64_t offset, uint64_t length); - void Compress(Packet &p, uint64_t offset, uint64_t length); + void Decode(Packet &p, size_t offset, size_t length); + void Encode(Packet &p, size_t offset, size_t length); + void Decompress(Packet &p, size_t offset, size_t length); + void Compress(Packet &p, size_t offset, size_t length); void ProcessResend(); void ProcessResend(int stream); void Ack(int stream, uint16_t seq); @@ -236,20 +236,20 @@ namespace EQ tic_rate_hertz = 20.0; } - uint64_t max_packet_size; - uint64_t max_connection_count; - uint64_t keepalive_delay_ms; + size_t max_packet_size; + size_t max_connection_count; + size_t keepalive_delay_ms; double resend_delay_factor; - uint64_t resend_delay_ms; - uint64_t stats_delay_ms; - uint64_t connect_delay_ms; - uint64_t connect_stale_ms; - uint64_t stale_connection_ms; - uint64_t crc_length; - uint64_t hold_size; - uint64_t hold_length_ms; - uint64_t simulated_in_packet_loss; - uint64_t simulated_out_packet_loss; + size_t resend_delay_ms; + size_t stats_delay_ms; + size_t connect_delay_ms; + size_t connect_stale_ms; + size_t stale_connection_ms; + size_t crc_length; + size_t hold_size; + size_t hold_length_ms; + size_t simulated_in_packet_loss; + size_t simulated_out_packet_loss; double tic_rate_hertz; DaybreakEncodeType encode_passes[2]; int port; @@ -282,11 +282,11 @@ namespace EQ std::function, const Packet&)> m_on_packet_recv; std::map, std::shared_ptr> m_connections; - void ProcessPacket(const std::string &endpoint, int port, const char *data, uint64_t size); + void ProcessPacket(const std::string &endpoint, int port, const char *data, size_t size); std::shared_ptr FindConnectionByEndpoint(std::string addr, int port); void SendDisconnect(const std::string &addr, int port); friend class DaybreakConnection; }; } -} +} \ No newline at end of file diff --git a/common/net/eqstream.cpp b/common/net/eqstream.cpp index cc7816518..31c8f43ac 100644 --- a/common/net/eqstream.cpp +++ b/common/net/eqstream.cpp @@ -119,16 +119,7 @@ EQApplicationPacket *EQ::Net::EQStream::PopPacket() { } EmuOpcode emu_op = (*m_opcode_manager)->EQToEmu(opcode); - auto sz = p->Length() - m_owner->m_options.opcode_size; - EQApplicationPacket *ret = nullptr; - - if (sz > 0) { - ret = new EQApplicationPacket(emu_op, (unsigned char*)p->Data() + m_owner->m_options.opcode_size, sz); - } - else { - ret = new EQApplicationPacket(emu_op); - } - + EQApplicationPacket *ret = new EQApplicationPacket(emu_op, (unsigned char*)p->Data() + m_owner->m_options.opcode_size, p->Length() - m_owner->m_options.opcode_size); ret->SetProtocolOpcode(opcode); m_packet_queue.pop_front(); return ret; @@ -189,17 +180,17 @@ EQStreamInterface::MatchState EQ::Net::EQStream::CheckSignature(const Signature if (opcode == sig->first_eq_opcode) { if (length == sig->first_length) { - Log.OutF(Logs::General, Logs::Netcode, "[IDENT_TRACE] {0}:{1}: First opcode matched {2:#x} and length matched {3}", + Log.OutF(Logs::General, Logs::Netcode, "[IDENT_TRACE] {0}:{1}: First opcode matched {2:#x} and length matched {3}", RemoteEndpoint(), m_connection->RemotePort(), sig->first_eq_opcode, length); return MatchSuccessful; } - else if(length == 0) { - Log.OutF(Logs::General, Logs::Netcode, "[IDENT_TRACE] {0}:{1}: First opcode matched {2:#x} and length is ignored.", + else if (length == 0) { + Log.OutF(Logs::General, Logs::Netcode, "[IDENT_TRACE] {0}:{1}: First opcode matched {2:#x} and length is ignored.", RemoteEndpoint(), m_connection->RemotePort(), sig->first_eq_opcode); return MatchSuccessful; } else { - Log.OutF(Logs::General, Logs::Netcode, "[IDENT_TRACE] {0}:{1}: First opcode matched {2:#x} but length {3} did not match expected {4}", + Log.OutF(Logs::General, Logs::Netcode, "[IDENT_TRACE] {0}:{1}: First opcode matched {2:#x} but length {3} did not match expected {4}", RemoteEndpoint(), m_connection->RemotePort(), sig->first_eq_opcode, length, sig->first_length); return MatchFailed; } @@ -226,4 +217,4 @@ EQStreamState EQ::Net::EQStream::GetState() { default: return CLOSED; } -} +} \ No newline at end of file diff --git a/common/net/eqstream.h b/common/net/eqstream.h index ff27bb0b9..526b62fa4 100644 --- a/common/net/eqstream.h +++ b/common/net/eqstream.h @@ -99,4 +99,4 @@ namespace EQ friend class EQStreamManager; }; } -} +} \ No newline at end of file diff --git a/common/net/packet.cpp b/common/net/packet.cpp index 67d674838..347774459 100644 --- a/common/net/packet.cpp +++ b/common/net/packet.cpp @@ -3,7 +3,7 @@ #include #include -void EQ::Net::Packet::PutInt8(uint64_t offset, int8_t value) +void EQ::Net::Packet::PutInt8(size_t offset, int8_t value) { if (Length() < offset + 1) { if (!Resize(offset + 1)) { @@ -14,7 +14,7 @@ void EQ::Net::Packet::PutInt8(uint64_t offset, int8_t value) *(int8_t*)((char*)Data() + offset) = value; } -void EQ::Net::Packet::PutInt16(uint64_t offset, int16_t value) +void EQ::Net::Packet::PutInt16(size_t offset, int16_t value) { if (Length() < offset + 2) { if (!Resize(offset + 2)) { @@ -25,7 +25,7 @@ void EQ::Net::Packet::PutInt16(uint64_t offset, int16_t value) *(int16_t*)((char*)Data() + offset) = value; } -void EQ::Net::Packet::PutInt32(uint64_t offset, int32_t value) +void EQ::Net::Packet::PutInt32(size_t offset, int32_t value) { if (Length() < offset + 4) { if (!Resize(offset + 4)) { @@ -36,7 +36,7 @@ void EQ::Net::Packet::PutInt32(uint64_t offset, int32_t value) *(int32_t*)((char*)Data() + offset) = value; } -void EQ::Net::Packet::PutInt64(uint64_t offset, int64_t value) +void EQ::Net::Packet::PutInt64(size_t offset, int64_t value) { if (Length() < offset + 8) { if (!Resize(offset + 8)) { @@ -47,7 +47,7 @@ void EQ::Net::Packet::PutInt64(uint64_t offset, int64_t value) *(int64_t*)((char*)Data() + offset) = value; } -void EQ::Net::Packet::PutUInt8(uint64_t offset, uint8_t value) +void EQ::Net::Packet::PutUInt8(size_t offset, uint8_t value) { if (Length() < offset + 1) { if (!Resize(offset + 1)) { @@ -58,7 +58,7 @@ void EQ::Net::Packet::PutUInt8(uint64_t offset, uint8_t value) *(uint8_t*)((char*)Data() + offset) = value; } -void EQ::Net::Packet::PutUInt16(uint64_t offset, uint16_t value) +void EQ::Net::Packet::PutUInt16(size_t offset, uint16_t value) { if (Length() < offset + 2) { if (!Resize(offset + 2)) { @@ -69,7 +69,7 @@ void EQ::Net::Packet::PutUInt16(uint64_t offset, uint16_t value) *(uint16_t*)((char*)Data() + offset) = value; } -void EQ::Net::Packet::PutUInt32(uint64_t offset, uint32_t value) +void EQ::Net::Packet::PutUInt32(size_t offset, uint32_t value) { if (Length() < offset + 4) { if (!Resize(offset + 4)) { @@ -80,7 +80,7 @@ void EQ::Net::Packet::PutUInt32(uint64_t offset, uint32_t value) *(uint32_t*)((char*)Data() + offset) = value; } -void EQ::Net::Packet::PutUInt64(uint64_t offset, uint64_t value) +void EQ::Net::Packet::PutUInt64(size_t offset, uint64_t value) { if (Length() < offset + 8) { if (!Resize(offset + 8)) { @@ -91,7 +91,7 @@ void EQ::Net::Packet::PutUInt64(uint64_t offset, uint64_t value) *(uint64_t*)((char*)Data() + offset) = value; } -void EQ::Net::Packet::PutFloat(uint64_t offset, float value) +void EQ::Net::Packet::PutFloat(size_t offset, float value) { if (Length() < offset + 4) { if (!Resize(offset + 4)) { @@ -102,7 +102,7 @@ void EQ::Net::Packet::PutFloat(uint64_t offset, float value) *(float*)((char*)Data() + offset) = value; } -void EQ::Net::Packet::PutDouble(uint64_t offset, double value) +void EQ::Net::Packet::PutDouble(size_t offset, double value) { if (Length() < offset + 8) { if (!Resize(offset + 8)) { @@ -113,7 +113,7 @@ void EQ::Net::Packet::PutDouble(uint64_t offset, double value) *(double*)((char*)Data() + offset) = value; } -void EQ::Net::Packet::PutString(uint64_t offset, const std::string &str) +void EQ::Net::Packet::PutString(size_t offset, const std::string &str) { if (Length() < offset + str.length()) { if (!Resize(offset + str.length())) { @@ -124,20 +124,20 @@ void EQ::Net::Packet::PutString(uint64_t offset, const std::string &str) memcpy(((char*)Data() + offset), str.c_str(), str.length()); } -void EQ::Net::Packet::PutCString(uint64_t offset, const char *str) +void EQ::Net::Packet::PutCString(size_t offset, const char *str) { - uint64_t sz = strlen(str); + size_t sz = strlen(str); if (Length() < offset + sz + 1) { if (!Resize(offset + sz + 1)) { throw std::out_of_range("Packet::PutCString(), could not resize packet and would of written past the end."); } } - memcpy(((char*)Data() + offset), str, (size_t)sz); + memcpy(((char*)Data() + offset), str, sz); *((char*)Data() + offset + sz) = 0; } -void EQ::Net::Packet::PutPacket(uint64_t offset, const Packet &p) +void EQ::Net::Packet::PutPacket(size_t offset, const Packet &p) { if (Length() < offset + p.Length()) { if (!Resize(offset + p.Length())) { @@ -145,10 +145,10 @@ void EQ::Net::Packet::PutPacket(uint64_t offset, const Packet &p) } } - memcpy(((char*)Data() + offset), p.Data(), (size_t)p.Length()); + memcpy(((char*)Data() + offset), p.Data(), p.Length()); } -void EQ::Net::Packet::PutData(uint64_t offset, void *data, uint64_t length) +void EQ::Net::Packet::PutData(size_t offset, void *data, size_t length) { if (Length() < offset + length) { if (!Resize(offset + length)) { @@ -156,10 +156,10 @@ void EQ::Net::Packet::PutData(uint64_t offset, void *data, uint64_t length) } } - memcpy(((char*)Data() + offset), data, (size_t)length); + memcpy(((char*)Data() + offset), data, length); } -int8_t EQ::Net::Packet::GetInt8(uint64_t offset) const +int8_t EQ::Net::Packet::GetInt8(size_t offset) const { if (Length() < offset + 1) { throw std::out_of_range("Packet read out of range."); @@ -168,7 +168,7 @@ int8_t EQ::Net::Packet::GetInt8(uint64_t offset) const return *(int8_t*)((char*)Data() + offset); } -int16_t EQ::Net::Packet::GetInt16(uint64_t offset) const +int16_t EQ::Net::Packet::GetInt16(size_t offset) const { if (Length() < offset + 2) { throw std::out_of_range("Packet read out of range."); @@ -177,7 +177,7 @@ int16_t EQ::Net::Packet::GetInt16(uint64_t offset) const return *(int16_t*)((char*)Data() + offset); } -int32_t EQ::Net::Packet::GetInt32(uint64_t offset) const +int32_t EQ::Net::Packet::GetInt32(size_t offset) const { if (Length() < offset + 4) { throw std::out_of_range("Packet read out of range."); @@ -186,7 +186,7 @@ int32_t EQ::Net::Packet::GetInt32(uint64_t offset) const return *(int32_t*)((char*)Data() + offset); } -int64_t EQ::Net::Packet::GetInt64(uint64_t offset) const +int64_t EQ::Net::Packet::GetInt64(size_t offset) const { if (Length() < offset + 8) { throw std::out_of_range("Packet read out of range."); @@ -195,7 +195,7 @@ int64_t EQ::Net::Packet::GetInt64(uint64_t offset) const return *(int64_t*)((char*)Data() + offset); } -uint8_t EQ::Net::Packet::GetUInt8(uint64_t offset) const +uint8_t EQ::Net::Packet::GetUInt8(size_t offset) const { if (Length() < offset + 1) { throw std::out_of_range("Packet read out of range."); @@ -204,7 +204,7 @@ uint8_t EQ::Net::Packet::GetUInt8(uint64_t offset) const return *(uint8_t*)((char*)Data() + offset); } -uint16_t EQ::Net::Packet::GetUInt16(uint64_t offset) const +uint16_t EQ::Net::Packet::GetUInt16(size_t offset) const { if (Length() < offset + 2) { throw std::out_of_range("Packet read out of range."); @@ -213,7 +213,7 @@ uint16_t EQ::Net::Packet::GetUInt16(uint64_t offset) const return *(uint16_t*)((char*)Data() + offset); } -uint32_t EQ::Net::Packet::GetUInt32(uint64_t offset) const +uint32_t EQ::Net::Packet::GetUInt32(size_t offset) const { if (Length() < offset + 4) { throw std::out_of_range("Packet read out of range."); @@ -222,7 +222,7 @@ uint32_t EQ::Net::Packet::GetUInt32(uint64_t offset) const return *(uint32_t*)((char*)Data() + offset); } -uint64_t EQ::Net::Packet::GetUInt64(uint64_t offset) const +uint64_t EQ::Net::Packet::GetUInt64(size_t offset) const { if (Length() < offset + 8) { throw std::out_of_range("Packet read out of range."); @@ -231,7 +231,7 @@ uint64_t EQ::Net::Packet::GetUInt64(uint64_t offset) const return *(uint64_t*)((char*)Data() + offset); } -float EQ::Net::Packet::GetFloat(uint64_t offset) const +float EQ::Net::Packet::GetFloat(size_t offset) const { if (Length() < offset + 4) { throw std::out_of_range("Packet read out of range."); @@ -240,7 +240,7 @@ float EQ::Net::Packet::GetFloat(uint64_t offset) const return *(float*)((char*)Data() + offset); } -double EQ::Net::Packet::GetDouble(uint64_t offset) const +double EQ::Net::Packet::GetDouble(size_t offset) const { if (Length() < offset + 8) { throw std::out_of_range("Packet read out of range."); @@ -249,7 +249,7 @@ double EQ::Net::Packet::GetDouble(uint64_t offset) const return *(double*)((char*)Data() + offset); } -std::string EQ::Net::Packet::GetString(uint64_t offset, uint64_t length) const +std::string EQ::Net::Packet::GetString(size_t offset, size_t length) const { if (Length() < offset + length) { throw std::out_of_range("Packet read out of range."); @@ -258,7 +258,7 @@ std::string EQ::Net::Packet::GetString(uint64_t offset, uint64_t length) const return std::string((char*)Data() + offset, (char*)Data() + offset + length); } -std::string EQ::Net::Packet::GetCString(uint64_t offset) const +std::string EQ::Net::Packet::GetCString(size_t offset) const { if (Length() < offset + 1) { throw std::out_of_range("Packet read out of range."); @@ -281,15 +281,15 @@ std::string EQ::Net::Packet::ToString() const return ToString(16); } -std::string EQ::Net::Packet::ToString(uint64_t line_length) const +std::string EQ::Net::Packet::ToString(size_t line_length) const { if (Length() == 0) { return fmt::format("{:0>5x} |", 0); } std::string ret; - uint64_t lines = Length() / line_length; - uint64_t i; + size_t lines = Length() / line_length; + size_t i; char *data = (char*)Data(); @@ -297,7 +297,7 @@ std::string EQ::Net::Packet::ToString(uint64_t line_length) const ret += fmt::format("{:0>5x} |", i * line_length); std::string hex; std::string ascii; - for (uint64_t j = 0; j < line_length; ++j) { + for (size_t j = 0; j < line_length; ++j) { hex += fmt::format(" {:0>2x}", (uint8_t)data[(i * line_length) + j]); ascii += fmt::format("{}", ToSafePrint(data[(i * line_length) + j])); } @@ -311,17 +311,17 @@ std::string EQ::Net::Packet::ToString(uint64_t line_length) const if (Length() % line_length > 0) { ret += fmt::format("{:0>5x} |", i * line_length); - uint64_t non_blank_count = Length() % line_length; - uint64_t blank_count = line_length - non_blank_count; + size_t non_blank_count = Length() % line_length; + size_t blank_count = line_length - non_blank_count; std::string hex; std::string ascii; - for (uint64_t j = 0; j < non_blank_count; ++j) { + for (size_t j = 0; j < non_blank_count; ++j) { hex += fmt::format(" {:0>2x}", (uint8_t)data[(i * line_length) + j]); ascii += fmt::format("{}", ToSafePrint(data[(i * line_length) + j])); } - for (uint64_t j = 0; j < blank_count; ++j) { + for (size_t j = 0; j < blank_count; ++j) { hex += " "; ascii += " "; } @@ -335,12 +335,11 @@ std::string EQ::Net::Packet::ToString(uint64_t line_length) const return ret; } -bool EQ::Net::StaticPacket::Resize(uint64_t new_size) +bool EQ::Net::StaticPacket::Resize(size_t new_size) { if (new_size > m_max_data_length) { return false; } m_data_length = new_size; - return true; -} +} \ No newline at end of file diff --git a/common/net/packet.h b/common/net/packet.h index 95bd93d9d..1ce173a10 100644 --- a/common/net/packet.h +++ b/common/net/packet.h @@ -18,68 +18,68 @@ namespace EQ { virtual const void *Data() const = 0; virtual void *Data() = 0; - virtual uint64_t Length() const = 0; - virtual uint64_t Length() = 0; + virtual size_t Length() const = 0; + virtual size_t Length() = 0; virtual bool Clear() = 0; - virtual bool Resize(uint64_t new_size) = 0; - virtual void Reserve(uint64_t new_size) = 0; + virtual bool Resize(size_t new_size) = 0; + virtual void Reserve(size_t new_size) = 0; template - T GetSerialize(uint64_t offset) const - { + T GetSerialize(size_t offset) const + { T ret; - Util::MemoryStreamReader reader(((char*)Data() + offset), (size_t)Length()); + Util::MemoryStreamReader reader(((char*)Data() + offset), Length()); cereal::BinaryInputArchive input(reader); input(ret); return ret; } template - void PutSerialize(uint64_t offset, const T &value) { + void PutSerialize(size_t offset, const T &value) { m_stream.clear(); cereal::BinaryOutputArchive output(m_stream); output(value); - + auto str = m_stream.str(); if (Length() < offset + str.length()) { if (!Resize(offset + str.length())) { throw std::out_of_range("Packet::PutSerialize(), could not resize packet and would of written past the end."); } } - + memcpy((char*)Data() + offset, &str[0], str.length()); } - void PutInt8(uint64_t offset, int8_t value); - void PutInt16(uint64_t offset, int16_t value); - void PutInt32(uint64_t offset, int32_t value); - void PutInt64(uint64_t offset, int64_t value); - void PutUInt8(uint64_t offset, uint8_t value); - void PutUInt16(uint64_t offset, uint16_t value); - void PutUInt32(uint64_t offset, uint32_t value); - void PutUInt64(uint64_t offset, uint64_t value); - void PutFloat(uint64_t offset, float value); - void PutDouble(uint64_t offset, double value); - void PutString(uint64_t offset, const std::string &str); - void PutCString(uint64_t offset, const char *str); - void PutPacket(uint64_t offset, const Packet &p); - void PutData(uint64_t offset, void *data, uint64_t length); - - int8_t GetInt8(uint64_t offset) const; - int16_t GetInt16(uint64_t offset) const; - int32_t GetInt32(uint64_t offset) const; - int64_t GetInt64(uint64_t offset) const; - uint8_t GetUInt8(uint64_t offset) const; - uint16_t GetUInt16(uint64_t offset) const; - uint32_t GetUInt32(uint64_t offset) const; - uint64_t GetUInt64(uint64_t offset) const; - float GetFloat(uint64_t offset) const; - double GetDouble(uint64_t offset) const; - std::string GetString(uint64_t offset, uint64_t length) const; - std::string GetCString(uint64_t offset) const; - + void PutInt8(size_t offset, int8_t value); + void PutInt16(size_t offset, int16_t value); + void PutInt32(size_t offset, int32_t value); + void PutInt64(size_t offset, int64_t value); + void PutUInt8(size_t offset, uint8_t value); + void PutUInt16(size_t offset, uint16_t value); + void PutUInt32(size_t offset, uint32_t value); + void PutUInt64(size_t offset, uint64_t value); + void PutFloat(size_t offset, float value); + void PutDouble(size_t offset, double value); + void PutString(size_t offset, const std::string &str); + void PutCString(size_t offset, const char *str); + void PutPacket(size_t offset, const Packet &p); + void PutData(size_t offset, void *data, size_t length); + + int8_t GetInt8(size_t offset) const; + int16_t GetInt16(size_t offset) const; + int32_t GetInt32(size_t offset) const; + int64_t GetInt64(size_t offset) const; + uint8_t GetUInt8(size_t offset) const; + uint16_t GetUInt16(size_t offset) const; + uint32_t GetUInt32(size_t offset) const; + uint64_t GetUInt64(size_t offset) const; + float GetFloat(size_t offset) const; + double GetDouble(size_t offset) const; + std::string GetString(size_t offset, size_t length) const; + std::string GetCString(size_t offset) const; + std::string ToString() const; - std::string ToString(uint64_t line_length) const; + std::string ToString(size_t line_length) const; protected: std::stringstream m_stream; }; @@ -87,7 +87,7 @@ namespace EQ { class StaticPacket : public Packet { public: - StaticPacket(void *data, uint64_t size) { m_data = data; m_data_length = size; m_max_data_length = size; } + StaticPacket(void *data, size_t size) { m_data = data; m_data_length = size; m_max_data_length = size; } virtual ~StaticPacket() { } StaticPacket(const StaticPacket &o) { m_data = o.m_data; m_data_length = o.m_data_length; } StaticPacket& operator=(const StaticPacket &o) { m_data = o.m_data; m_data_length = o.m_data_length; return *this; } @@ -95,16 +95,16 @@ namespace EQ { virtual const void *Data() const { return m_data; } virtual void *Data() { return m_data; } - virtual uint64_t Length() const { return m_data_length; } - virtual uint64_t Length() { return m_data_length; } + virtual size_t Length() const { return m_data_length; } + virtual size_t Length() { return m_data_length; } virtual bool Clear() { return false; } - virtual bool Resize(uint64_t new_size); - virtual void Reserve(uint64_t new_size) { } + virtual bool Resize(size_t new_size); + virtual void Reserve(size_t new_size) { } protected: void *m_data; - uint64_t m_data_length; - uint64_t m_max_data_length; + size_t m_data_length; + size_t m_max_data_length; }; class DynamicPacket : public Packet @@ -118,13 +118,13 @@ namespace EQ { virtual const void *Data() const { return &m_data[0]; } virtual void *Data() { return &m_data[0]; } - virtual uint64_t Length() const { return m_data.size(); } - virtual uint64_t Length() { return m_data.size(); } + virtual size_t Length() const { return m_data.size(); } + virtual size_t Length() { return m_data.size(); } virtual bool Clear() { m_data.clear(); return true; } - virtual bool Resize(uint64_t new_size) { m_data.resize((size_t)new_size, 0); return true; } - virtual void Reserve(uint64_t new_size) { m_data.reserve((size_t)new_size); } + virtual bool Resize(size_t new_size) { m_data.resize(new_size, 0); return true; } + virtual void Reserve(size_t new_size) { m_data.reserve(new_size); } protected: std::vector m_data; }; } -} +} \ No newline at end of file diff --git a/world/client.cpp b/world/client.cpp index 8feb7e381..f17e90942 100644 --- a/world/client.cpp +++ b/world/client.cpp @@ -1046,7 +1046,7 @@ bool Client::HandlePacket(const EQApplicationPacket *app) { bool Client::Process() { bool ret = true; //bool sendguilds = true; - sockaddr_in to; + sockaddr_in to = {}; memset((char *) &to, 0, sizeof(to)); to.sin_family = AF_INET; diff --git a/zone/net.cpp b/zone/net.cpp index 68c3446e7..131a052e1 100644 --- a/zone/net.cpp +++ b/zone/net.cpp @@ -451,7 +451,7 @@ int main(int argc, char** argv) { if (!eqsf_open && Config->ZonePort != 0) { Log.Out(Logs::General, Logs::Zone_Server, "Starting EQ Network server on port %d", Config->ZonePort); - EQ::Net::EQStreamManagerOptions opts(Config->ZonePort, false, false); + EQ::Net::EQStreamManagerOptions opts(Config->ZonePort, false, true); eqsm.reset(new EQ::Net::EQStreamManager(opts)); eqsf_open = true;