public void ExitGame() { MsgHead msg = new MsgHead(); msg.m_type = (uint)EventType.EXIT; msg.m_len = 0; msg.m_usrid = mainPlayerID; msg.m_errID = 0; byte[] temp = new byte[MsgHead.headsize()]; msg.EnCode(temp, 0); m_Net.Send(temp, MsgHead.headsize(), 0); m_Net.DisConnect(); Invoke(nameof(Application.Quit), 1f); }
public int SendMessage <T>(T _protobuffer, EventType mode) where T : global::Google.Protobuf.IMessage <T>, new() { Debug.Log("sendmessage type " + mode); m_msgHeadSend.setType(mode); byte[] tmp = Google.Protobuf.MessageExtensions.ToByteArray(_protobuffer); m_msgHeadSend.m_len = (UInt32)tmp.Length; Debug.Log("protobuf size is " + _protobuffer.CalculateSize()); Debug.Log("head.type is" + m_msgHeadSend.m_type + "head.id is" + m_msgHeadSend.m_usrid + "protobuf size is " + m_msgHeadSend.m_len); m_msgHeadSend.EnCode(m_sendbuf, 0); tmp.CopyTo(m_sendbuf, MsgHead.headsize()); if (Send(m_sendbuf, m_msgHeadSend.m_len + MsgHead.headsize()) < 0) { return(-1); } return(0); }
public void RecvMessage() { // int left = _len; // int n = 0; // int off = _offset; // Debug.Log("recv bytes : " + _len + "offset :" + off + "data length is" + _data.Length); // while (left > 0) { // if ((n = m_socket.Receive(_data, off, left, SocketFlags.None, out m_errorCode)) < 0) { // if (m_errorCode == SocketError.WouldBlock // || m_errorCode == SocketError.Interrupted // || m_errorCode == SocketError.AlreadyInProgress) { // continue; // } else { // Debug.Log("send data failed"); // return -1; // } // } // left -= n; // off += n; // } // Debug.Log( " recv " + _len + " data sucess"); Debug.Log("Net Recv start, Flag = " + FLAG); while (FLAG) { if (MsgHead.headsize() > (m_tail - m_head)) { if (recv() < 0) { Thread.Sleep(1000); } } else { m_msgHeadRecv.DeCode(m_recvbuf, m_head); if (m_msgHeadRecv.m_len > (m_tail - m_head - MsgHead.headsize())) { if (recv() < 0) { Thread.Sleep(1000); } } else { m_head += (int)MsgHead.headsize(); m_message.m_type = m_msgHeadRecv.getType(); m_message.m_usrid = m_msgHeadRecv.getId(); m_message.m_datalen = m_msgHeadRecv.m_len; m_message.m_data = new byte [m_msgHeadRecv.m_len]; Array.Copy(m_recvbuf, m_head, m_message.m_data, 0, m_msgHeadRecv.m_len); m_head += (int)m_msgHeadRecv.m_len; Debug.Log("enqueue, type is" + m_message.m_type + "id is " + m_message.m_usrid); GameManager.g_mQueue.Enqueue(m_message); } } Thread.Sleep(5); } Debug.Log("Net Recv end, Flag = " + FLAG); }