changeset 39:9217d14cc220

fix NetworkStream
author riono <e165729@ie.u-ryukyu.ac.jp>
date Tue, 25 May 2021 02:35:52 +0900
parents 9f8e1087c61b
children 7276e3429c99
files Christie_net.csproj daemon/Connection.cs daemon/IncomingTcpConnection.cs datagear/RemoteMessage.cs datagear/command/CloseCommand.cs datagear/command/Command.cs datagear/command/FinishCommand.cs datagear/command/PutCommand.cs datagear/command/RemotePeedCommand.cs datagear/command/RemoteTakeCommand.cs datagear/command/TakeCommand.cs
diffstat 11 files changed, 74 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/Christie_net.csproj	Mon May 24 23:54:54 2021 +0900
+++ b/Christie_net.csproj	Tue May 25 02:35:52 2021 +0900
@@ -3,7 +3,7 @@
     <PropertyGroup>
         <OutputType>Exe</OutputType>
         <TargetFramework>netcoreapp3.1</TargetFramework>
-        <StartupObject>Christie_net.Test.Example.RemoteTake.StartRemoteTake</StartupObject>
+        <StartupObject>Christie_net.Test.Example.FizzBuzz.StartFizzBuzz</StartupObject>
     </PropertyGroup>
 
     <ItemGroup>
--- a/daemon/Connection.cs	Mon May 24 23:54:54 2021 +0900
+++ b/daemon/Connection.cs	Tue May 25 02:35:52 2021 +0900
@@ -13,11 +13,13 @@
     public CodeGearManager cgm;
     public BlockingCollection<Command> sendQueue = new BlockingCollection<Command>();
     public bool sendManager = true;
