public virtual byte[] Receive(ws2_32.SocketFlags flag) { return(_Receive(flag)); }
/// <summary> send a packet to the remote server </summary> /// <param name="payload">The bytes that you want to sent</param> /// <returns>The length of the packet that has been sent</returns> public virtual int SendPacket(byte[] payload, ws2_32.SocketFlags flag) { return(_SendPacket(payload, flag, false)); }
private unsafe byte[] _Receive(ws2_32.SocketFlags flag) { List <Byte> payload = new List <Byte>(); if (EnableSizeHeader) { int PayloadSize = 0; byte[] xSize = Receive((uint)HeaderSize); if (xSize.Length <= 0) { return(new byte[] { }); } try { switch (HeaderSize) { case HeaderTypeSize.BYTE: PayloadSize = xSize[0]; break; case HeaderTypeSize.INT: PayloadSize = BitConverter.ToInt32(xSize, 0); break; case HeaderTypeSize.SHORT: PayloadSize = BitConverter.ToInt16(xSize, 0); break; } } catch (Exception ex) { GlobalVariables.DumpErrorLog(ex); //Overflow exception, We have been attacked! Disconnect(); return(new byte[] { }); } if (PayloadSize < 0) { Disconnect(); return(new byte[] { }); } //Read the whole packet payload.AddRange(Receive((uint)PayloadSize)); if (!Connected) { Disconnect(); return(new byte[] { }); } } else { //Just read the data we are receiving byte[] bytes = new byte[ReceiveBufferSize]; int recv = 0; fixed(byte *x = bytes) recv = ws2_32.recv(this.Handle, x, bytes.Length, (int)flag); if (recv <= 0) { Disconnect(); return(new byte[] { }); } Array.Resize(ref bytes, recv); payload.AddRange(bytes); } return(payload.ToArray()); }
private unsafe int _SendPacket(byte[] payload, ws2_32.SocketFlags flag, bool async) { if (!Connected) { return(-1); } if (EnableSizeHeader) { if (HeaderSize == HeaderTypeSize.BYTE && payload.Length > 255) { throw new Exception("Payload cannot be bigger then 255 bytes, Recheck the HeaderSize"); } else if (HeaderSize == HeaderTypeSize.INT && payload.Length > int.MaxValue) { throw new Exception("Payload cannot be bigger then " + int.MaxValue + " bytes, Recheck the HeaderSize"); } else if (HeaderSize == HeaderTypeSize.SHORT && payload.Length > 65535) { throw new Exception("Payload cannot be bigger then 65535 bytes, Recheck the HeaderSize"); } byte[] HeaderBuffer = new byte[(int)HeaderSize]; switch (HeaderSize) { case HeaderTypeSize.BYTE: HeaderBuffer = BitConverter.GetBytes((byte)payload.Length); break; case HeaderTypeSize.INT: HeaderBuffer = BitConverter.GetBytes((int)payload.Length); break; case HeaderTypeSize.SHORT: HeaderBuffer = BitConverter.GetBytes((short)payload.Length); break; } //Send header int DataSend = 0; fixed(byte *x = HeaderBuffer) { int ret = ws2_32.send(this.Handle, x, HeaderBuffer.Length, (int)flag); if (ret <= 0) { Disconnect(); return(0); } DataSend += ret; } //Send our data byte[] buffer = new byte[0]; for (uint i = 0; i < payload.Length; i += SendBufferSize) { if ((i + SendBufferSize) <= payload.Length) { buffer = new byte[SendBufferSize]; } else { buffer = new byte[payload.Length - i]; } Array.Copy(payload, i, buffer, 0, buffer.Length); fixed(byte *x = buffer) { int ret = ws2_32.send(this.Handle, x, buffer.Length, (int)flag); if (ret <= 0) { Disconnect(); return(0); } DataSend += ret; } } return(DataSend); } else { fixed(byte *x = payload) return(ws2_32.send(this.Handle, x, payload.Length, (int)flag)); } }