public static UnPackageObject UnPackage(byte[] totalBuffer) { UnPackageObject unPackageObject = new UnPackageObject(); //读取消息总长 byte[] totalLengthBytes = new byte[4]; Array.Copy(totalBuffer, 0, totalLengthBytes, 0, 4); uint totalLength = ByteConvert.Bytes2UInt(totalLengthBytes); unPackageObject.totalLength = totalLength; //读取话题 byte[] topicBytes = new byte[4]; Array.Copy(totalBuffer, 4, topicBytes, 0, 4); unPackageObject.ope = (MsgOperation)ByteConvert.Bytes2UInt(topicBytes); //读取消息ID byte[] msgIdBytes = new byte[4]; Array.Copy(totalBuffer, 8, msgIdBytes, 0, 4); unPackageObject.msgId = ByteConvert.Bytes2UInt(msgIdBytes); //读取消息体 byte[] bodyBytes = new byte[totalLength - 12]; Array.Copy(totalBuffer, 12, bodyBytes, 0, totalLength - 12); unPackageObject.body = ByteConvert.ByteToObj(bodyBytes, bodyBytes.Length); return(unPackageObject); }
/// <summary> /// 组装将要发送的数据包 含包头 /// </summary> /// <param name="data">消息体</param> /// <param name="msgId">消息ID</param> /// <param name="msgOperation">操作类型</param> /// <returns></returns> public static byte[] GetPackage(object data, uint msgId, MsgOperation msgOperation) { byte[] byteData = ByteConvert.ObjToByte(data); byte[] sendData = new byte[12 + byteData.Length]; uint totalLength = (uint)(12 + byteData.Length); Array.Copy(ByteConvert.UInt2Bytes(totalLength), 0, sendData, 0, 4); Array.Copy(ByteConvert.UInt2Bytes((uint)msgOperation), 0, sendData, 4, 4); Array.Copy(ByteConvert.UInt2Bytes(msgId), 0, sendData, 8, 4); Array.Copy(byteData, 0, sendData, 12, byteData.Length); return(sendData); }
private void ReceiveCallback(IAsyncResult ar) { try { // Retrieve the state object and the client socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket client = state.workSocket; // Read data from the remote device. int bytesRead = client.EndReceive(ar); Console.WriteLine($"[{client.LocalEndPoint.ToString()}]接收到[{bytesRead}]字节数据"); if (bytesRead > 0) { //不管是不是第一次接收,只要接收字节总长度小于4 if (state.readBufferLength + bytesRead < 4) { Array.Copy(state.buffer, 0, state.totalBuffer, state.readBufferLength, bytesRead); state.readBufferLength += bytesRead; //继续接收 state.buffer = new byte[StateObject.BufferSize]; client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); return; } //已读长度如果小于4 要先获取总的消息长度 if (state.readBufferLength < 4) { //先拼出消息体长度 byte[] totalLengthBytes = new byte[4]; if (state.readBufferLength > 0) { Array.Copy(state.totalBuffer, 0, totalLengthBytes, 0, state.readBufferLength); } int readLength = 4 - state.readBufferLength; Array.Copy(state.buffer, 0, totalLengthBytes, state.readBufferLength, readLength); state.totalLength = ByteConvert.Bytes2UInt(totalLengthBytes); state.totalBuffer = new byte[state.totalLength]; } //还是没读完 if (state.totalLength > state.readBufferLength + bytesRead) { Array.Copy(state.buffer, 0, state.totalBuffer, state.readBufferLength, bytesRead); state.readBufferLength += bytesRead; //继续接收 state.buffer = new byte[StateObject.BufferSize]; client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); return; } //已经够读完了 Array.Copy(state.buffer, 0, state.totalBuffer, state.readBufferLength, state.totalLength - state.readBufferLength); Console.WriteLine($"[{client.LocalEndPoint.ToString()}]接收包总长度[{state.totalLength}]字节数据"); UnPackageObject unPackageObject = MQPackageHelper.UnPackage(state.totalBuffer); //接收成功事件不为null时触发 if (msgReceiveEvent != null) { Task.Run(() => { msgReceiveEvent(unPackageObject); }); } //接收完完整的一个包,休息200毫秒 间隔太短可能导致实体类的值被覆盖 Thread.Sleep(200); //继续接收这个客户端发来的下一个消息 StateObject nextState = new StateObject(); nextState.workSocket = client; //当超出当前接收内容时触发 if (state.readBufferLength + bytesRead > state.totalLength) { //这里说明一个完整的消息体接收完了,有可能还会读到下一次的消息体 byte[] newTotalBuffer = new byte[state.readBufferLength + bytesRead - state.totalLength]; Array.Copy(state.buffer, state.totalLength - state.readBufferLength, newTotalBuffer, 0, state.readBufferLength + bytesRead - state.totalLength); //要重新建一个对象,不然会影响到异步执行后续处理方法 nextState.readBufferLength = newTotalBuffer.Length; if (nextState.readBufferLength >= 4) { //拼出消息体长度 byte[] totalLengthBytes = new byte[4]; Array.Copy(newTotalBuffer, 0, totalLengthBytes, 0, 4); nextState.totalLength = ByteConvert.Bytes2UInt(totalLengthBytes); nextState.totalBuffer = new byte[nextState.totalLength]; } Array.Copy(newTotalBuffer, 0, nextState.totalBuffer, 0, nextState.readBufferLength); } client.BeginReceive(nextState.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), nextState); receiveDone.Set(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
private void ReadCallback(IAsyncResult ar) { Socket handler = null; try { // Retrieve the state object and the handler socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; handler = state.workSocket; // Read data from the client socket. int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { //不管是不是第一次接收,只要接收字节总长度小于4 if (state.readBufferLength + bytesRead < 4) { Array.Copy(state.buffer, 0, state.totalBuffer, state.readBufferLength, bytesRead); state.readBufferLength += bytesRead; //继续接收 state.buffer = new byte[StateObject.BufferSize]; handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); return; } //已读长度如果小于4 要先获取总的消息长度 if (state.readBufferLength < 4) { //先拼出消息体长度 byte[] totalLengthBytes = new byte[4]; if (state.readBufferLength > 0) { Array.Copy(state.totalBuffer, 0, totalLengthBytes, 0, state.readBufferLength); } int readLength = 4 - state.readBufferLength; Array.Copy(state.buffer, 0, totalLengthBytes, state.readBufferLength, readLength); state.totalLength = ByteConvert.Bytes2UInt(totalLengthBytes); state.totalBuffer = new byte[state.totalLength]; } //还是没读完 if (state.totalLength > state.readBufferLength + bytesRead) { Array.Copy(state.buffer, 0, state.totalBuffer, state.readBufferLength, bytesRead); state.readBufferLength += bytesRead; //继续接收 state.buffer = new byte[StateObject.BufferSize]; handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); return; } //已经够读完了 Array.Copy(state.buffer, 0, state.totalBuffer, state.readBufferLength, state.totalLength - state.readBufferLength); Task.Run(() => { Handle(state); }); //继续接收这个客户端发来的下一个消息 StateObject nextState = new StateObject(); nextState.workSocket = handler; if (state.readBufferLength + bytesRead > state.totalLength) { //这里说明一个完整的消息体接收完了,有可能还会读到下一次的消息体 byte[] newTotalBuffer = new byte[state.readBufferLength + bytesRead - state.totalLength]; Array.Copy(state.buffer, state.totalLength - state.readBufferLength, newTotalBuffer, 0, state.readBufferLength + bytesRead - state.totalLength); //要重新建一个对象,不然会影响到异步执行后续处理方法 nextState.readBufferLength = newTotalBuffer.Length; if (nextState.readBufferLength >= 4) { //拼出消息体长度 byte[] totalLengthBytes = new byte[4]; Array.Copy(newTotalBuffer, 0, totalLengthBytes, 0, 4); nextState.totalLength = ByteConvert.Bytes2UInt(totalLengthBytes); nextState.totalBuffer = new byte[nextState.totalLength]; } Array.Copy(newTotalBuffer, 0, nextState.totalBuffer, 0, nextState.readBufferLength); } handler.BeginReceive(nextState.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), nextState); } } catch (SocketException ex) { Console.WriteLine(ex.ToString()); if (ex.ErrorCode == 10054)//断开连接了 { if (handler == null) { return; } //登录状态移除 loginDic.TryRemove(handler, out bool loginResult); Task.Run(() => { foreach (var item in subscribeListSocket) { //队列移除 QueueHelper.Remove(ref subscribeListSocket, item.Key, handler); } }); } } }