void SendCallback(IAsyncResult asyncResult) { if (isConnected == false) { return; } AsyncSendStateObject asyncSendData = (AsyncSendStateObject)asyncResult; int sendSize = 0; try { sendSize = asyncSendData.socket.EndSend(asyncResult); } catch (SocketException ex) { ProcessException(ex); } //보낼게 다안보내 졌을 경우 if (sendSize < asyncSendData.sendSize) { socket.BeginSend( asyncSendData.buffer, sendSize, asyncSendData.buffer.Length - sendSize, SocketFlags.Truncated, SendCallback, asyncSendData); Debug.Log("Send additionally"); } Debug.Log("Send Complete"); }
public void SendPacket <T>(T data, PacketId pktID) { if (isConnected == false) { Debug.Log("Not Connected! Send Packet Failed"); return; } AsyncSendStateObject asyncSendData = new AsyncSendStateObject(socket); unsafe { #region make empty packet string dataJson = JsonUtility.ToJson(data); int packetID = (int)pktID; int bodysize = dataJson.Length + 1; //Debug.Log("bodysize =" + bodysize); asyncSendData.buffer = new byte[NetworkProperty.PacketHeaderSize + bodysize + 1]; asyncSendData.sendSize = NetworkProperty.PacketHeaderSize + bodysize; #endregion //Debug.Log("MakePacket"); #region pack data byte *packetIdByte = (byte *)&packetID; for (int i = 0; i < sizeof(int); ++i) { asyncSendData.buffer[i] = packetIdByte[i]; } byte *bodySizeByte = (byte *)&bodysize; for (int i = 0; i < sizeof(int); ++i) { asyncSendData.buffer[i + sizeof(int)] = bodySizeByte[i]; } char[] bodyChar = dataJson.ToCharArray(); for (int i = 0; i < bodysize - 1; ++i) { asyncSendData.buffer[i + NetworkProperty.PacketHeaderSize] = (byte)bodyChar[i]; } #endregion } try { //Debug.Log("SendBeginPacket"); socket.BeginSend( asyncSendData.buffer, 0, asyncSendData.buffer.Length, SocketFlags.None, sendCallback, asyncSendData); Debug.Log("Send id = packetID = " + pktID + "bodySize = " + asyncSendData.buffer.Length); } catch (SocketException ex) { ProcessException(ex); } //Debug.Log("SendEndPacket"); }