Exemple #1
0
        /// <summary>
        /// Send synchronous message
        /// </summary>
        /// <param name="evt">Event</param>
        /// <param name="msg">message</param>
        /// <param name="milliseconds">time out</param>
        /// <returns>response object. If no reponse from server, return null</returns>
        public object SendSyncMessage(short evt, object msg)
        {
            if (_ClientStream == null)
            {
                Connect();
            }

            try
            {
                lock (this)
                {
                    TcpCacheStream tcpStream = new TcpCacheStream(_ClientStream);

                    object result = null;

                    MessageHead head = new MessageHead(evt);

                    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);

                            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);
                            IFormatter formatter = new BinaryFormatter();
                            formatter.Serialize(tcpStream, msg);
                        }
                    }
                    else
                    {
                        //Send Flag
                        sendBuf = BitConverter.GetBytes((short)head.Flag);
                        tcpStream.Write(sendBuf, 0, sendBuf.Length);
                    }

                    tcpStream.Flush();

                    tcpStream = new TcpCacheStream(_ClientStream);

                    //Recevie data

                    byte[] revBuf = new byte[4];
                    int    offset = 0;

                    while (offset < 4)
                    {
                        int len = tcpStream.Read(revBuf, offset, 4 - offset);

                        if (len == 0)
                        {
                            throw new Exception("Tcp stream closed!");
                        }


                        offset += len;
                    }

                    head.Event = BitConverter.ToInt16(revBuf, 0);
                    head.Flag  = (MessageFlag)BitConverter.ToInt16(revBuf, 2);

                    if ((head.Flag & MessageFlag.NullData) == 0)
                    {
                        if ((head.Flag & MessageFlag.CustomSerialization) != 0)
                        {
                            if (RequireCustomSerialization != null)
                            {
                                Hubble.Framework.Serialization.IMySerialization mySerializer =
                                    RequireCustomSerialization(head.Event, null);

                                if (mySerializer == null)
                                {
                                    throw new Exception(string.Format("RequireCustomSerialization of Event = {0} is null!",
                                                                      head.Event));
                                }

                                result = mySerializer.Deserialize(tcpStream, mySerializer.Version);
                            }
                            else
                            {
                                throw new Exception("RequireCustomSerialization of TcpClient is null!");
                            }
                        }
                        else if ((head.Flag & MessageFlag.IsString) == 0)
                        {
                            IFormatter formatter = new BinaryFormatter();
                            result = formatter.Deserialize(tcpStream);
                        }
                        else
                        {
                            MemoryStream m = new MemoryStream();

                            byte[] buf = new byte[1024];

                            int len = 0;
                            do
                            {
                                len = tcpStream.Read(buf, 0, buf.Length);
                                if (buf[len - 1] == 0)
                                {
                                    m.Write(buf, 0, len - 1);
                                    break;
                                }
                                else
                                {
                                    m.Write(buf, 0, len);
                                }
                            } while (true);

                            m.Position = 0;

                            using (StreamReader sr = new StreamReader(m, Encoding.UTF8))
                            {
                                result = sr.ReadToEnd();
                            }

                            if ((head.Flag & MessageFlag.IsException) != 0)
                            {
                                string[] strs = Hubble.Framework.Text.Regx.Split(result as string, "innerStackTrace:");

                                result = new InnerServerException(strs[0], strs[1]);
                            }
                        }

                        if (result is InnerServerException)
                        {
                            throw new ServerException(result as InnerServerException);
                        }
                        else if (result is Exception)
                        {
                            throw result as Exception;
                        }
                    }

                    return(result);
                }
            }
            catch (System.IO.IOException ioEx)
            {
                Close();
                throw ioEx;
            }
        }
