changeset 34:1236da135f79

update
author riono <e165729@ie.u-ryukyu.ac.jp>
date Tue, 27 Apr 2021 22:57:14 +0900
parents 7575980bffc9
children 9e31b8c7a5bd
files Test/Example/FizzBuzz/Counter.cs Test/Example/FizzBuzz/FizzBuzz.cs Test/RewritingTest/MessageClient.cs Test/RewritingTest/MessageListener.cs Test/RewritingTest/MessagepackTest.cs daemon/AcceptThread.cs daemon/Connection.cs daemon/IncomingTcpConnection.cs daemon/OutboundTcpConnection.cs datagear/RemoteDataGearManager.cs datagear/RemoteMessage.cs datagear/command/Command.cs datagear/command/PutCommand.cs datagear/dg/DataGear.cs datagear/dg/MessagePackDataGear.cs
diffstat 15 files changed, 203 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/Test/Example/FizzBuzz/Counter.cs	Tue Apr 20 18:42:17 2021 +0900
+++ b/Test/Example/FizzBuzz/Counter.cs	Tue Apr 27 22:57:14 2021 +0900
@@ -8,7 +8,6 @@
     
     public override void Run(CodeGearManager cgm) {
         if (num <= 100) {
-            Console.WriteLine("call");
             GetDgm("FizzBuzz").Put("num", num);
             cgm.GetLocalDGM().Put("num", num+1);
             cgm.Setup(new Counter());
--- a/Test/Example/FizzBuzz/FizzBuzz.cs	Tue Apr 20 18:42:17 2021 +0900
+++ b/Test/Example/FizzBuzz/FizzBuzz.cs	Tue Apr 27 22:57:14 2021 +0900
@@ -7,7 +7,6 @@
     [Take] private int num;
     
     public override void Run(CodeGearManager cgm) {
-        Console.WriteLine("call fizi");
         if (num % 3 == 0 && num % 5 == 0) {
             Console.WriteLine(num + ":FizzBuzz");
         }else if (num % 3 == 0) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Test/RewritingTest/MessageClient.cs	Tue Apr 27 22:57:14 2021 +0900
@@ -0,0 +1,46 @@
+using System;
+using System.Net;
+using System.Net.Sockets;
+using MessagePack;
+
+public class MessageClient {
+    public void TcpClient() {
+        IPHostEntry host = Dns.GetHostEntry("localhost");
+        IPAddress ipAddress = host.AddressList[0];
+        IPEndPoint endPoint = new IPEndPoint(ipAddress, 11000);
+
+        Socket sender = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
+
+        try {
+            sender.Connect(endPoint);
+            var message = new MessageObject();
+            message.key = "ababa";
+            message.foo = 543;
+
+            byte[] data = MessagePackSerializer.Serialize(message);
+
+            int length = sender.Send(data);
+            Console.WriteLine("lenght:" + length);
+
+            sender.Shutdown(SocketShutdown.Both);
+            sender.Close();
+        } catch (Exception e) {
+            Console.WriteLine(e);
+            
+        }
+    }
+
+    public static void Main() {
+        var ms = new MessageClient();
+        ms.TcpClient();
+    }
+}
+
+[MessagePackObject]
+public class MessageObject {
+    [Key("key")]
+    public string key;
+
+    [Key("foo")]
+    public int foo;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Test/RewritingTest/MessageListener.cs	Tue Apr 27 22:57:14 2021 +0900
@@ -0,0 +1,34 @@
+using System;
+using System.Net;
+using System.Net.Sockets;
+using MessagePack;
+
+namespace Christie_net {
+public class MessageListener {
+
+   public void TcpListiener() {
+      IPHostEntry host = Dns.GetHostEntry("localhost");
+      IPAddress ipAddress = host.AddressList[0];
+      IPEndPoint endPoint = new IPEndPoint(ipAddress, 11000);
+
+      Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
+      listener.Bind(endPoint);
+      listener.Listen(10);
+
+      Console.WriteLine("accept...");
+      Socket handler = listener.Accept();
+
+
+      byte[] data = new byte[1024];
+      int datalen = handler.Receive(data);
+      MessageObject obj = MessagePackSerializer.Deserialize<MessageObject>(data);
+
+      Console.WriteLine("receive:" + obj.foo + " len;" + datalen);
+   }
+
+   public static void Main() {
+      var server = new MessageListener();
+      server.TcpListiener();
+   }
+}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Test/RewritingTest/MessagepackTest.cs	Tue Apr 27 22:57:14 2021 +0900
@@ -0,0 +1,36 @@
+using System;
+using Christie_net.datagear.dg;
+using MessagePack;
+
+namespace Christie_net {
+[MessagePackObject()]
+public class MessagepackTest {
+
+    [Key("key")]
+    public string key;
+
+    [Key("foo")]
+    public int foo;
+
+    [Key("ms")]
+    public byte[] data;
+
+    public MessagepackTest() {
+        
+    }
+
+    public static void Main() {
+        var message = new MessagepackTest();
+        message.key = "fuga";
+        message.foo = 10;
+        message.data = MessagePackSerializer.Serialize(100);
+
+        byte[] bytes = MessagePackSerializer.Serialize(message);
+        MessagepackTest mes = MessagePackSerializer.Deserialize<MessagepackTest>(bytes);
+
+
+        int data = MessagePackSerializer.Deserialize<int>(mes.data);
+        Console.WriteLine(data);
+    }
+ }
+}
\ No newline at end of file
--- a/daemon/AcceptThread.cs	Tue Apr 20 18:42:17 2021 +0900
+++ b/daemon/AcceptThread.cs	Tue Apr 27 22:57:14 2021 +0900
@@ -16,7 +16,7 @@
     }
 
     public void Run() {
-        //while (true) {
+        while (true) {
             try {
                 TcpClient client = null;
                 client = listener.AcceptTcpClient();
@@ -28,7 +28,6 @@
                 Console.WriteLine("Accept " + hostEntry.HostName + ":" + endPoint.Port);
                 
                 Connection connection = new Connection(client.Client, cgm);
-                Console.WriteLine("connect@" + connection.GetInfoString());
                 string key = "accept" + counter;
                 
                 IncomingTcpConnection incoming = new IncomingTcpConnection(connection);
@@ -37,7 +36,7 @@
                 incomingThread.Start();
                 
                 cgm.SetAccept(key, incoming);
-
+                
                 OutboundTcpConnection outbound = new OutboundTcpConnection(connection);
                 Thread outboundThread = new Thread(outbound.Run);
                 outboundThread.Name = connection.GetInfoString() + "-OutboundTcp";
@@ -46,7 +45,7 @@
             } catch (Exception e) {
                 Console.WriteLine(e.StackTrace);
             }
-        //}
+        }
     }
 }
 }
\ No newline at end of file
--- a/daemon/Connection.cs	Tue Apr 20 18:42:17 2021 +0900
+++ b/daemon/Connection.cs	Tue Apr 27 22:57:14 2021 +0900
@@ -50,13 +50,16 @@
     /// </summary>
     /// <param name="cmd"></param>
     public void Write(Command cmd) {
+        // Debug
+        //Console.WriteLine("length:" + cmd.type);
+        
         MemoryStream stream = cmd.Convert();
         byte[] buffer = stream.ToArray();
-
+        
         try {
-            while (stream.Length > 0) {
+            //while (stream.Length > 0) {
                 socket.Send(buffer);
-            }
+            //}
         } catch (Exception e) {
             Console.WriteLine(e.StackTrace); 
         }
--- a/daemon/IncomingTcpConnection.cs	Tue Apr 20 18:42:17 2021 +0900
+++ b/daemon/IncomingTcpConnection.cs	Tue Apr 27 22:57:14 2021 +0900
@@ -6,6 +6,7 @@
 using Christie_net.datagear.command;
 using Christie_net.datagear.dg;
 using MessagePack;
+using Microsoft.VisualBasic;
 using CommandType = Christie_net.datagear.command.CommandType;
 
 
@@ -25,25 +26,41 @@
     }
 
     public void Run() {
-        //TODO: Data長がわからないので4096で仮置き
-        byte[] deserializeCommand = new byte[100000];
-        
         while (true) {
             try {
+                //TODO: Data長がわからないので1024で仮置き → ぴったしで読み込む必要がある
+                byte[] deserializeCommand = new byte[1024];
                 // データはRemotemessage(Command), length, dataの順で入っている
-                connection.socket.Receive(deserializeCommand);
-                RemoteMessage msg = MessagePackSerializer.Deserialize<RemoteMessage>(deserializeCommand);
+                // 修正: データはRemotemessageに全て入っている  length,dataは無し
+                int dataLength = connection.socket.Receive(deserializeCommand);
+                
+                // Debug
+                Console.WriteLine("length: " + dataLength);
+                
+                RemoteMessage msg =
+                    MessagePackSerializer.Deserialize<RemoteMessage>(deserializeCommand);
                 CommandType type = CommandTypeExt.GetCommandTypeFormId(msg.type);
-                byte[] data;
+
+                // Debug
+                //Console.WriteLine("incoming:" + msg.type);
 
                 switch (type) {
                     case CommandType.PUT:
-                        data = new byte[MessagePackSerializer.Deserialize<int>(deserializeCommand)];
-                        connection.socket.Receive(data);
+                        //data = new byte[MessagePackSerializer.Deserialize<int>(deserializeCommand)];
+                        //connection.socket.Receive(data);
+                        byte[] data = msg.data;
 
                         try {
                             MessagePackDataGear<object> dg =
                                 new MessagePackDataGear<object>(data, Type.GetType(msg.clazz));
+                           
+                            // Debug
+                            Type t = Type.GetType(msg.clazz);
+                            object obj = MessagePackSerializer.Deserialize<object>(msg.data);
+                            Console.WriteLine("type:" + msg.type +  " key:" + msg.key + " fromDgm:" + msg.fromDmgName + " class:" + msg.clazz +
+                                              " data:" + Convert.ChangeType(obj, t));
+                            
+                            
                             cgm.GetLocalDGM().Put(msg.key, dg);
                         } catch (TypeLoadException e) {
                             Console.WriteLine(e.StackTrace);
@@ -81,7 +98,7 @@
                 }
             } catch (IOException e) {
                 Console.WriteLine(e.StackTrace);
-            } 
+            }
         }
     }
 }
--- a/daemon/OutboundTcpConnection.cs	Tue Apr 20 18:42:17 2021 +0900
+++ b/daemon/OutboundTcpConnection.cs	Tue Apr 27 22:57:14 2021 +0900
@@ -14,16 +14,19 @@
     public void Run() {
         while (true) {
             try {
-                Command cmd = connection.sendQueue.Take();
-                switch (cmd.type) {
-                    case CommandType.CLOSE:
-                    case CommandType.FINISH:
-                        cmd.Execute();
-                        return;
-                    default:
-                        break;
+                Command cmd;
+                if (connection.sendQueue.TryTake(out cmd)) {
+                    Console.WriteLine("outboud:" + cmd.type);
+                    switch (cmd.type) {
+                        case CommandType.CLOSE:
+                        case CommandType.FINISH:
+                            cmd.Execute();
+                            return;
+                        default:
+                            break;
+                    }
+                    connection.Write(cmd);
                 }
-                connection.Write(cmd);
             } catch (ThreadInterruptedException e) {
                 Console.WriteLine(e.StackTrace);
             }
--- a/datagear/RemoteDataGearManager.cs	Tue Apr 20 18:42:17 2021 +0900
+++ b/datagear/RemoteDataGearManager.cs	Tue Apr 27 22:57:14 2021 +0900
@@ -28,16 +28,20 @@
                     Socket socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                     socket.Connect(remoteEndPoint);
                     socket.NoDelay = true;
+                    
+                    connection = new Connection(socket, cgm);
+                    connection.name = dgmname;
 
-                    Socket listener = socket.Accept();
-                    connection = new Connection(listener, cgm);
-                    connection.name = dgmname;
+                    // Debug
+                    //Console.WriteLine("connect:" + connection);
 
                     lock (syncObj) {
                         connect = true;
                         Monitor.Pulse(syncObj);
                     }
-                } catch { }
+                } catch(Exception e) {
+                    Console.WriteLine(e.StackTrace);
+                }
             } while (!connect);
 
             IncomingTcpConnection incoming = new IncomingTcpConnection(connection);
@@ -70,6 +74,9 @@
         if (!connect) {
             ConnectWait();
         }
+       
+        // Debug
+        //Console.WriteLine("coneect:" + connection);
         
         connection.Write(cm);
     }
