Ejemplo n.º 1
0
        /// <summary>
        /// 发送字节流
        /// </summary>
        /// <param name="remoteEndPoint">udp远程地址,如果为null表示tcp发送</param>
        /// <param name="buffer">字节流</param>
        /// <returns>发送结果</returns>
        private SocketResult SendCore(IPEndPoint remoteEndPoint, List <byte> buffer)
        {
            if (Socket == null)
            {
                return(SocketResult.Disconnection);
            }

            if (buffer == null || buffer.Count == 0)
            {
                return(SocketResult.SendFailed);
            }
            byte[] temp = buffer.ToArray();
            TransmitSize += Convert.ToUInt32(temp.Length);
            try
            {
                if (remoteEndPoint == null ||
                    remoteEndPoint.Address.Equals(IPAddress.Any) && remoteEndPoint.Port == 0)
                {
                    Log($"{Socket.Handle} - {temp.Length} {ByteConvert.ToHex(temp)}");
                    int written = 0;
                    while (written != temp.Length)
                    {
                        int n;
                        if ((n = Socket.Send(temp, written, temp.Length - written, SocketFlags.None)) <= 0)
                        {
                            return(SocketResult.SendFailed);
                        }
                        written += n;
                    }
                }
                else
                {
                    Log($"{Socket.Handle} {remoteEndPoint} - {temp.Length} {ByteConvert.ToHex(temp)}");
                    int written = 0;
                    while (written != temp.Length)
                    {
                        int n;
                        if ((n = Socket.SendTo(temp, written, temp.Length - written, SocketFlags.None, remoteEndPoint)) <= 0)
                        {
                            return(SocketResult.SendFailed);
                        }
                        written += n;
                    }
                }
                return(SocketResult.Success);
            }
            catch (SocketException)
            {
                return(SocketResult.SendFailed);
            }
            catch (ObjectDisposedException)
            {
                return(SocketResult.SendFailed);
            }
        }
Ejemplo n.º 2
0
 public string Bytes_To_Hexadecimal_String(byte[] bytes)
 {
     return(ByteConvert.ToHex(bytes));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 处理接收字节流
        /// </summary>
        /// <param name="buffer">字节流</param>
        /// <param name="size">字节流长度</param>
        /// <param name="remoteEndPoint">远程地址</param>
        public void Handle(byte[] buffer, int size, IPEndPoint remoteEndPoint = null)
        {
            ReceiveSize += Convert.ToUInt32(size);

            Log(remoteEndPoint == null
                ? $"{Socket.Handle} + {size} {ByteConvert.ToHex(buffer, size)}"
                : $"{Socket.Handle} {remoteEndPoint} + {size} {ByteConvert.ToHex(buffer, size)}");

            _residueBuffer.AddRange(buffer.Take(size));

            int offset = 0;

            do
            {
                SocketPack packet = _handler.Unpack(Socket, remoteEndPoint, _residueBuffer, offset);
                if (packet.Result == AnalysisResult.Full)
                {
                    ReceivedEventArgs args = new ReceivedEventArgs
                    {
                        Socket         = Socket,
                        RemoteEndPoint = remoteEndPoint,
                        Channel        = this,
                        Buffer         = _residueBuffer.GetRange(packet.Offset, packet.Size),
                        ProtocolId     = packet.ProtocolId,
                        TimeStamp      = packet.TimeStamp
                    };
                    long timeStamp = TimeStampConvert.ToUtcTimeStamp();
                    lock (_lockObj)
                    {
                        LinkedListNode <ResponseItem> node = _responseItems.First;
                        while (node != null)
                        {
                            if (node.Value.TimeStamp >= timeStamp)
                            {
                                if (node.Value.Match(args))
                                {
                                    if (node.Value.Action == null)
                                    {
                                        node.Value.ReceiveBuffer?.AddRange(args.Buffer);
                                        node.Value.ResetEvent.Set();
                                    }
                                    else
                                    {
                                        node.Value.Action(args);
                                    }
                                    LinkedListNode <ResponseItem> nextNode = node.Next;
                                    _responseItems.Remove(node);
                                    node = nextNode;
                                }
                                else
                                {
                                    node = node.Next;
                                }
                            }
                            else
                            {
                                LinkedListNode <ResponseItem> nextNode = node.Next;
                                _responseItems.Remove(node);
                                node = nextNode;
                            }
                        }
                    }


                    Received?.Invoke(this, args);
                }
                else if (packet.Result == AnalysisResult.Half)
                {
                    _residueBuffer.RemoveRange(0, offset);
                    return;
                }
                offset += packet.Offset + packet.Size;
            } while (offset < _residueBuffer.Count);
            _residueBuffer.Clear();
        }