public static byte[] ToBinary(WSBinaryCommandType commandObject) { MemoryStream ms = ToStream(commandObject) as MemoryStream; ms.Position = 0; BinaryReader br = new BinaryReader(ms); byte[] data = br.ReadBytes((int)ms.Length); br.Close(); return(data); }
public static WSBinaryCommandType ToCommandType(Stream stream) { stream.Position = 0; BinaryReader br = new BinaryReader(stream); if (stream.Length < 2 || br.ReadByte() != 0 || br.ReadByte() != 0) { return(null); } WSBinaryCommandType commandObj = new WSBinaryCommandType(); //Request ID byte[] requestId = br.ReadBytes(16); commandObj.RequestID = new Guid(requestId); //OccurTime long ticks = br.ReadInt64(); commandObj.OccurTime = new DateTime(ticks); //Name commandObj.CommandName = readString(br); //Type commandObj.CommandType = readString(br); //IsOver byte isOver = br.ReadByte(); commandObj.IsOver = (isOver == 0 ? false : true); //Parameter List //Count int parCount = br.ReadInt32(); if (parCount > 0) { commandObj.Parameters = new WSBinaryCommandTypeParameter[parCount]; for (int i = 0; i < parCount; i++) { commandObj.Parameters[i] = new WSBinaryCommandTypeParameter(); commandObj.Parameters[i].Name = readString(br); int valLen = br.ReadInt32(); if (valLen > 0) { commandObj.Parameters[i].Value = br.ReadBytes(valLen); } } } return(commandObj); }
public static Stream ToStream(WSBinaryCommandType commandObject) { MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); //固定标头 bw.Write((byte)0); bw.Write((byte)0); //Request Guid bw.Write(commandObject.RequestID.ToByteArray()); //OccurTime bw.Write(commandObject.OccurTime.Ticks); //Name writeString(bw, commandObject.CommandName); //Type writeString(bw, commandObject.CommandType); //IsOver bw.Write((byte)(commandObject.IsOver ? 1 : 0)); //Parameter List //Count bw.Write(commandObject.Parameters == null ? (int)0 : commandObject.Parameters.Length); //Parameter if (commandObject.Parameters != null && commandObject.Parameters.Any()) { foreach (WSBinaryCommandTypeParameter p in commandObject.Parameters) { writeString(bw, p.Name); bw.Write(p.Value == null ? (int)0 : p.Value.Length); if (p.Value != null && p.Value.Length > 0) { bw.Write(p.Value); } } } ms.Flush(); return(ms); }
/// <summary> /// 新建一个CommandServer /// </summary> /// <param name="ipAddress">监听的IP地址,如果传入空对应于IPAddress.Any</param> /// <param name="port">监听端口</param> /// <param name="appName">应用名称,表示此服务的名称,对功能无关紧要</param> /// <param name="serverName">服务名称,表示服务使用的内部TCP监听服务的名称,对功能无关紧要</param> /// <param name="useProtocol">所使用的协议</param> /// <param name="sessionFactory">创建内部Session的工厂, 注意:工厂所创建的Session必须是WebSocketSession的子类</param> public CommandServer(string ipAddress, int port, string appName, string serverName, string useProtocol, ISocketSessionFactory sessionFactory) { logger = LoggerManager.GetLogger("CommandServer." + (appName == "" ? "UnName" : appName)); DefaultCommandParser = new WSBinaryCommandType(); this.SessionFactory = sessionFactory; IP = ipAddress; Port = port; RequestTimeout = TimeSpan.FromMinutes(2); SessionList = new Dictionary <string, CommandSession>(); Protocol = useProtocol; IsAync = true; CommandList = new List <ICommand>(); CommandList.Add(new SetCommandParserRequest()); CommandParser = new List <ICommandParser>() { new WSCommandType(), new WSBinaryCommandType() }; CommandAliveTime = TimeSpan.FromMinutes(2); IPAddress address = IPAddress.Any; if (!String.IsNullOrEmpty(ipAddress)) { address = IPAddress.Parse(IP); } application = new WebSocketApplication(); application.Setup(appName); AsyncTcpServer server = new AsyncTcpServer(); ServerConfig = new SocketServerConfig() { Address = address, AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork, MaxConnectionNumber = 1000, Name = serverName, Port = Port, SessionTimeout = (long)TimeSpan.FromMinutes(5).TotalMilliseconds, TimeoutType = Dynamic.Net.Session.SessionTimeoutType.Unknown, //不过期 ServerType = NetServerType.ASyncTcp }; server.Setup(ServerConfig, application, new WebSocketProtocol(), sessionFactory); application.AddServer(server); application.SwitchingProtocol += new EventHandler <SwitchingProtocolEventArgs>(SwitchingProtocol); application.SessionStarted += new EventHandler(SessionStarted); application.SessionClosed += new EventHandler(SessionClosed); application.HandshakeCompleted += new EventHandler(HandshakeCompleted); application.MessageReceived += new EventHandler <MessageReceivedEventArgs>(MessageReceived); }