view daemon/IncomingTcpConnection.cs @ 23:46cfeb0609c5

Add TcpConnections
author riono <e165729@ie.u-ryukyu.ac.jp>
date Tue, 15 Dec 2020 22:11:40 +0900
parents
children 58858657a0e0
line wrap: on
line source

using System;
using System.Data;
using System.IO;
using Christie_net.codegear;
using Christie_net.datagear;
using Christie_net.datagear.command;
using Christie_net.datagear.dg;
using MessagePack;
using CommandType = Christie_net.datagear.command.CommandType;


namespace Christie_net.daemon {
public class IncomingTcpConnection {
    private RemoteDataGearManager manager;
    private CodeGearManager cgm;
    private Connection connection;

    public IncomingTcpConnection(Connection connection) {
        this.connection = connection;
        this.cgm = connection.cgm;
    }

    public void SetManager(RemoteDataGearManager manager) {
        this.manager = manager;
    }

    public void Run() {
        byte[] deserializeData = null;
        
        
        while (true) {
            try {
                // データはType, length, dataの順で入っているっぽい
                connection.socket.Receive(deserializeData);
                RemoteMessage msg = MessagePackSerializer.Deserialize<RemoteMessage>(deserializeData);
                CommandType type = CommandTypeExt.GetCommandTypeFormId(msg.type);
                byte[] data;

                switch (type) {
                    case CommandType.PUT:
                        data = new byte[MessagePackSerializer.Deserialize<int>(deserializeData)];
                        connection.socket.Receive(data);

                        try {
                            MessagePackDataGear<object> dg =
                                new MessagePackDataGear<object>(data, Type.GetType(msg.clazz));
                        } catch (TypeLoadException e) {
                            Console.WriteLine(e.StackTrace);
                        }

                        break;
                    case CommandType.REMOTEPEEK:
                    case CommandType.REMOTETAKE:
                        try {
                            Command cm = new CommandBuilder().Init(type).FromDgmName(msg.fromDmgName)
                                .Key(msg.key)
                                .Clazz(Type.GetType(msg.clazz))
                                .Connection(connection).Build();
                            //cgm.GetLocalDGM().RunCommand(cm);
                        } catch (TypeLoadException e) {
                            Console.WriteLine(e.StackTrace);
                        }

                        break;
                    case CommandType.REPLY: // 待っていたwaitlistに渡してcsにセット
                        data = new byte[MessagePackSerializer.Deserialize<int>(deserializeData)];
                        connection.socket.Receive(data);

                        try {
                            MessagePackDataGear<object> dg =
                                new MessagePackDataGear<object>(data, Type.GetType(msg.clazz));
                            // cgm.GetDGM(msg.fromDgmName).ResolveWaitCommand(msg.key, dg);
                        } catch (TypeLoadException e) {
                            Console.WriteLine(e.StackTrace);
                        }

                        break;
                    default:
                        break;
                }
            } catch (IOException e) {
                Console.WriteLine(e.StackTrace);
            } 
        }
    }
}
}