public static NetPackage[] ContentCreate(NetCommandType commandID, byte[] data, IPEndPoint iep) { int contentID = NetPackage.GenerateContentID(); int max_pkg_sz = 384; int offset = 0; int order = 1; int count = (data.Length + max_pkg_sz - 1) / max_pkg_sz; NetPackage[] packages = new NetPackage[count]; int length = data.Length; while (length != 0) { int sz = Math.Min(384, length); byte[] buf = new MemoryStream(data, offset, sz).ToArray(); packages[order - 1] = (NetPackage.Create(contentID, order, commandID, count, buf, iep)); order++; offset += sz; length -= sz; } return(packages); }
public static NetPackage Parse(byte[] buffer, int sz, IPEndPoint ep) { using (BinaryReader br = new BinaryReader(new MemoryStream(buffer))) { int magic = br.ReadInt32(); if (magic != MagicNumber) { return(null); } NetPackage package = new NetPackage(); package.RemoteEP = new IPEndPoint(ep.Address, ep.Port); package.ID = br.ReadInt32(); package.ContentID = br.ReadInt32(); package.OrderID = br.ReadInt32(); if (package.OrderID == 1) { package.CommandID = (NetCommandType)br.ReadInt32(); package.PackageCount = br.ReadInt32(); } package.Size = sz - (int)br.BaseStream.Position; package.Buffer = new MemoryStream(buffer, 0, sz).ToArray(); return(package); } }
public static NetPackage Create(int contentID, int orderID, NetCommandType commandID, int packageCount, byte[] data, IPEndPoint iep) { using (BinaryWriter bw = new BinaryWriter(new MemoryStream())) { int packageID = GeneratePackageID(); bw.Write(MagicNumber); bw.Write(packageID); bw.Write(contentID); bw.Write(orderID); if (orderID == 1) { bw.Write((int)commandID); bw.Write(packageCount); } bw.Write(data, 0, data.Length); bw.Flush(); bw.BaseStream.Seek(0, SeekOrigin.Begin); NetPackage package = new NetPackage(); package.RemoteEP = new IPEndPoint(iep.Address, iep.Port); package.ID = packageID; package.ContentID = contentID; package.OrderID = orderID; package.CommandID = commandID; package.PackageCount = packageCount; package.Buffer = ((MemoryStream)bw.BaseStream).ToArray(); package.Size = data.Length; return package; } }
private void AddPackage(NetPackage package) { if (package == null) { return; } if (package.RemoteEP.Port != NetService.SocketSendPort) { return; } package.RemoteEP.Port = NetService.SocketReadPort; if (package.CommandID != NetCommandType.RemoveProcessedPackage) { OutgoingPackagePool.AddFirst(NetPackageGenerater.TellReceived(package.ID, package.RemoteEP)); } string key = UserInfo.ToUserToken(package.RemoteEP); Sender sender = null; if (m_senders.ContainsKey(key)) { sender = m_senders[key]; } else { sender = new Sender(package.RemoteEP); m_senders.Add(key, sender); } sender.AddPackage(package); }
public static void DequeueUnprocess(List <NetPackage> pkgs) { lock (m_lockobject) { int sz = 0; foreach (NetPackage pkg in pkgs) { sz += pkg.Buffer.Length; } while (s_singleton.m_unprocessed.Count != 0) { NetPackage pkg = s_singleton.m_unprocessed[0]; sz += pkg.Buffer.Length; if (sz >= NetService.SocketBufferSize) { break; } s_singleton.m_unprocessed.RemoveAt(0); if (pkg.CommandID != NetCommandType.RemoveProcessedPackage) { s_singleton.m_processed.Add(pkg.ID, new PackageLife(pkg)); } pkgs.Add(pkg); } } }
public static void Add(NetPackage[] packages) { lock (m_lockobject) { s_singleton.m_unprocessed.AddRange(packages); } }
public static void AddFirst(NetPackage[] packages) { lock (m_lockobject) { s_singleton.m_unprocessed.InsertRange(0, packages); } }
public static NetPackage Create(int contentID, int orderID, NetCommandType commandID, int packageCount, byte[] data, IPEndPoint iep) { using (BinaryWriter bw = new BinaryWriter(new MemoryStream())) { int packageID = GeneratePackageID(); bw.Write(MagicNumber); bw.Write(packageID); bw.Write(contentID); bw.Write(orderID); if (orderID == 1) { bw.Write((int)commandID); bw.Write(packageCount); } bw.Write(data, 0, data.Length); bw.Flush(); bw.BaseStream.Seek(0, SeekOrigin.Begin); NetPackage package = new NetPackage(); package.RemoteEP = new IPEndPoint(iep.Address, iep.Port); package.ID = packageID; package.ContentID = contentID; package.OrderID = orderID; package.CommandID = commandID; package.PackageCount = packageCount; package.Buffer = ((MemoryStream)bw.BaseStream).ToArray(); package.Size = data.Length; return(package); } }
public static void AddProcessedPackage(NetPackage[] packages) { lock (m_lockobject) { foreach (NetPackage pkg in packages) { s_singleton.m_processed.Add(pkg.ID, new PackageLife(pkg)); } } }
public static NetPackage[] VersionUpdate(byte[] updateFileData, IPEndPoint iep) { byte[] bytes = Helper.GetBytes(DataManager.Version); MemoryStream ms = new MemoryStream(); ms.Write(Helper.GetBytes(bytes.Length), 0, 4); ms.Write(bytes, 0, bytes.Length); ms.Write(Helper.GetBytes(updateFileData.Length), 0, 4); ms.Write(updateFileData, 0, updateFileData.Length); return(NetPackage.ContentCreate(NetCommandType.VersionUpdate, ms.ToArray(), iep)); }
public PackageLife(NetPackage pkg) { m_pkg = pkg; m_resendCounter = 20; m_timer = 300; if (s_maxTimes == null) { s_maxTimes = new List <int>(); for (int i = 0; i < m_resendCounter; i++) { s_maxTimes.Add(200 + 100 * (m_resendCounter - i - 1)); } } }
public void AddPackage(NetPackage package) { if (package == null) { return; } if (m_finishedContents.ContainsKey(package.ContentID)) { return; } Content content = null; if (m_contents.ContainsKey(package.ContentID)) { content = m_contents[package.ContentID]; } else { content = new Content(package.ContentID); m_contents.Add(package.ContentID, content); } content.Add(package); if (content.IsFull()) { Cmd cmd = null; byte[] data = content.CombineData(); UserInfo usr = UserInfoManager.FindUser(RemoteEP); string usrname = (usr == null) ? "no user" : usr.Username; Logger.CounterCommand_Recv(content.CommandType); if (content.CommandType != NetCommandType.RemoveProcessedPackage) { Logger.WriteLine("<<--- command: " + content.CommandType.ToString() + " from :" + usrname); } cmd = GenerateCommand(content, cmd, data); CommandPool.AddCommand(cmd); m_contents.Remove(content.ID); m_finishedContents.Add(content.ID, content.ID); } }
public void Add(NetPackage package) { if (m_packages.ContainsKey(package.OrderID)) { return; } else { m_packages.Add(package.OrderID, package); } if (package.OrderID == 1) { type = (NetCommandType)package.CommandID; m_count = package.PackageCount; } }
public static NetPackage[] AppendImageMessage(string imagePath, IPEndPoint iep) { MemoryStream stream = new MemoryStream(); byte[] bytes = null; bytes = Helper.GetBytes(Path.GetFileName(imagePath)); stream.Write(Helper.GetBytes(bytes.Length), 0, 4); stream.Write(bytes, 0, bytes.Length); bytes = File.ReadAllBytes(imagePath); stream.Write(bytes, 0, bytes.Length); bytes = stream.ToArray(); stream.Dispose(); return(NetPackage.ContentCreate(NetCommandType.AppendImageMessage, bytes, iep)); }
public static bool Receive(Socket socket) { if (socket.Available <= 0) { return(false); } try { IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0); EndPoint ep = iep; bool received = false; int len = socket.ReceiveFrom(buffer, ref ep); while (len != 0) { received = true; Logger.Counter_Pkg_Recv(); s_singleton.AddPackage(NetPackage.Parse(buffer, len, (IPEndPoint)ep)); if (socket.Available <= 0) { break; } len = socket.ReceiveFrom(buffer, ref ep); } return(received); } catch (Exception e) { Logger.WriteLine(e.Message); } return(false); }
public static NetPackage[] ContentCreate(NetCommandType commandID, byte[] data, IPEndPoint iep) { int contentID = NetPackage.GenerateContentID(); int max_pkg_sz = 384; int offset = 0; int order = 1; int count = (data.Length + max_pkg_sz - 1) / max_pkg_sz; NetPackage[] packages = new NetPackage[count]; int length = data.Length; while (length != 0) { int sz = Math.Min(384, length); byte[] buf = new MemoryStream(data, offset, sz).ToArray(); packages[order - 1] = (NetPackage.Create(contentID, order, commandID, count, buf, iep)); order++; offset += sz; length -= sz; } return packages; }
public static NetPackage[] BroadcastFindUser() { IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, NetService.SocketReadPort); return(NetPackage.ContentCreate(NetCommandType.BroadcastFindUser, Helper.GetBytes(DataManager.WhoAmI), iep)); }
public static NetPackage Parse(byte[] buffer, int sz, IPEndPoint ep) { using (BinaryReader br = new BinaryReader(new MemoryStream(buffer))) { int magic = br.ReadInt32(); if (magic != MagicNumber) return null; NetPackage package = new NetPackage(); package.RemoteEP = new IPEndPoint(ep.Address, ep.Port); package.ID = br.ReadInt32(); package.ContentID = br.ReadInt32(); package.OrderID = br.ReadInt32(); if (package.OrderID == 1) { package.CommandID = (NetCommandType)br.ReadInt32(); package.PackageCount = br.ReadInt32(); } package.Size = sz - (int)br.BaseStream.Position; package.Buffer = new MemoryStream(buffer, 0, sz).ToArray(); return package; } }
public static NetPackage[] TellReceived(int packageID, IPEndPoint iep) { return(NetPackage.ContentCreate(NetCommandType.RemoveProcessedPackage, BitConverter.GetBytes(packageID), iep)); }
public static NetPackage[] FindGroupUser(string groupKey, IPEndPoint iep) { return(NetPackage.ContentCreate(NetCommandType.FindGroupUser, Helper.GetBytes(groupKey + ";" + DataManager.WhoAmI), iep)); }
public static NetPackage[] AddGroupUser(string groupKey, IPEndPoint iep) { return(NetPackage.ContentCreate(NetCommandType.AddGroupUser, Helper.GetBytes(groupKey), iep)); }
public PackageLife(NetPackage pkg) { m_pkg = pkg; m_resendCounter = 20; m_timer = 300; if (s_maxTimes == null) { s_maxTimes = new List<int>(); for (int i = 0; i < m_resendCounter; i++) { s_maxTimes.Add(200 + 100 * (m_resendCounter - i - 1)); } } }
public static NetPackage[] AppendGroupTextMessage(string groupKey, string msg, IPEndPoint iep) { string val = string.Format("{0};{1}", groupKey, msg); return(NetPackage.ContentCreate(NetCommandType.AppendGroupTextMessage, Helper.GetBytes(val), iep)); }
public static NetPackage[] AppendTextMessage(string msg, IPEndPoint iep) { return(NetPackage.ContentCreate(NetCommandType.AppendTextMessage, Helper.GetBytes(msg), iep)); }
public static NetPackage[] CreateNewGroup(string groupKey, string name, IPEndPoint iep) { string val = groupKey + ";" + name; return(NetPackage.ContentCreate(NetCommandType.CreateNewGroup, Helper.GetBytes(val), iep)); }
public static NetPackage[] UserOffline(IPEndPoint iep) { return(NetPackage.ContentCreate(NetCommandType.UserOffline, Helper.GetBytes(DataManager.WhoAmI), iep)); }