Exemple #2
0
        static private void ReturnMessage(object lockObj, System.Net.Sockets.TcpClient tcpClient, MessageHead msgHead,
                                          object returnMsg, Hubble.Framework.Serialization.IMySerialization customSerializer, int classId)
        {
            lock (lockObj)
            {
                System.Net.Sockets.NetworkStream clientStream = tcpClient.GetStream();

                //System.Net.Sockets.NetworkStream tcpStream = clientStream;
                Hubble.Framework.Net.TcpCacheStream tcpStream = new TcpCacheStream(clientStream);

                byte[] sendBuf = BitConverter.GetBytes(msgHead.Event);
                tcpStream.Write(sendBuf, 0, sendBuf.Length);
                bool isAsync = (msgHead.Flag & MessageFlag.ASyncMessage) != 0;

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

                    if (isAsync)
                    {
                        msgHead.Flag = MessageFlag.ASyncMessage;
                    }
                    else
                    {
                        msgHead.Flag = 0;
                    }


                    if (customSerializer != null)
                    {
                        msgHead.Flag |= MessageFlag.CustomSerialization;
                        sendBuf       = BitConverter.GetBytes((short)msgHead.Flag);
                        tcpStream.Write(sendBuf, 0, sendBuf.Length);

                        if ((msgHead.Flag & MessageFlag.ASyncMessage) != 0)
                        {
                            sendBuf = BitConverter.GetBytes(classId);
                            tcpStream.Write(sendBuf, 0, sendBuf.Length);
                        }

                        //MemoryStream m = new MemoryStream();

                        //customSerializer.Serialize(m);
                        //m.Position = 0;

                        //while (m.Position < m.Length)
                        //{
                        //    byte[] buf = new byte[8192];

                        //    int len = m.Read(buf, 0, buf.Length);

                        //    tcpStream.Write(buf, 0, len);
                        //}

                        //customSerializer.Serialize(tcpStream);
                        customSerializer.Serialize(tcpStream);
                    }
                    else if (returnMsg is string)
                    {
                        msgHead.Flag |= MessageFlag.IsString;
                        sendBuf       = BitConverter.GetBytes((short)msgHead.Flag);
                        tcpStream.Write(sendBuf, 0, sendBuf.Length);

                        if ((msgHead.Flag & MessageFlag.ASyncMessage) != 0)
                        {
                            sendBuf = BitConverter.GetBytes(classId);
                            tcpStream.Write(sendBuf, 0, sendBuf.Length);
                        }

                        byte[] buf = Encoding.UTF8.GetBytes((returnMsg 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 if (returnMsg is Exception)
                    {
                        msgHead.Flag |= MessageFlag.IsException;
                        msgHead.Flag |= MessageFlag.IsString;

                        sendBuf = BitConverter.GetBytes((short)msgHead.Flag);
                        tcpStream.Write(sendBuf, 0, sendBuf.Length);

                        if ((msgHead.Flag & MessageFlag.ASyncMessage) != 0)
                        {
                            sendBuf = BitConverter.GetBytes(classId);
                            tcpStream.Write(sendBuf, 0, sendBuf.Length);
                        }

                        StringBuilder sb = new StringBuilder();
                        sb.AppendFormat("{0} innerStackTrace:{1}",
                                        (returnMsg as Exception).Message,
                                        Hubble.Framework.HubbleException.GetStackTrace(returnMsg as Exception));

                        byte[] buf = Encoding.UTF8.GetBytes(sb.ToString());

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

                        if ((msgHead.Flag & MessageFlag.ASyncMessage) != 0)
                        {
                            sendBuf = BitConverter.GetBytes(classId);
                            tcpStream.Write(sendBuf, 0, sendBuf.Length);
                        }

                        IFormatter formatter = new BinaryFormatter();
                        formatter.Serialize(tcpStream, returnMsg);
                    }
                }
                else
                {
                    msgHead.Flag |= MessageFlag.NullData;

                    //Send Flag
                    sendBuf = BitConverter.GetBytes((short)msgHead.Flag);
                    tcpStream.Write(sendBuf, 0, sendBuf.Length);

                    if ((msgHead.Flag & MessageFlag.ASyncMessage) != 0)
                    {
                        sendBuf = BitConverter.GetBytes(classId);
                        tcpStream.Write(sendBuf, 0, sendBuf.Length);
                    }
                }

                tcpStream.Flush();
            }
        }
Exemple #3
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;
            }
        }