public void Broadcast(INetCommand command, Func <INetSession, bool> check) { List <INetSession> bcList = new List <INetSession>(); lock (SyncLocker) { foreach (KeyValuePair <string, INetSession> kv in SessionList) { if (check(kv.Value)) { bcList.Add(kv.Value); } } } bcList.All(x => { if (x != null) { try { x.Protocol.WriteCommand(command, x); } catch (Exception e) { logger.Warn("广播消息失败:{0}", x.SessionID); } } return(true); }); }
public virtual void Broadcast(INetCommand command) { lock (SyncLocker) { Broadcast(command, (x) => { return(true); }); } }
public virtual void WriteCommand(INetCommand command, INetSession session) { Byte[] rdata = null; if (command != null) { rdata = command.CommandName; if (rdata != null && rdata.Length > 0) { session.WriteBytes(rdata, 0, rdata.Length); WriteCommandNameEndBytes(command, session); } if (command.Parameters != null) { foreach (Byte[] pData in command.Parameters) { if (pData != null && pData.Length > 0) { session.WriteBytes(pData, 0, pData.Length); } WriteCommandParameterSplitBytes(command, session); } } } WriteFrameEndBytes(command, session); }
public override Dynamic.Net.Base.INetCommand GetCommand(INetSession session, System.IO.Stream stream) { INetCommand command = null; StreamReader sr = new StreamReader(stream); string header = sr.ReadToEnd(); if (header.StartsWith("GET ", StringComparison.OrdinalIgnoreCase)) { HandshakeRequestCommand handshakeCmd = new HandshakeRequestCommand(); if (handshakeCmd.Parse(header)) { command = handshakeCmd; } } else { WebSocketSession wSession = session as WebSocketSession; if (wSession != null && wSession.IsHandShake) { } else { session.Close(); } } return(command); }
public override Dynamic.Net.Base.INetCommand Execute(Dynamic.Net.Base.INetSession session) { WebSocketSessionBase ws = session as WebSocketSessionBase; if (ws == null) { return(null); } MessageReceivedEventArgs args = new MessageReceivedEventArgs() { ContentType = MessageContentType.Binary, Data = InnerData, Session = session as WebSocketSessionBase, IsAync = false }; ws.OnMessageReceived(args); INetCommand responseCommand = null; if (!args.IsAync && args.ResponseData != null && args.ResponseData.Length > 0) { responseCommand = WebSocketCommandFactory.CreateCommand(args.ResponseData); } return(responseCommand); }
public override Dynamic.Net.Base.INetCommand Execute(Dynamic.Net.Base.INetSession session) { if (InnerData != null && InnerData.Length > 0) { Content = session.Encoding.GetString(InnerData); } WebSocketSessionBase ws = session as WebSocketSessionBase; if (ws == null) { return(null); } MessageReceivedEventArgs args = new MessageReceivedEventArgs() { Content = this.Content, ContentType = MessageContentType.Text, Data = InnerData, Session = ws, IsAync = false }; ws.OnMessageReceived(args); INetCommand responseCommand = null; if (!args.IsAync && !String.IsNullOrEmpty(args.ResponseContent)) { responseCommand = WebSocketCommandFactory.CreateCommand(args.ResponseContent); } return(responseCommand); }
protected virtual void AsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e) { if (e.BytesTransferred <= 0) { Offline(); return; } if (e.SocketError != SocketError.Success) { Offline(); return; } ReceivedBytes += e.BytesTransferred - e.Offset; for (int i = e.Offset; i < e.Offset + e.BytesTransferred; i++) { frameStream.WriteByte(e.Buffer[i]); frameStream.Flush(); if (isNoCheck && noCheckCount > 0) { noCheckCount--; continue; } else if (isNoCheck) { isNoCheck = false; noCheckCount = 0; } if (Protocol.IsFrameEnd(frameStream)) { frameStream.Position = 0; INetCommand command = Protocol.GetCommand(this, frameStream); if (command != null) { frameStream.Position = 0; frameStream.SetLength(0); command = command.Execute(this); } Protocol.WriteCommand(command, this); } } if (!isClosed) { Protocol.TryGetCommand(this); } }
public override void WriteCommand(INetCommand command, INetSession session) { if (command is HandshakeResponseCommand) { HandshakeResponseCommand response = command as HandshakeResponseCommand; Byte[] responseData = response.GetResponseData(session); session.WriteBytes(responseData, 0, responseData.Length); } else if (command is FrameCommandBase) { FrameStreamWriter writer = new FrameStreamWriter(command as FrameCommandBase); writer.Write(session); } }
public static void Send(this INetCommand command, NetworkDestination destination, Int32 recievedFrom = -1) { if (destination.ShouldRun()) { command.OnRecieved(); } if (destination.ShouldSend()) { Header header = destination.GetHeader(NetworkCore.GetNetworkCoreHash(command.GetType())); if (NetworkServer.active) { for (Int32 i = 0; i < NetworkServer.connections.Count; ++i) { if (i == recievedFrom) { continue; } NetworkConnection conn = NetworkServer.connections[i]; if (conn == null) { continue; } if (NetworkServer.localClientActive && NetworkServer.localConnections.Contains(conn)) { continue; } using (Writer netWriter = NetworkCore.GetWriter(NetworkCore.commandIndex, conn, NetworkCore.qos)) { NetworkWriter writer = netWriter; writer.Write(header); } } } else if (NetworkClient.active) { using (Writer netWriter = NetworkCore.GetWriter(NetworkCore.commandIndex, ClientScene.readyConnection, NetworkCore.qos)) { NetworkWriter writer = netWriter; writer.Write(header); } } } }
public override INetCommand GetCommand(INetSession session, Stream stream) { INetCommand command = null; List <Byte> sb = new List <Byte>(); int c = 0; int parIdx = 0; while (true) { c = stream.ReadByte(); if (c == -1) { break; } if (c == 0xff && command == null) { Byte[] cmdNam = sb.ToArray(); command = Commands.FirstOrDefault(x => x.IsMatch(cmdNam)); sb.Clear(); if (command == null) { break; } } else if (command != null && c == 0xff) { if (!command.SetParameter(sb.ToArray(), parIdx)) { command = null; break; } parIdx++; } else { sb.Add((byte)c); } } return(command); }
public static void Send(this INetCommand command, NetworkDestination destination) { if (destination.ShouldRun()) { command.OnReceived(); } if (destination.ShouldSend()) { var header = destination.GetHeader(NetworkingAPI.GetNetworkHash(command.GetType())); if (NetworkServer.active) { for (int i = 0; i < NetworkServer.connections.Count; ++i) { NetworkConnection conn = NetworkServer.connections[i]; if (conn == null) { continue; } if (NetworkServer.localClientActive && NetworkServer.localConnections.Contains(conn)) { continue; } using (Writer netWriter = NetworkingAPI.GetWriter(NetworkingAPI.CommandIndex, conn, QosType.Reliable)) { NetworkWriter writer = netWriter; writer.Write(header); } } } else if (NetworkClient.active) { using (var netWriter = NetworkingAPI.GetWriter(NetworkingAPI.CommandIndex, ClientScene.readyConnection, QosType.Reliable)) { NetworkWriter writer = netWriter; writer.Write(header); } } } }
public virtual void Start() { OnSessionStarted(); while (true) { if (isClosed) { break; } INetCommand command = protocol.GetCommand(this); if (isClosed) { break; } if (command != null) { command = command.Execute(this); } protocol.WriteCommand(command, this); } }
protected override void WriteFrameEndBytes(INetCommand command, INetSession session) { session.WriteBytes(splitCommandBytes, 0, splitCommandBytes.Length); }
protected abstract void WriteFrameEndBytes(INetCommand command, INetSession session);
protected override void WriteCommandNameEndBytes(INetCommand command, INetSession session) { session.WriteBytes(new byte[] { 0xff }, 0, 1); }
protected abstract void WriteCommandParameterSplitBytes(INetCommand command, INetSession session);
protected override void WriteCommandParameterSplitBytes(INetCommand command, INetSession session) { session.WriteBytes(new byte[] { 0xff }, 0, 1); }