use std::net::SocketAddr;
use rocketmq_rust::ArcMut;
use tracing::error;
use crate::connection::Connection;
use crate::net::channel::Channel;
use crate::protocol::remoting_command::RemotingCommand;
pub type ConnectionHandlerContext = ArcMut<ConnectionHandlerContextWrapper>;
#[derive(Hash, Eq, PartialEq)]
pub struct ConnectionHandlerContextWrapper {
pub(crate) channel: Channel,
}
impl ConnectionHandlerContextWrapper {
pub fn new(channel: Channel) -> Self {
Self { channel }
}
pub fn connection_ref(&self) -> &Connection {
self.channel.connection_ref()
}
pub fn connection_mut(&mut self) -> &mut Connection {
self.channel.connection_mut()
}
pub async fn write_response(&mut self, cmd: RemotingCommand) {
match self.channel.connection_mut().send_command(cmd).await {
Ok(_) => {}
Err(error) => {
error!("failed to send response: {}", error);
}
}
}
pub async fn write_response_ref(&mut self, cmd: &mut RemotingCommand) {
match self.channel.connection_mut().send_command_ref(cmd).await {
Ok(_) => {}
Err(error) => {
error!("failed to send response: {}", error);
}
}
}
#[deprecated(since = "0.6.0", note = "Use `write_response()` instead")]
pub async fn write(&mut self, cmd: RemotingCommand) {
self.write_response(cmd).await;
}
#[deprecated(since = "0.6.0", note = "Use `write_response_ref()` instead")]
pub async fn write_ref(&mut self, cmd: &mut RemotingCommand) {
self.write_response_ref(cmd).await;
}
pub fn channel(&self) -> &Channel {
&self.channel
}
pub fn channel_mut(&mut self) -> &mut Channel {
&mut self.channel
}
pub fn remote_address(&self) -> SocketAddr {
self.channel.remote_address()
}
}
impl AsRef<ConnectionHandlerContextWrapper> for ConnectionHandlerContextWrapper {
fn as_ref(&self) -> &ConnectionHandlerContextWrapper {
self
}
}
impl AsMut<ConnectionHandlerContextWrapper> for ConnectionHandlerContextWrapper {
fn as_mut(&mut self) -> &mut ConnectionHandlerContextWrapper {
self
}
}