+    public NetworkStream stream;
     private object syncObject = new object();
     
     public Connection(Socket socket, CodeGearManager cgm) {
         this.socket = socket;
         this.cgm = cgm;
+        stream = new NetworkStream(this.socket);
     }
 
     public void SendCommand(Command cmd) {
@@ -53,13 +55,12 @@
         // Debug
         //Console.WriteLine("length:" + cmd.type);
         
-        MemoryStream stream = cmd.Convert();
-        byte[] buffer = stream.ToArray();
-        
+        byte[] buffer = cmd.Convert();
+
         try {
-            //while (stream.Length > 0) {
-                socket.Send(buffer);
-            //}
+            while (buffer.Length > 0) {
+                stream.Write(buffer);
+            }
         } catch (Exception e) {
             Console.WriteLine(e.StackTrace); 
         }
--- a/daemon/IncomingTcpConnection.cs	Mon May 24 23:54:54 2021 +0900
+++ b/daemon/IncomingTcpConnection.cs	Tue May 25 02:35:52 2021 +0900
@@ -1,6 +1,7 @@
 using System;
 using System.Data;
 using System.IO;
+using System.Runtime.InteropServices;
 using Christie_net.codegear;
 using Christie_net.datagear;
 using Christie_net.datagear.command;
@@ -26,30 +27,55 @@
     }
 
     public void Run() {
+        //TODO: Data長がわからないので1024で仮置き → ぴったしで読み込む必要がある
+        byte[] streamData = new byte[1024];
+        MemoryStream memoryData = null;
+        try {
+            connection.stream.Read(streamData);
+            memoryData = new MemoryStream(streamData);
+        } catch (Exception e) {
+            Console.WriteLine(e);
+        }
+
+        if (memoryData == null) {
+            return;
+        }
+        
         while (true) {
             try {
-                //TODO: Data長がわからないので1024で仮置き → ぴったしで読み込む必要がある
-                byte[] deserializeCommand = new byte[1024];
                 // データはRemotemessage(Command), length, dataの順で入っている
-                // 修正: データはRemotemessageに全て入っている  length,dataは無し
-                int dataLength = connection.socket.Receive(deserializeCommand);
+                //int dataLength = connection.socket.Receive(deserializeCommand);
+                byte[] remoteMessageData = new byte[100];
+                memoryData.Read(remoteMessageData, 0, (int) Marshal.SizeOf(typeof(RemoteMessage)));
+                RemoteMessage msg = MessagePackSerializer.Deserialize<RemoteMessage>(remoteMessageData);
+                CommandType type = CommandTypeExt.GetCommandTypeFormId(msg.type);
                 
+                Console.WriteLine("stream pos;" + memoryData.Position);
+
                 // Debug
-                Console.WriteLine("length: " + dataLength);
+                //Console.WriteLine("length: " + dataLength);
                 
-                RemoteMessage msg =
-                    MessagePackSerializer.Deserialize<RemoteMessage>(deserializeCommand);
-                CommandType type = CommandTypeExt.GetCommandTypeFormId(msg.type);
+                // old code
+                // RemoteMessage msg =
+                //     MessagePackSerializer.Deserialize<RemoteMessage>(streamData);
 
                 // Debug
                 //Console.WriteLine("incoming:" + msg.type);
-
+                byte[] data;
+                byte[] lengthData;
+                int length;
+                
                 switch (type) {
                     case CommandType.PUT:
                         //data = new byte[MessagePackSerializer.Deserialize<int>(deserializeCommand)];
                         //connection.socket.Receive(data);
-                        byte[] data = msg.data;
+                        lengthData = new byte[100];
+                        memoryData.Read(lengthData, 0, Marshal.SizeOf(typeof(int)));
+                        length = MessagePackSerializer.Deserialize<int>(lengthData);
+                        data = new byte[length];
 
+                        memoryData.Read(data, 0, length);
+                        
                         try {
                             MessagePackDataGear<object> dg =
                                 new MessagePackDataGear<object>(data, Type.GetType(msg.clazz));
@@ -57,7 +83,7 @@
                             // 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: null" );
+                            Console.WriteLine("***type:" + msg.type +  " key:" + msg.key + " fromDgm:" + msg.fromDmgName + " class:" + msg.clazz + " data: null" );
                             
                             
                             cgm.GetLocalDGM().Put(msg.key, dg);
@@ -80,8 +106,15 @@
 
                         break;
                     case CommandType.REPLY: // 待っていたwaitlistに渡してcsにセット
-                        data = new byte[MessagePackSerializer.Deserialize<int>(deserializeCommand)];
-                        connection.socket.Receive(data);
+                        lengthData = new byte[100];
+                        memoryData.Read(lengthData, 0, Marshal.SizeOf(typeof(int)));
+                        length = MessagePackSerializer.Deserialize<int>(lengthData);
+                        data = new byte[length];
+
+                        memoryData.Read(data, 0, length);
+                        
+                        // data = new byte[MessagePackSerializer.Deserialize<int>(streamData)];
+                        // connection.socket.Receive(data);
 
                         try {
                             MessagePackDataGear<object> dg =
--- a/datagear/RemoteMessage.cs	Mon May 24 23:54:54 2021 +0900
+++ b/datagear/RemoteMessage.cs	Tue May 25 02:35:52 2021 +0900
@@ -12,17 +12,14 @@
     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, byte[] data) {
+    public RemoteMessage(int type, string fromDmgName, string key, string clazz) {
         this.type = type;
         this.fromDmgName = fromDmgName;
         this.key = key;
         this.clazz = clazz;
-        this.data = data;
     }
 }
 }
\ No newline at end of file
--- a/datagear/command/CloseCommand.cs	Mon May 24 23:54:54 2021 +0900
+++ b/datagear/command/CloseCommand.cs	Tue May 25 02:35:52 2021 +0900
@@ -9,7 +9,7 @@
         connection.Close();
     }
 
-    public override MemoryStream Convert() {
+    public override byte[] Convert() {
         return null;
     }
 }
--- a/datagear/command/Command.cs	Mon May 24 23:54:54 2021 +0900
+++ b/datagear/command/Command.cs	Tue May 25 02:35:52 2021 +0900
@@ -35,10 +35,10 @@
     public abstract void Execute();
 
     // for remote
-    public abstract MemoryStream Convert();
+    public abstract byte[] Convert();
 
-    public RemoteMessage CreateRemoteMessage(byte[] data) {
-        return new RemoteMessage((int) type, fromDgmName, key, clazz.FullName, data);
+    public RemoteMessage CreateRemoteMessage() {
+        return new RemoteMessage((int) type, fromDgmName, key, clazz.FullName);
     }
 
     public void SetDg(Object obj) {
--- a/datagear/command/FinishCommand.cs	Mon May 24 23:54:54 2021 +0900
+++ b/datagear/command/FinishCommand.cs	Tue May 25 02:35:52 2021 +0900
@@ -9,7 +9,7 @@
         Environment.Exit(0);
     }
 
-    public override MemoryStream Convert() {
+    public override byte[] Convert() {
         return null;
     }
 }
--- a/datagear/command/PutCommand.cs	Mon May 24 23:54:54 2021 +0900
+++ b/datagear/command/PutCommand.cs	Tue May 25 02:35:52 2021 +0900
@@ -13,25 +13,21 @@
     }
 
     // commandとdata, dataSizeをMemoryStreamに変換する
-    public override MemoryStream Convert() {
+    public override byte[] Convert() {
         MemoryStream stream = new MemoryStream();
 
         try {
-            // 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[] command = MessagePackSerializer.Serialize(CreateRemoteMessage());
+            byte[] data = new MessagePackDataGear<object>(dg.GetData()).GetMessagePack();
+            byte[] dataSize = MessagePackSerializer.Serialize(data.Length);
             
-            byte[] data = new MessagePackDataGear<object>(dg.GetData()).GetMessagePack();
-            byte[] command = MessagePackSerializer.Serialize(CreateRemoteMessage(data));
+            stream.Write(command);
+            stream.Write(dataSize);
+            stream.Write(data);
 
             // Debug
-            var ms = MessagePackSerializer.Deserialize<RemoteMessage>(command);
-            Console.WriteLine("***type:" + ms.type +  " key:" + ms.key + " fromDgm:" + ms.fromDmgName + " class:" + ms.clazz +
-                              " data: null");
+            //var ms = MessagePackSerializer.Deserialize<RemoteMessage>(command);
+            //Console.WriteLine("***type:" + ms.type +  " key:" + ms.key + " fromDgm:" + ms.fromDmgName + " class:" + ms.clazz +" data: null");
             //MessagePackSerializer.Deserialize<object>(ms.data)
 
             stream.Write(command);
@@ -40,7 +36,7 @@
         } catch (IOException e) {
             Console.WriteLine(e.StackTrace);
         }
-        return stream;
+        return stream.ToArray();
     }
 }
 }
\ No newline at end of file
--- a/datagear/command/RemotePeedCommand.cs	Mon May 24 23:54:54 2021 +0900
+++ b/datagear/command/RemotePeedCommand.cs	Tue May 25 02:35:52 2021 +0900
@@ -7,7 +7,7 @@
     public override void Execute() {
     }
 
-    public override MemoryStream Convert() {
+    public override byte[] Convert() {
         return null;
     }
 }
--- a/datagear/command/RemoteTakeCommand.cs	Mon May 24 23:54:54 2021 +0900
+++ b/datagear/command/RemoteTakeCommand.cs	Tue May 25 02:35:52 2021 +0900
@@ -11,7 +11,7 @@
         new CommandBuilder().Init(CommandType.REPLY).Connection(connection).Key(key).Dg(dg).Build().Execute();
     }
 
-    public override MemoryStream Convert() {
+    public override byte[] Convert() {
         throw new System.NotImplementedException();
     }
 }
--- a/datagear/command/TakeCommand.cs	Mon May 24 23:54:54 2021 +0900
+++ b/datagear/command/TakeCommand.cs	Tue May 25 02:35:52 2021 +0900
@@ -9,7 +9,7 @@
         cg.GetIdg().SetInputs(key, dg);
     }
 
-    public override MemoryStream Convert() {
+    public override byte[] Convert() {
         return null;
     }
 }