/** Ask for the pointer using sizebuf_t.cursize (RST) */ public static int GetSpace(sizebuf_t buf, int length) { int oldsize; if (buf.cursize + length > buf.maxsize) { if (!buf.allowoverflow) { Com.Error(Defines.ERR_FATAL, "SZ_GetSpace: overflow without allowoverflow set"); } if (length > buf.maxsize) { Com.Error(Defines.ERR_FATAL, "SZ_GetSpace: " + length + " is > full buffer size"); } Com.Printf("SZ_GetSpace: overflow\n"); SZ.Clear(buf); buf.overflowed = true; } oldsize = buf.cursize; buf.cursize += length; return(oldsize); }
static byte[] SZ_GetSpace(sizebuf_t buf, int length, out int offset) { byte[] data; if (buf.cursize + length > buf.maxsize) { if (!buf.allowoverflow) { sys_linux.Sys_Error("SZ_GetSpace: overflow without allowoverflow set"); } if (length > buf.maxsize) { sys_linux.Sys_Error("SZ_GetSpace: " + length + " is > full buffer size"); } buf.overflowed = true; console.Con_Printf("SZ_GetSpace: overflow"); SZ_Clear(buf); } data = buf.data; offset = buf.cursize; buf.cursize += length; return(data); }
public static void Write(sizebuf_t buf, byte[] data) { var length = data.Length; //memcpy(SZ_GetSpace(buf, length), data, length); Array.Copy(data, 0, buf.data, SZ.GetSpace(buf, length), length); }
public static void SV_WriteFrameToClient(client_t client, sizebuf_t msg) { client_frame_t frame, oldframe; Int32 lastframe; frame = client.frames[SV_INIT.sv.framenum & Defines.UPDATE_MASK]; if (client.lastframe <= 0) { oldframe = null; lastframe = -1; } else if (SV_INIT.sv.framenum - client.lastframe >= (Defines.UPDATE_BACKUP - 3)) { oldframe = null; lastframe = -1; } else { oldframe = client.frames[client.lastframe & Defines.UPDATE_MASK]; lastframe = client.lastframe; } MSG.WriteByte(msg, Defines.svc_frame); MSG.WriteLong(msg, SV_INIT.sv.framenum); MSG.WriteLong(msg, lastframe); MSG.WriteByte(msg, client.surpressCount); client.surpressCount = 0; MSG.WriteByte(msg, frame.areabytes); SZ.Write(msg, frame.areabits, frame.areabytes); SV_WritePlayerstateToClient(oldframe, frame, msg); SV_EmitPacketEntities(oldframe, frame, msg); }
public static void SZ_Print(sizebuf_t buf, string data) { int len; len = data.Length + 1; // byte * cast to keep VC++ happy if (buf.data[buf.cursize - 1] != 0) { int offset; byte[] dst = SZ_GetSpace(buf, len, out offset); for (int kk = 0; kk < data.Length; kk++) { dst[offset + kk] = (byte)data[kk]; // no trailing 0 } } else { int offset; byte[] dst = SZ_GetSpace(buf, len - 1, out offset); for (int kk = 0; kk < data.Length; kk++) { dst[offset - 1 + kk] = (byte)data[kk]; // write over trailing 0 } } }
public static void WriteShort(sizebuf_t sb, int c) { var i = SZ.GetSpace(sb, 2); sb.data[i++] = (byte)(c & 0xff); sb.data[i] = (byte)(((uint)c >> 8) & 0xFF); }
public static string ReadStringLine(sizebuf_t msg_read) { int l; byte c; l = 0; do { c = (byte)MSG.ReadChar(msg_read); if (c == 0xff || c == 0x00 || c == 0x0a) { break; } MSG.readbuf[l] = c; l++; }while (l < 2047); var ret = Encoding.ASCII.GetString(MSG.readbuf, 0, l).Trim(); Com.dprintln("MSG.ReadStringLine:[" + ret.Replace('\0', '@') + "]"); return(ret); }
// public static void Print(sizebuf_t buf, string data) { Com.dprintln("SZ.print():<" + data + ">"); var length = data.Length; var str = Lib.stringToBytes(data); if (buf.cursize != 0) { if (buf.data[buf.cursize - 1] != 0) { //memcpy( SZ_GetSpace(buf, len), data, len); // no trailing 0 Array.Copy(str, 0, buf.data, SZ.GetSpace(buf, length + 1), length); } else { Array.Copy(str, 0, buf.data, SZ.GetSpace(buf, length) - 1, length); //memcpy(SZ_GetSpace(buf, len - 1) - 1, data, len); // write over trailing 0 } } else { // first print. Array.Copy(str, 0, buf.data, SZ.GetSpace(buf, length), length); } //memcpy(SZ_GetSpace(buf, len), data, len); buf.data[buf.cursize - 1] = 0; }
public static void ReadData(sizebuf_t msg_read, byte[] data, int len) { for (var i = 0; i < len; i++) { data[i] = (byte)MSG.ReadByte(msg_read); } }
//should be ok. public static void WriteDir(sizebuf_t sb, float[] dir) { int i, best; float d, bestd; if (dir == null) { MSG.WriteByte(sb, 0); return; } bestd = 0; best = 0; for (i = 0; i < Defines.NUMVERTEXNORMALS; i++) { d = Math3D.DotProduct(dir, Globals.bytedirs[i]); if (d > bestd) { bestd = d; best = i; } } MSG.WriteByte(sb, best); }
public static void SZ_Write(sizebuf_t buf, byte[] data, int dataofs, int length) { int offset; byte[] dst = SZ_GetSpace(buf, length, out offset); Buffer.BlockCopy(data, dataofs, dst, offset, length); }
public static void Copy(sizebuf_t src, sizebuf_t dst) { dst.allowoverflow = src.allowoverflow; dst.overflowed = src.overflowed; dst.data = src.data; dst.maxsize = src.maxsize; dst.cursize = src.cursize; }
public static void MSG_WriteFloat(sizebuf_t sb, double f) { byte[] dat; dat = BitConverter.GetBytes((float)f); SZ_Write(sb, dat, 4); }
public static void MSG_WriteByte(sizebuf_t sb, int c) { byte[] buf; int offset; buf = SZ_GetSpace(sb, 1, out offset); buf[offset] = (byte)c; }
public static void ReadDeltaUsercmd(sizebuf_t msg_read, usercmd_t from, usercmd_t move) { int bits; //memcpy(move, from, sizeof(* move)); // IMPORTANT!! copy without new move.set(from); bits = MSG.ReadByte(msg_read); // read current angles if ((bits & Defines.CM_ANGLE1) != 0) { move.angles[0] = MSG.ReadShort(msg_read); } if ((bits & Defines.CM_ANGLE2) != 0) { move.angles[1] = MSG.ReadShort(msg_read); } if ((bits & Defines.CM_ANGLE3) != 0) { move.angles[2] = MSG.ReadShort(msg_read); } // read movement if ((bits & Defines.CM_FORWARD) != 0) { move.forwardmove = MSG.ReadShort(msg_read); } if ((bits & Defines.CM_SIDE) != 0) { move.sidemove = MSG.ReadShort(msg_read); } if ((bits & Defines.CM_UP) != 0) { move.upmove = MSG.ReadShort(msg_read); } // read buttons if ((bits & Defines.CM_BUTTONS) != 0) { move.buttons = (byte)MSG.ReadByte(msg_read); } if ((bits & Defines.CM_IMPULSE) != 0) { move.impulse = (byte)MSG.ReadByte(msg_read); } // read time to run command move.msec = (byte)MSG.ReadByte(msg_read); // read the light level move.lightlevel = (byte)MSG.ReadByte(msg_read); }
public static void MSG_WriteShort(sizebuf_t sb, int c) { byte[] buf; int offset; buf = SZ_GetSpace(sb, 2, out offset); buf[offset] = (byte)(c & 0xff); buf[offset + 1] = (byte)(c >> 8); }
//=========================================================================== public static void Init(sizebuf_t buf, byte[] data, int length) { buf.readcount = 0; buf.data = data; buf.maxsize = length; buf.cursize = 0; buf.allowoverflow = buf.overflowed = false; }
//ok. public static void WriteInt(sizebuf_t sb, int c) { var i = SZ.GetSpace(sb, 4); sb.data[i++] = (byte)(c & 0xff); sb.data[i++] = (byte)(((uint)c >> 8) & 0xff); sb.data[i++] = (byte)(((uint)c >> 16) & 0xff); sb.data[i++] = (byte)(((uint)c >> 24) & 0xff); }
//=========================================================================== public static void SZ_Alloc(sizebuf_t buf, int startsize) { if (startsize < 256) { startsize = 256; } buf.data = new byte[startsize]; buf.maxsize = startsize; buf.cursize = 0; }
public static void WritePos(sizebuf_t sb, float[] pos) { if (!(pos.Length == 3)) { throw new("vec3_t bug"); } MSG.WriteShort(sb, (int)(pos[0] * 8)); MSG.WriteShort(sb, (int)(pos[1] * 8)); MSG.WriteShort(sb, (int)(pos[2] * 8)); }
public static void ReadPos(sizebuf_t msg_read, float[] pos) { if (!(pos.Length == 3)) { throw new("vec3_t bug"); } pos[0] = MSG.ReadShort(msg_read) * (1.0f / 8); pos[1] = MSG.ReadShort(msg_read) * (1.0f / 8); pos[2] = MSG.ReadShort(msg_read) * (1.0f / 8); }
public static void MSG_WriteLong(sizebuf_t sb, int c) { byte[] buf; int offset; buf = SZ_GetSpace(sb, 4, out offset); buf[offset] = (byte)(c & 0xff); buf[offset + 1] = (byte)((c >> 8) & 0xff); buf[offset + 2] = (byte)((c >> 16) & 0xff); buf[offset + 3] = (byte)(c >> 24); }
//should be ok. public static void ReadDir(sizebuf_t sb, float[] dir) { int b; b = MSG.ReadByte(sb); if (b >= Defines.NUMVERTEXNORMALS) { Com.Error(Defines.ERR_DROP, "MSF_ReadDir: out of range"); } Math3D.VectorCopy(Globals.bytedirs[b], dir); }
// had a bug, now its ok. public static void WriteString(sizebuf_t sb, string s) { var x = s; if (s == null) { x = ""; } SZ.Write(sb, Lib.stringToBytes(x)); MSG.WriteByte(sb, 0); //Com.dprintln("MSG.WriteString:" + s.replace('\0', '@')); }
/** * Writes a frame to a client system. */ public static void SV_WriteFrameToClient(client_t client, sizebuf_t msg) { //ptr client_frame_t frame, oldframe; int lastframe; //Com.Printf ("%i . %i\n", new // Vargs().add(client.lastframe).add(sv.framenum)); // this is the frame we are creating frame = client.frames[SV_INIT.sv.framenum & Defines.UPDATE_MASK]; if (client.lastframe <= 0) { // client is asking for a retransmit oldframe = null; lastframe = -1; } else if (SV_INIT.sv.framenum - client.lastframe >= Defines.UPDATE_BACKUP - 3) { // client hasn't gotten a good message through in a long time // Com_Printf ("%s: Delta request from out-of-date packet.\n", // client.name); oldframe = null; lastframe = -1; } else { // we have a valid message to delta from oldframe = client.frames[client.lastframe & Defines.UPDATE_MASK]; lastframe = client.lastframe; } MSG.WriteByte(msg, Defines.svc_frame); MSG.WriteLong(msg, SV_INIT.sv.framenum); MSG.WriteLong(msg, lastframe); // what we are delta'ing from MSG.WriteByte(msg, client.surpressCount); // rate dropped packets client.surpressCount = 0; // send over the areabits MSG.WriteByte(msg, frame.areabytes); SZ.Write(msg, frame.areabits, frame.areabytes); // delta encode the playerstate SV_ENTS.SV_WritePlayerstateToClient(oldframe, frame, msg); // delta encode the entities SV_ENTS.SV_EmitPacketEntities(oldframe, frame, msg); }
public static int ReadByte(sizebuf_t msg_read) { int c; if (msg_read.readcount + 1 > msg_read.cursize) { c = -1; } else { c = msg_read.data[msg_read.readcount] & 0xff; } msg_read.readcount++; return(c); }
public static short ReadShort(sizebuf_t msg_read) { int c; if (msg_read.readcount + 2 > msg_read.cursize) { c = -1; } else { c = (short)((msg_read.data[msg_read.readcount] & 0xff) + (msg_read.data[msg_read.readcount + 1] << 8)); } msg_read.readcount += 2; return((short)c); }
// returns -1 if no more characters are available, but also [-128 , 127] public static int ReadChar(sizebuf_t msg_read) { int c; if (msg_read.readcount + 1 > msg_read.cursize) { c = -1; } else { c = (sbyte)msg_read.data[msg_read.readcount]; } msg_read.readcount++; // kickangles bugfix (rst) return(c); }
public static void MSG_WriteString(sizebuf_t sb, string s) { byte[] buf; if (s == null) { buf = new byte[1]; buf[0] = 0; SZ_Write(sb, buf, 1); } else { buf = new byte[s.Length + 1]; for (int kk = 0; kk < s.Length; kk++) { buf[kk] = (byte)s[kk]; } SZ_Write(sb, buf, s.Length + 1); } }
public static bool GetLoopPacket(int sock, netadr_t net_from, sizebuf_t net_message) { loopback_t loop = loopbacks[sock]; if (loop.send - loop.get > MAX_LOOPBACK) { loop.get = loop.send - MAX_LOOPBACK; } if (loop.get >= loop.send) { return(false); } int i = loop.get & (MAX_LOOPBACK - 1); loop.get++; System.Array.Copy(loop.msgs[i].data, 0, net_message.data, 0, loop.msgs[i].datalen); net_message.cursize = loop.msgs[i].datalen; net_from.Set(net_local_adr); return(true); }
//=========================================================================== public static void SZ_Alloc(sizebuf_t buf, int startsize) { if (startsize < 256) startsize = 256; buf.data = new byte[startsize]; buf.maxsize = startsize; buf.cursize = 0; }
public static void MSG_WriteString(sizebuf_t sb, string s) { byte[] buf; if (s == null) { buf = new byte[1]; buf[0] = 0; SZ_Write(sb, buf, 1); } else { buf = new byte[s.Length + 1]; for (int kk = 0; kk < s.Length; kk++) buf[kk] = (byte)s[kk]; SZ_Write(sb, buf, s.Length + 1); } }
public static void MSG_WriteByte (sizebuf_t sb, int c) { //Debug.WriteLine("MSG_WriteByte " + c); Uint8Array buf; int offset; buf = SZ_GetSpace (sb, 1, out offset); buf[offset] = (byte)c; }
public static void SZ_Write(sizebuf_t buf, byte[] data, int length) { int offset; byte[] dst = SZ_GetSpace(buf, length, out offset); Buffer.BlockCopy(data, 0, dst, offset, length); }
public static void MSG_WriteCoord(sizebuf_t sb, double f) { MSG_WriteShort (sb, (int)(f*8)); }
public static void SZ_Write(sizebuf_t buf, Uint8Array data, int dataofs, int length) { int offset; Uint8Array dst = SZ_GetSpace(buf, length, out offset); Buffer.BlockCopy(data, dataofs, dst, offset, length); }
public static void MSG_WriteString (sizebuf_t sb, string s) { //Debug.WriteLine("MSG_WriteString " + s); Uint8Array buf; if (s == null) { buf = new Uint8Array(1); buf[0] = 0; SZ_Write(sb, buf, 1); } else { buf = new Uint8Array(s.Length + 1); for (int kk = 0; kk < s.Length; kk++) buf[kk] = (byte)s[kk]; SZ_Write(sb, buf, s.Length + 1); } }
public static void MSG_WriteFloat (sizebuf_t sb, double f) { Uint8Array dat; //Debug.WriteLine(string.Format("MSG_WriteFloat {0:F1}", f)); dat = BitConverter.GetBytes((float)f); SZ_Write (sb, dat, 4); }
//=========================================================================== public static void SZ_Alloc (sizebuf_t buf, int startsize) { if (startsize < 256) startsize = 256; buf.data = new Uint8Array(startsize); buf.maxsize = startsize; buf.cursize = 0; }
/* ============================================================================== MESSAGE IO FUNCTIONS Handles byte ordering and avoids alignment errors ============================================================================== */ // // writing functions // public static void MSG_WriteChar(sizebuf_t sb, int c) { byte[] buf; int offset; buf = SZ_GetSpace (sb, 1, out offset); buf[offset] = (byte)c; }
public static void SZ_Free(sizebuf_t buf) { buf.cursize = 0; }
public static void SZ_Clear(sizebuf_t buf) { buf.cursize = 0; }
public static void SZ_Print(sizebuf_t buf, string data) { int len; len = data.Length+1; // byte * cast to keep VC++ happy if (buf.data[buf.cursize - 1] != 0) { int offset; Uint8Array dst = SZ_GetSpace(buf, len, out offset); for (int kk = 0; kk < len; kk++) { if(kk < data.Length) dst[offset + kk] = (byte)data[kk]; // no trailing 0 else dst[offset + kk] = 0; } } else { int offset; Uint8Array dst = SZ_GetSpace(buf, len - 1, out offset); for (int kk = 0; kk < data.Length; kk++) dst[offset - 1 + kk] = (byte)data[kk]; // write over trailing 0 } }
public static void MSG_WriteLong (sizebuf_t sb, int c) { //Debug.WriteLine("MSG_WriteLong " + c); Uint8Array buf; int offset; buf = SZ_GetSpace(sb, 4, out offset); buf[offset] = (byte)(c & 0xff); buf[offset + 1] = (byte)((c >> 8) & 0xff); buf[offset + 2] = (byte)((c >> 16) & 0xff); buf[offset + 3] = (byte)(c >> 24); }
public static void MSG_WriteFloat(sizebuf_t sb, double f) { byte[] dat; dat = BitConverter.GetBytes((float)f); SZ_Write (sb, dat, 4); }
public static void MSG_WriteShort (sizebuf_t sb, int c) { //Debug.WriteLine("MSG_WriteShort " + c); Uint8Array buf; int offset; buf = SZ_GetSpace(sb, 2, out offset); buf[offset] = (byte)(c & 0xff); buf[offset + 1] = (byte)(c >> 8); }
public static void MSG_WriteCoord (sizebuf_t sb, double f) { //Debug.WriteLine(string.Format("MSG_WriteCoord {0:F1}", f)); MSG_WriteShort(sb, (int)(f * 8)); }
static Uint8Array SZ_GetSpace (sizebuf_t buf, int length, out int offset) { Uint8Array data; if (buf.cursize + length > buf.maxsize) { if (!buf.allowoverflow) sys_linux.Sys_Error ("SZ_GetSpace: overflow without allowoverflow set"); if (length > buf.maxsize) sys_linux.Sys_Error("SZ_GetSpace: " + length + " is > full buffer size"); buf.overflowed = true; console.Con_Printf ("SZ_GetSpace: overflow"); SZ_Clear (buf); } data = buf.data; offset = buf.cursize; buf.cursize += length; return data; }
public static void MSG_WriteAngle(sizebuf_t sb, double f) { //Debug.WriteLine(string.Format("MSG_WriteAngle {0:F1}", f)); MSG_WriteByte(sb, ((int)f * 256 / 360) & 255); }
public static void MSG_WriteAngle(sizebuf_t sb, double f) { MSG_WriteByte (sb, ((int)f*256/360) & 255); }