public void SendMsg_RequestClientState(INetworkSocket socket)
        {
            string msgData = TransferProtocol.Msg_RequestClientState();

            byte[] bytes = BytesFromString(msgData);
            socket.Send(new Packet(bytes, 0, bytes.Count(), false));
        }
Example #2
0
        public override ValueTask <FlushResult> FlushAsync(CancellationToken cancellationToken)
        {
            try
            {
                while (!_buffer.IsEmpty)
                {
                    var memory = _buffer.GetOccupiedMemory();

                    _logger.LogTrace("TODO: SendStarting {bytes}", memory.Length);
                    var bytes = _socket.Send(memory);
                    _logger.LogTrace("TODO: SendComplete {bytes}", bytes);

                    _buffer.ConsumeOccupiedMemory(bytes);
                }
            }
            catch (Exception ex)
            {
                _logger.LogTrace(ex, "TODO: SendError");

                // Return FlushResult.IsCompleted true from now on
                // because we assume any write exceptions are not temporary
                _isCompleted = true;
#if NETSTANDARD2_0
                FireReaderCompleted(ex);
#endif
            }

            return(new ValueTask <FlushResult>(new FlushResult(
                                                   isCanceled: IsCanceled,
                                                   isCompleted: IsCompleted)));
        }
Example #3
0
        private void SendMessageToSocket(string sendText, INetworkSocket socket)
        {
            byte[] bytes  = BytesFromString(sendText, "gb2312");
            Packet packet = new Packet(bytes, 0, bytes.Count(), false);

            socket.Send(packet);
        }
Example #4
0
 /// <summary>
 /// Receive callback
 /// </summary>
 /// <param name="socket">client socket</param>
 /// <param name="receivedPacket">received packet</param>
 public void OnReceived(INetworkSocket socket, Packet receivedPacket)
 {
     lock (m_generalLock)
     {
         if (socket == m_socket1)
         {
             m_socket2.Send(receivedPacket);
         }
         else
         {
             m_socket1.Send(receivedPacket);
         }
     }
 }
Example #5
0
        public override ValueTask <FlushResult> FlushAsync(CancellationToken cancellationToken)
        {
            try
            {
                while (!_buffer.IsEmpty)
                {
                    var memory = _buffer.GetOccupiedMemory();

                    if (_options.HighVolumeLogging)
                    {
                        _logger.WriteStarting(_connection.ConnectionId, (int)memory.Length);
                    }

                    var bytes = _socket.Send(memory);

                    if (_options.HighVolumeLogging)
                    {
                        _logger.WriteSucceeded(_connection.ConnectionId, bytes);
                    }

                    _buffer.ConsumeOccupiedMemory(bytes);
                }
            }
            catch (TaskCanceledException)
            {
                _logger.WriteCanceled(_connection.ConnectionId);
                _isCanceled = true;
            }
            catch (Exception ex)
            {
                _logger.WriteFailed(_connection.ConnectionId, ex);

                // Return FlushResult.IsCompleted true from now on
                // because we assume any write exceptions are not temporary
                _isCompleted = true;
#if NETSTANDARD2_0
                FireReaderCompleted(ex);
#endif
            }

            return(new ValueTask <FlushResult>(new FlushResult(
                                                   isCanceled: IsCanceled,
                                                   isCompleted: IsCompleted)));
        }
        private void FlushBufferToSocket()
        {
            try
            {
                while (!_buffer.IsEmpty)
                {
                    var memory = _buffer.GetOccupiedMemory();

                    if (_options.HighVolumeLogging)
                    {
                        _logger.WriteStarting(_connection.ConnectionId, (int)memory.Length);
                    }

                    var bytes = _socket.Send(memory);

                    if (_options.HighVolumeLogging)
                    {
                        _logger.WriteSucceeded(_connection.ConnectionId, bytes);
                    }

                    _buffer.ConsumeOccupiedMemory(bytes);
                }
            }
            catch (TaskCanceledException)
            {
                _logger.WriteCanceled(_connection.ConnectionId);
                _isCanceled = true;
            }
            catch (Exception ex)
            {
                _logger.WriteFailed(_connection.ConnectionId, ex);

                // Return FlushResult.IsCompleted true from now on
                // because we assume any write exceptions are not temporary
                _isCompleted = true;
            }
        }
 public void AddSocket(INetworkSocket socket)
 {
     lock (m_generalLock)
     {
         if (MaxStreamCountPerSocket != SocketCount.Infinite && m_clientSet.Count > MaxStreamCountPerSocket)
         {
             socket.Disconnect();
             return;
         }
         m_clientSet.Add(socket);
     }
     CurSocketCount++;
     ((IocpTcpSocket)socket).CallBackObj = this;
     ParallelPacket sendPacket = new ParallelPacket(-1, ParallelPacketType.READY, null);
     socket.Send(sendPacket.PacketRaw);
     lock (m_sendLock)
     {
         m_pendingClientSet.Add(socket);
         if (m_pendingClientSet.Count > 0 && (m_errorPacketSet.Count > 0 || m_packetQueue.Count > 0))
             m_sendReadyEvent.SetEvent();
     }
 }
 public void AddSocket(INetworkSocket socket)
 {
     m_clientSet.Add(socket);
     CurSocketCount++;
     ((IocpTcpSocket)socket).CallBackObj = this;
     ParallelPacket sendPacket = new ParallelPacket(getCurPacketSequence(), ParallelPacketType.READY, null);
     socket.Send(sendPacket.GetPacketRaw());
     lock (m_sendLock)
     {
         m_pendingClientSet.Add(socket);
         if (m_pendingClientSet.Count > 0 && (m_errorPacketSet.Count > 0 || m_packetQueue.Count > 0))
             m_sendReadyEvent.SetEvent();
     }
 }
Example #9
0
 public void SendData(INetworkSocket socket, string strData)
 {
     byte[] bytes = BytesFromString(strData);
     Program.AddLog(socket.IPInfo.IPAddress + " [Send] " + strData);
     socket.Send(new Packet(bytes, 0, bytes.Count(), false));
 }
 /// <summary>
 /// NewConnection callback
 /// </summary>
 /// <param name="socket">client socket</param>
 public void OnNewConnection(INetworkSocket socket)
 {
     // Request Identity of new connected socket
     ParallelPacket sendPacket = new ParallelPacket(-1, ParallelPacketType.IDENTITY_REQUEST,null);
     socket.Send(sendPacket.GetPacketRaw());
 }