//new player for me public static void sendCreateSelf() { // ====== 走pb 发送 ====== or 直接发送 ===== #if PROTOBUFF Rqst_CreateSelf data = new Rqst_CreateSelf(); data.Account = "" + 1000 + UnityEngine.Random.Range(1, 100); data.Password = "" + 123456; DebugTool.LogYellow("[TCP] sendCreateSelf 请求创建我方角色" + data.Account); connect_TCP.Send <Rqst_CreateSelf> ((short)NetCode.CreateSelf, data); GameProxy.mine = new PlayerData(); GameProxy.mine.userid = data.Account; #else DebugTool.LogError("已经不许使用自写编码,请检察 UnityEngine里是否添加宏: PROTOBUFF"); //===== 直接发送 自写的编码,无法对付可变长byte[]====== SendBuffer.ResetIndex(); SendBuffer.writeUshort(10000); //cmd, payload as below var r = UnityEngine.Random.Range(1, 100); SendBuffer.writeInt(9999 + r); //accountid // SendBuffer.writeString ("gaohebing" + UnityEngine.Random.Range (0, 100));//nickname // SendBuffer.writeInt (1);//level SendBuffer.writeInt(123); //password var arr = SendBuffer.GetBytes_HasData(); byte[] sendbuff = new byte[arr.Length]; Array.Copy(arr, sendbuff, sendbuff.Length); connect_TCP.SendBytes(sendbuff); #endif }
public static void sendHeartBeating() //msg.PlayerInfo playerinfo = ) { #if PROTOBUFF DebugTool.LogYellow("[TCP] sendHeartBeating"); var data = new Rqst_HeartBeating(); data.Status = 0; // data.Player = playerinfo; connect_TCP.Send <Rqst_HeartBeating> ((short)NetCode.BeatHeart, data); #else SendBuffer.ResetIndex(); SendBuffer.writeUshort(110); //cmd, payload as below SendBuffer.writeInt(1); // payload byte[] sendbuff = SendBuffer.GetBytes_HasData(); connect_TCP.SendBytes(sendbuff); #endif }
private void EndSend(IAsyncResult ar) { lock (SendBuffer) { if (IsConnected)//User disconnect tcpConnection proactively { int length = 0; try { var socket = ar.AsyncState as System.Net.Sockets.Socket; AssertThat.IsNotNull(socket, "Socket is null when end send"); length = socket.EndSend(ar); } catch (Exception e) { throw new Exception(e.ToString()); } byte[] sendBytes = SendBuffer.Read(length); SendBuffer.ResetIndex(); SocketSendEvent(sendBytes); } } }