public string Read_str() { ushort len = Read_ushort(); byte[] bytes = Read_bytes(len); return(ByteAr_b.b2str(bytes));; }
private void ReceiveBodyCallback(IAsyncResult ar) { try { OnInfo("--------------------start ReceiveBodyCallback"); if (!this.socket.Connected) { return; } int count = this.socket.EndReceive(ar); if (count == 0) { OnError("ReceiveBodyCallback:socket receive 0"); return; } this.readBytes += count; if (this.readBytes < this.bodyBuf.Length) { this.BeginReceiveBody(this.readBytes); return; } byte[] bytes_cmdid = new byte[2]; byte[] bytes_seq = new byte[4]; byte[] bytes_body = new byte[bodyBuf.Length - 6]; ByteAr_b.PickBytes(this.bodyBuf, bytes_cmdid.Length, bytes_cmdid, 0); ByteAr_b.PickBytes(this.bodyBuf, bytes_seq.Length, bytes_seq, 2); ByteAr_b.PickBytes(this.bodyBuf, bytes_body.Length, bytes_body, bytes_cmdid.Length + bytes_seq.Length); if (ByteAr_b.b2us(bytes_cmdid) == 0xa10b) { PswCode = ByteAr_b.b2i(bytes_body); } else { this.OnReceived(this.bodyBuf); /* Pt pt = ptMng.GetInstance().MakePt(ByteAr_b.b2us(bytes_cmdid),ByteAr_b.b2ui(bytes_seq), bytes_body); * * if (pt == null) * OnError("Receive unknow pt pt_id = " + ByteAr_b.b2us(bytes_cmdid)); * else * { * pt.seq = ByteAr_b.b2ui(bytes_seq); * this.OnReceived(pt); * }*/ } this.BeginReceiveHeader(0); } catch (Exception e) { OnError("ReceiveBodyCallback:socket catch" + e.Message); } }
public byte[] Read_bytes(int len) { if (this.data.Length < read_indx + len) { return(null); } byte[] getbytes = new byte[len]; ByteAr_b.PickBytes(this.data, len, getbytes, read_indx); read_indx += len; return(getbytes); }
public void Send(Pt pt) { if (pt == null) { OnInfo("--------------------error pt"); return; } byte[] data = pt.toBinary(); OnInfo("--------------------start send:" + (data.Length + 10)); if ((this.socket == null) || (!this.socket.Connected)) { OnInfo("--------------------send,no net"); return; } int allLen = data.Length + 10; allLen = allLen ^ PswCode; byte[] bytesLen = ByteAr_b.i2b(allLen); byte[] bytesPt = ByteAr_b.s2b(pt.GetID()); byte[] bytesSeq = ByteAr_b.i2b(pt.seq); byte[] allData = new byte[data.Length + 10]; Array.Copy(bytesLen, 0, allData, 0, bytesLen.Length); Array.Copy(bytesPt, 0, allData, bytesLen.Length, bytesPt.Length); Array.Copy(bytesSeq, 0, allData, bytesLen.Length + bytesPt.Length, bytesSeq.Length); Array.Copy(data, 0, allData, bytesLen.Length + bytesPt.Length + bytesSeq.Length, data.Length); lock (send_buf) { if (send_len == 0) { allData.CopyTo(send_buf, send_len); send_len = send_len + allData.Length; BeginSend(); } else { allData.CopyTo(send_buf, send_len); send_len = send_len + allData.Length; } } }
public static void Update() { if (unHandMsgList != null) { while (unHandMsgList.Count > 0) { Pt pt = null; if (unHandMsgList.Count > 0) { lock (unHandMsgList) { byte[] bts = unHandMsgList.Dequeue(); byte[] bytes_cmdid = new byte[2]; byte[] bytes_seq = new byte[4]; byte[] bytes_body = new byte[bts.Length - 6]; ByteAr_b.PickBytes(bts, bytes_cmdid.Length, bytes_cmdid, 0); ByteAr_b.PickBytes(bts, bytes_seq.Length, bytes_seq, 2); ByteAr_b.PickBytes(bts, bytes_body.Length, bytes_body, bytes_cmdid.Length + bytes_seq.Length); ushort head = ByteAr_b.b2us(bytes_cmdid); if (msgStructDic.ContainsKey(head)) { pt = msgStructDic[head].createNew(); pt.seq = ByteAr_b.b2ui(bytes_seq); pt.fromBinary(bytes_body); } } } if (pt != null) { if (screen_Debug_Log_S2C) { GameSys.LogInternal("[S] to [C] :" + Convert.ToString(pt.GetID(), 16), false); } if (editor_Debug_Log_S2C) { Debug.Log("[S] to [C] :" + Convert.ToString(pt.GetID(), 16)); } MsgHander.ProcComand(pt); } } } }
private void ReceiveHeaderCallback(IAsyncResult ar) { try { OnInfo("--------------------start ReceiveHeaderCallback"); if (!this.socket.Connected) { OnError("ReceiveHeaderCallback socket not connected"); return; } int count = this.socket.EndReceive(ar); if (count == 0) { OnError("ReceiveHeaderCallback:socket receive 0"); return; } this.readBytes += count; if (this.readBytes < this.headerBuf.Length) { this.BeginReceiveHeader(this.readBytes); return; } int dataSize = ByteAr_b.b2i(headerBuf); dataSize = dataSize ^ PswCode; int bodySize = dataSize - this.headerBuf.Length; this.bodyBuf = new byte[bodySize]; this.BeginReceiveBody(0); } catch (Exception e) { OnError("ReceiveHeaderCallback:socket catch" + e.Message); } }
public int Read_int() { byte[] bytes = Read_bytes(4); return(ByteAr_b.b2i(bytes)); }
public bool write_long(ulong data) { return(write_bytes(ByteAr_b.l2b(data))); }
public bool write_byte(byte data) { return(write_bytes(ByteAr_b.b2b(data))); }
public bool write_short(short data) { return(write_bytes(ByteAr_b.s2b(data))); }
public byte Read_byte() { byte[] bytes = Read_bytes(1); return(ByteAr_b.b2b(bytes)); }
public bool write_float(float data) { return(write_bytes(ByteAr_b.f2b(data))); }
public uint Read_uint() { byte[] bytes = Read_bytes(4); return(ByteAr_b.b2ui(bytes)); }
public ushort Read_ushort() { byte[] bytes = Read_bytes(2); return(ByteAr_b.b2us(bytes)); }
public bool write_int(uint data) { return(write_bytes(ByteAr_b.i2b(data))); }
public ulong Read_ulong() { byte[] bytes = Read_bytes(8); return(ByteAr_b.b2ul(bytes)); }
public long Read_long() { byte[] bytes = Read_bytes(8); return(ByteAr_b.b2l(bytes)); }
public float Read_float() { byte[] bytes = Read_bytes(4); return(ByteAr_b.b2f(bytes)); }
public bool write_str(string str) { return(write_bytes(ByteAr_b.str2b(str))); }