Ejemplo n.º 1
0
        /// <summary>Writes the specified structure.</summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item">The structure.</param>
        public void Write <T>(T item) where T : struct
        {
            if (exception != null)
            {
                var ex = exception;
                exception = null;
                throw ex;
            }
            if (!stream.IsConnected)
            {
                if (connectionResult != null)
                {
                    return;
                }
                AsyncWaitConnection();
            }
            writer.Write(nextPacketNumber);
            var packetSize = Marshal.SizeOf(item);
            var remainder  = packetSize % 8;

            if (remainder > 0)
            {
                packetSize += 8 - remainder;
            }
            writer.Write(packetSize);
            var buffer = new byte[packetSize];

            MarshalStruct.Write(item, buffer, 0);
            writer.Write(buffer);
            if (--nextPacketNumber < -0xFFFF)
            {
                nextPacketNumber = -1;
            }
            writer.Flush();
        }
Ejemplo n.º 2
0
 void OnGUI()
 {
     if (GUI.Button(new Rect(0, 0, 100, 50), "启动服务器"))
     {
         this.m_marshalServer = new AsyncMarshalServer();
     }
     if (GUI.Button(new Rect(100, 0, 100, 50), "关闭服务器"))
     {
         if (this.m_marshalServer != null)
         {
             this.m_marshalServer.ShutDown();
             this.m_marshalServer.Dispose();
             this.m_marshalServer = null;
         }
     }
     if (GUI.Button(new Rect(200, 0, 100, 50), "发送消息"))
     {
         MarshalStruct msg = new MarshalStruct();
         msg.type = 12;
         msg.data = mServerMsg;
         if (this.m_marshalServer != null)
         {
             this.m_marshalServer.PostAll(msg);
         }
     }
     mServerMsg = GUI.TextField(new Rect(300, 0, 200, 50), mServerMsg);
     if (GUI.Button(new Rect(0, 50, 100, 50), "启动一个客户端"))
     {
         AsyncMarshalClient client = new AsyncMarshalClient();
         this.m_clients.Add(client);
         this.m_clientMsgs.Add("");
     }
     for (int i = 0; i < this.m_clients.Count; i++)
     {
         GUI.Label(new Rect(0, 50 * i + 100, 100, 50), "客户端" + i);
         if (GUI.Button(new Rect(100, 50 * i + 100, 100, 50), "关闭客户端"))
         {
             this.m_clients.RemoveAt(i);
             this.m_clientMsgs.RemoveAt(i);
             return;
         }
         if (GUI.Button(new Rect(200, 50 * i + 100, 100, 50), "发送消息"))
         {
             MarshalStruct msg = new MarshalStruct();
             msg.type = 12;
             msg.data = mServerMsg;
             this.m_clients[i].Post(msg);
         }
         this.m_clientMsgs[i] = GUI.TextField(new Rect(300, 50 * i + 100, 200, 50), this.m_clientMsgs[i]);
     }
 }
Ejemplo n.º 3
0
        /// <summary>Reads the specified structure.</summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="result">The result.</param>
        /// <param name="flags">The flags.</param>
        /// <returns></returns>
        /// <exception cref="Exception">
        /// Lost connection!
        /// or
        /// Packet number mismatch, its likely we lost synchronization! You can use Resync to read subsequent data...
        /// </exception>
        /// <exception cref="ArgumentException">Packet size does not match structure size!</exception>
        public bool Read <T>(out T result, Flags flags) where T : struct
        {
            if (!stream.IsConnected)
            {
                if (0 != (flags & Flags.AllowResync))
                {
                    Connect();
                }
                else
                {
                    throw new Exception("Lost connection!");
                }
            }

            var packetNumber = reader.ReadInt32();
            int packetSize;

            result = new T();
            var valueSize = Marshal.SizeOf(result);

            if (packetNumber != nextPacketNumber)
            {
                Debug.WriteLine("Resync required at named pipe!");
                if (0 == (flags & Flags.AllowResync))
                {
                    throw new Exception("Packet number mismatch, its likely we lost synchronization! You can use Resync to read subsequent data...");
                }

                //do resync
                while (true)
                {
                    packetNumber = reader.ReadInt32();
                    if (packetNumber < -0xFFFF)
                    {
                        Debug.WriteLine("Skipping invalid data");
                        continue;
                    }

                    packetSize = reader.ReadInt32();
                    if (packetSize != valueSize)
                    {
                        Debug.WriteLine($"Skipping packet with size {packetSize} at named pipe.");
                        reader.ReadBytes(packetSize);
                    }
                }
            }
            else
            {
                packetSize = reader.ReadInt32();
                if (packetSize != valueSize)
                {
                    throw new ArgumentException("Packet size does not match structure size!");
                }
            }

            var data = reader.ReadBytes(packetSize);

            result = MarshalStruct.GetStruct <T>(data);
            if (--nextPacketNumber < -0xFFFF)
            {
                nextPacketNumber = -1;
            }
            return(true);
        }