Esempio n. 1
0
        public void SendASyncMessage(short evt, ref ASyncPackage package, bool priorMessage)
        {
            //if (_Client == null)
            //{
            //    Connect();
            //}
            //else
            //{
            //    lock (this)
            //    {
            //        if (!_Client.Connected)
            //        {
            //            Close();
            //            Connect();
            //        }
            //    }
            //}

            try
            {
                lock (this)
                {
                    object msg = package.Message;

                    TcpCacheStream tcpStream = new TcpCacheStream(_ClientStream);

                    MessageHead head = new MessageHead(evt);

                    head.Flag |= MessageFlag.ASyncMessage;

                    if (priorMessage)
                    {
                        head.Flag |= MessageFlag.Prior;
                    }

                    if (msg == null)
                    {
                        head.Flag |= MessageFlag.NullData;
                    }

                    //Send event
                    byte[] sendBuf = BitConverter.GetBytes(head.Event);
                    tcpStream.Write(sendBuf, 0, sendBuf.Length);

                    if (msg != null)
                    {
                        //Send Flag

                        if (msg is string)
                        {
                            head.Flag |= MessageFlag.IsString;
                            sendBuf    = BitConverter.GetBytes((short)head.Flag);
                            tcpStream.Write(sendBuf, 0, sendBuf.Length);

                            sendBuf = BitConverter.GetBytes(package.ClassId);
                            tcpStream.Write(sendBuf, 0, sendBuf.Length);

                            byte[] buf = Encoding.UTF8.GetBytes((msg as string));

                            for (int i = 0; i < buf.Length; i++)
                            {
                                if (buf[i] == 0)
                                {
                                    buf[i] = 0x20;
                                }
                            }

                            tcpStream.Write(buf, 0, buf.Length);
                            tcpStream.WriteByte(0);
                        }
                        else
                        {
                            sendBuf = BitConverter.GetBytes((short)head.Flag);
                            tcpStream.Write(sendBuf, 0, sendBuf.Length);
                            sendBuf = BitConverter.GetBytes(package.ClassId);
                            tcpStream.Write(sendBuf, 0, sendBuf.Length);

                            IFormatter formatter = new BinaryFormatter();
                            formatter.Serialize(tcpStream, msg);
                        }
                    }
                    else
                    {
                        //Send Flag
                        sendBuf = BitConverter.GetBytes((short)head.Flag);
                        tcpStream.Write(sendBuf, 0, sendBuf.Length);
                        sendBuf = BitConverter.GetBytes(package.ClassId);
                        tcpStream.Write(sendBuf, 0, sendBuf.Length);
                    }

                    tcpStream.Flush();
                }
            }
            catch (System.IO.IOException ioEx)
            {
                Close();
                throw ioEx;
            }
        }
Esempio n. 2
0
 public void SendASyncMessage(short evt, ref ASyncPackage package)
 {
     SendASyncMessage(evt, ref package, false);
 }