--- a/datagear/RemoteMessage.cs	Tue Apr 20 18:42:17 2021 +0900
+++ b/datagear/RemoteMessage.cs	Tue Apr 27 22:57:14 2021 +0900
@@ -12,14 +12,17 @@
     public string key;
     [Key("clazz")]
     public string clazz;
+    [Key("data")]
+    public byte[] data;
     
     public RemoteMessage(){} // for messagePack
 
-    public RemoteMessage(int type, string fromDmgName, string key, string clazz) {
+    public RemoteMessage(int type, string fromDmgName, string key, string clazz, byte[] data) {
         this.type = type;
         this.fromDmgName = fromDmgName;
         this.key = key;
         this.clazz = clazz;
+        this.data = data;
     }
 }
 }
\ No newline at end of file
--- a/datagear/command/Command.cs	Tue Apr 20 18:42:17 2021 +0900
+++ b/datagear/command/Command.cs	Tue Apr 27 22:57:14 2021 +0900
@@ -37,8 +37,8 @@
     // for remote
     public abstract MemoryStream Convert();
 
-    public RemoteMessage CreateRemoteMessage() {
-        return new RemoteMessage((int) type, fromDgmName, key, clazz.Name);
+    public RemoteMessage CreateRemoteMessage(byte[] data) {
+        return new RemoteMessage((int) type, fromDgmName, key, clazz.FullName, data);
     }
 
     public void SetDg(Object obj) {
--- a/datagear/command/PutCommand.cs	Tue Apr 20 18:42:17 2021 +0900
+++ b/datagear/command/PutCommand.cs	Tue Apr 27 22:57:14 2021 +0900
@@ -17,14 +17,24 @@
         MemoryStream stream = new MemoryStream();
 
         try {
-            byte[] command = MessagePackSerializer.Serialize(CreateRemoteMessage());
+            // byte[] command = MessagePackSerializer.Serialize(CreateRemoteMessage();
+            // byte[] data = new MessagePackDataGear<object>(dg.GetData()).GetMessagePack();
+            // byte[] dataSize = MessagePackSerializer.Serialize(data.Length);
+            //
+            // stream.Write(command);
+            // stream.Write(dataSize);
+            // stream.Write(data);
+            
             byte[] data = new MessagePackDataGear<object>(dg.GetData()).GetMessagePack();
-            byte[] dataSize = MessagePackSerializer.Serialize(data.Length);
+            byte[] command = MessagePackSerializer.Serialize(CreateRemoteMessage(data));
+
+            // Debug
+            var ms = MessagePackSerializer.Deserialize<RemoteMessage>(command);
+            //Console.WriteLine("***type:" + ms.type +  " key:" + ms.key + " fromDgm:" + ms.fromDmgName + " class:" + ms.clazz +
+                              //" data:" + MessagePackSerializer.Deserialize<object>(ms.data));
 
             stream.Write(command);
-            stream.Write(dataSize);
-            stream.Write(data);
-            
+
             stream.Position = 0;
         } catch (IOException e) {
             Console.WriteLine(e.StackTrace);
--- a/datagear/dg/DataGear.cs	Tue Apr 20 18:42:17 2021 +0900
+++ b/datagear/dg/DataGear.cs	Tue Apr 27 22:57:14 2021 +0900
@@ -51,7 +51,7 @@
         return clazz;
     }
 
-    public T GetData() {
+    public virtual T GetData() {
         return data;
     }
 
--- a/datagear/dg/MessagePackDataGear.cs	Tue Apr 20 18:42:17 2021 +0900
+++ b/datagear/dg/MessagePackDataGear.cs	Tue Apr 27 22:57:14 2021 +0900
@@ -22,6 +22,8 @@
             return messagePack;
         try {
             messagePack = MessagePackSerializer.Serialize(data);
+            // Debug
+            //Console.WriteLine("data:" + data);
             SetDataSize(messagePack.Length);
         } catch (Exception e) {
             Console.WriteLine(e.StackTrace);
@@ -30,14 +32,16 @@
         return messagePack;
     }
 
-    public T GetData() {
+    public override T GetData() {
         lock (syncObject) {
             if (data == null)
                 try {
-                    SetData(MessagePackSerializer.Deserialize<T>(messagePack));
+                    object dataObj = MessagePackSerializer.Deserialize<object>(messagePack);
+                    var dataCast = Convert.ChangeType(dataObj, clazz);
+                    SetData((T) dataCast);
                 } catch (Exception e) {
                     Console.WriteLine(e.StackTrace);
-                }
+                } 
 
             return base.GetData();
         }