
io.jsync.net.impl.AsyncNetHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsync.io Show documentation
Show all versions of jsync.io Show documentation
jsync.io is a non-blocking, event-driven networking framework for Java
/*
* Copyright (c) 2011-2013 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.jsync.net.impl;
import io.jsync.buffer.Buffer;
import io.jsync.impl.AsyncInternal;
import io.jsync.impl.DefaultContext;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import java.util.Map;
/**
* @author Norman Maurer
*/
public class AsyncNetHandler extends AsyncHandler {
public AsyncNetHandler(AsyncInternal async, Map connectionMap) {
super(async, connectionMap);
}
@Override
protected void channelRead(final DefaultNetSocket sock, final DefaultContext context, ChannelHandlerContext chctx, Object msg) throws Exception {
if (sock != null) {
final ByteBuf buf = (ByteBuf) msg;
Channel ch = chctx.channel();
// We need to do this since it's possible the server is being used from a worker context
if (context.isOnCorrectWorker(ch.eventLoop())) {
try {
async.setContext(context);
try {
sock.handleDataReceived(new Buffer(buf));
} catch (Throwable t) {
context.reportException(t);
}
} catch (Throwable t) {
context.reportException(t);
}
} else {
context.execute(new Runnable() {
public void run() {
try {
sock.handleDataReceived(new Buffer(buf));
} catch (Throwable t) {
context.reportException(t);
}
}
});
}
} else {
// just discard
}
}
@Override
protected Object safeObject(Object msg, ByteBufAllocator allocator) throws Exception {
if (msg instanceof ByteBuf) {
return safeBuffer((ByteBuf) msg, allocator);
}
return msg;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy