public void Send(NetCommand command) { if (!socket.Connected) { return; } if (command.Sender == null || command.Sender == "") { if (this.Name != null && this.Name != "") { command.Sender = Name; } else { command.Sender = LocalIpPortString; } if (asynSender == null) { asynSender = new SocketAsyncEventArgs(); } } ByteCommand byteCommand = new ByteCommand(command); if (byteCommand.Bytes.Length > PublicConfig.ReceiveCount) { byte[] packetPart; int byteCount = byteCommand.Bytes.Length / PublicConfig.ReceiveCount; for (int i = 0; i <= byteCount; i++) { if (i == byteCount) { packetPart = new byte[byteCommand.Bytes.Length - PublicConfig.ReceiveCount * i]; Array.Copy(byteCommand.Bytes, i * PublicConfig.ReceiveCount, packetPart, 0, byteCommand.Bytes.Length - PublicConfig.ReceiveCount * i); } else { packetPart = new byte[PublicConfig.ReceiveCount]; Array.Copy(byteCommand.Bytes, i * PublicConfig.ReceiveCount, packetPart, 0, PublicConfig.ReceiveCount); } sendthreadqueue.PutInNewItem(Send, packetPart); } } else { sendthreadqueue.PutInNewItem(Send, byteCommand.Bytes); } }
public void SendToClient(NetCommand command, string client) { if (clients.ContainsValue(client)) { foreach (var item in clients) { if (item.Value == client) { client = item.Key; break; } } } if (sessions.ContainsKey(client)) { sessions[client].Send(command); } }
public void SendToOtherClients(NetCommand command, string except) { if (clients.ContainsValue(except)) { foreach (var item in clients) { if (item.Value == except) { except = item.Key; break; } } } foreach (var item in sessions) { if (item.Key != except) { item.Value.Send(command); } } }
public ByteCommand(NetCommand command) { netCommand = command; objectBytes = PacketBytes(CommandToBytes(command)); }
public ByteCommand(byte[] b) { objectBytes = b; netCommand = BytesToCommand(b); }
public abstract void Receive(NetCommand command);
/// <summary> /// 接受到信息处理 /// </summary> public override void Receive(NetCommand command) { if (command.CommandLevel != CommandLevel.SYSTEM) { if (DataReceived != null) { DataEventArgs args = new DataEventArgs(); args.Client = Name; args.RemoteIpPortString = this.RemoteIpPortString; args.Command = command; DataReceived(args); } } else { if (command.CommandType == CommandType.Get_OtherClients) { OtherClients.Clear(); foreach (var item in (command.Data as Dictionary <string, string>)) { if (item.Key != this.LocalIpPortString) { OtherClients.Add(item.Value); } } if (ClientConnected != null) { ClientConnected(new ClientEventArgs() { Client = Name, RemoteIpPortString = this.RemoteIpPortString, IsSuccess = true }); } } if (command.CommandType == CommandType.Client_Connect) { OtherClients.Add(command.Command); if (OtherClientConnected != null) { OtherClientConnected(new DataEventArgs() { Client = command.Command }); } } if (command.CommandType == CommandType.Client_Disconnect) { OtherClients.Remove(command.Command); if (OtherClientDisconnected != null) { OtherClientDisconnected(new DataEventArgs() { Client = command.Command }); } } if (command.CommandType == CommandType.Client_Change) { OtherClients.Remove(command.Sender); OtherClients.Add(command.Command); if (OtherClientChanged != null) { OtherClientChanged(new DataEventArgs() { Command = command }); } } if (SysCommandReceived != null) { DataEventArgs args = new DataEventArgs(); args.Client = this.Name; args.RemoteIpPortString = this.RemoteIpPortString; args.Command = command; SysCommandReceived(args); } } }