Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / Java [игнор отключен] [закрыт для гостей] / Netty. Few questions about Echo Object Server. / 6 сообщений из 6, страница 1 из 1
30.08.2013, 15:23:31
    #38382482
eldarkaa
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Netty. Few questions about Echo Object Server.
Продолжаю пытаться сделать сетевую mini - игру по TCP/IP протоколу.
Code (nothing special, but it's working!)
ObjectEchoClient
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package Shooter2Dv30082013;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.example.echo.EchoClient;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;

/**
 * Modification of {@link EchoClient} which utilizes Java object serialization.
 */
public class ObjectEchoClient {

    private final String host;
    private final int port;
    private final int playerX;
    private final int playerY;

    public ObjectEchoClient(String host, int port, int playerX, int playerY) {
        this.host = host;
        this.port = port;
        this.playerX = playerX;
        this.playerY = playerY;
    }

    public void run() throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
             .channel(NioSocketChannel.class)
             .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(
                            new ObjectEncoder(),
                            new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
                            new ObjectEchoClientHandler(playerX, playerY));
                }
             });

            // Start the connection attempt.
            b.connect(host, port).sync().channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        // Print usage if no argument is specified.
        String[] news= new String[4];
        news[0]="127.0.0.1";
        news[1]="8080";
        news[2]="50";
        news[3]="100";
        // Parse options.
        final String host = news[0];
        final int port = Integer.parseInt(news[1]);
        final int player_X = Integer.parseInt(news[2]);
        final int player_Y = Integer.parseInt(news[3]);

        new ObjectEchoClient(host, port, player_X, player_Y).run();
    }
}


ObjectEchoClientHandler
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package Shooter2Dv30082013;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Handler implementation for the object echo client.  It initiates the
 * ping-pong traffic between the object echo client and server by sending the
 * first message to the server.
 */
public class ObjectEchoClientHandler extends ChannelInboundHandlerAdapter {

    private static final Logger logger = Logger.getLogger(
            ObjectEchoClientHandler.class.getName());

    private final int playerX;
    private final int playerY;

    /**
     * Creates a client-side handler.
     */
    public ObjectEchoClientHandler(int Player_X, int Player_Y) {
        if (Player_X < 0) {
            throw new IllegalArgumentException(
                    "Wrong coordinates X: " + Player_X);
        }
        this.playerX = Player_X;
        this.playerY = Player_Y;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // Send the first message if this handler is a client-side handler.
        ctx.write(playerX);
        ctx.writeAndFlush(playerY);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // Echo back the received object to the server.
        ctx.write(msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(
            ChannelHandlerContext ctx, Throwable cause) throws Exception {
        logger.log(
                Level.WARNING,
                "Unexpected exception from downstream.", cause);
        ctx.close();
    }
}


ObjectEchoServer
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package Shooter2Dv30082013;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.example.echo.EchoServer;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;

/**
 * Modification of {@link EchoServer} which utilizes Java object serialization.
 */
public class ObjectEchoServer {

    private final int port;

    public ObjectEchoServer(int port) {
        this.port = port;
    }

    public void run() throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class)
             .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(
                            new ObjectEncoder(),
                            new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
                            new ObjectEchoServerHandler());
                }
             });

            // Bind and start to accept incoming connections.
            b.bind(port).sync().channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        int port;
        if (args.length > 0) {
            port = Integer.parseInt(args[0]);
        } else {
            port = 8080;
        }
        new ObjectEchoServer(port).run();
    }
}


ObjectEchoServerHandler
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package Shooter2Dv30082013;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Handles both client-side and server-side handler depending on which
 * constructor was called.
 */
public class ObjectEchoServerHandler extends ChannelInboundHandlerAdapter {

    private static final Logger logger = Logger.getLogger(
            ObjectEchoServerHandler.class.getName());

    @Override
    public void channelRead(
            ChannelHandlerContext ctx, Object msg) throws Exception {
        ctx.write(msg);
        System.out.println(msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(
            ChannelHandlerContext ctx, Throwable cause) throws Exception {
        logger.log(
                Level.WARNING,
                "Unexpected exception from downstream.", cause);
        ctx.close();
    }
}



Result, 50-coordx1, 56-coordx2,100-coordy1,106-coordy2
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
50    
100
56
106
50
100
56
106
50
100
56
106
50
56
100 
106


Я собрался передавать данные с помощью сетевой библиотеки Netty. Есть парочка вопросов.
1. Как же получить несколько объектов из pipeline'a ? (сериализация понятно, как это тут реализуется в Netty, видел классы ObjectDecode, ObjectEncode, но как их использовать душе не ясно)
2. Это нормально постоянно в канал отсылать координаты игрока?
3. Я видел реализации серверов с пакетами. В пакетах(специальный класс Packet) обычно отсылают "код управления", что-то типа остановить сервер, разорвать соединение с клиетном и тд? Для этого они нужны?
4. Почему происходит такое :
50
56
100
106
...
Рейтинг: 0 / 0
30.08.2013, 22:53:07
    #38382970
eldarkaa
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Netty. Few questions about Echo Object Server.
i really need your help
...
Рейтинг: 0 / 0
31.08.2013, 21:27:56
    #38383286
eldarkaa
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Netty. Few questions about Echo Object Server.
Давайте посоветуем автору решение?) (а то придется к буржуазам идти и считать, что никто тут не может подсказать с нетти(..);
...
Рейтинг: 0 / 0
09.09.2013, 13:34:28
    #38390927
eldarkaa
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Netty. Few questions about Echo Object Server.
снова появилось время взяться за Netty, так что если появились спецы подскажите ответы на вопросы (а то придется до всего (очень) долго доходить)
...
Рейтинг: 0 / 0
09.09.2013, 14:01:39
    #38390964
eldarkaa
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Netty. Few questions about Echo Object Server.
снова появилось время взяться за Netty, так что если появились спецы подскажите ответы на вопросы (а то придется до всего (очень) долго доходить)
...
Рейтинг: 0 / 0
09.09.2013, 17:13:32
    #38391305
eldarkaa
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Netty. Few questions about Echo Object Server.
ObjectEchoClient
Код: java
1.
2.
3.
Connected.
20 20
Message is sended

ObjectEchoServer
Код: java
1.
2.
3.
4.
5.
6.
7.
Server is started
[Shooter2Dv30082013.Player@14115e3]
Shooter2Dv30082013.Player@14115e3
io.netty.channel.DefaultChannelHandlerContext@2fb4b69d
DefaultChannelPromise@7036b673(failure(io.netty.handler.codec.EncoderException: java.io.NotSerializableException: io.netty.channel.DefaultChannelHandlerContext)


В самом ObjectEchoServerHandler:
ObjectEchoServerHandler.java
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
    @Override
    public void channelRead(
            ChannelHandlerContext ctx, Object msg) throws Exception {
        Player player = (Player) msg;
        entityMap.put(ctx,player);
        System.out.println(entityMap.values());
        System.out.println(player);
        System.out.println(ctx);

        ctx.write(entityMap);
        System.out.println(ctx.write(entityMap));
    }


Player унаследован от сущности Entity, в которой хранятся координаты(переменные) (1 - х, 2 - у).
В EntitiyMap - специальный класс (в первую очередь для Сериализации), в котором хранятся сущности Entity (player,enemy).
------------------------
Проблема такая. Не хочет получать с сервера объект класса EntityMap.
...
Рейтинг: 0 / 0
Форумы / Java [игнор отключен] [закрыт для гостей] / Netty. Few questions about Echo Object Server. / 6 сообщений из 6, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]