/// <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; } }
public ServerException(InnerServerException e) : base(e.Message) { _InnerStackTrace = e.InnerStackTrace; }
private void AsyncMessageRecv() { TcpCacheStream tcpStream = new TcpCacheStream(_ClientStream); while (true) { try { object result; //Recevie data byte[] revBuf = new byte[8]; int offset = 0; while (offset < 8) { int len = tcpStream.Read(revBuf, offset, 8 - offset); if (len == 0) { throw new Exception("Tcp stream closed!"); } offset += len; } MessageHead head = new MessageHead(); head.Event = BitConverter.ToInt16(revBuf, 0); head.Flag = (MessageFlag)BitConverter.ToInt16(revBuf, 2); int classId = BitConverter.ToInt32(revBuf, 4); 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 b = (byte)tcpStream.ReadByte(); while (b != 0) { m.WriteByte(b); b = (byte)tcpStream.ReadByte(); } 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 (AsyncMessageRecieved != null) { AsyncMessageRecieved(new ASyncPackage(classId, result)); } } } catch (Exception e) { lock (this) { Close(); } ThreadClosed = true; if (AsyncMessageRecieved != null) { AsyncMessageRecieved(new ASyncPackage(-1, new TcpRemoteCloseException(e.Message))); } return; } } }