Пример #1
0
        private void SendBytes(bool isUDP, byte[] data)
        {
            try
            {
                if (!OwningConnection.IsConnected)
                {
                    return;
                }

                if (isUDP)
                {
                    SocketDebug(777);
                    OwningConnection.MyUDPSocket.SendTo(data, OwningConnection.UDPSendTarget);
                }
                else
                {
                    SocketDebug(3);
                    OwningConnection.MyTCPSocket.Blocking = true;
                    OwningConnection.MyTCPSocket.Send(data);
                    SocketDebug(4);
                }

                OwningConnection.SentBytes(data);
                OwningConnection.PacketSent();
            }
            catch (Exception e)
            {
                Log.LogMsg("Error SendBytes. " + e.Message);
                OwningConnection.KillConnection("Send error. " + e.Message);
            }
        }
        private void SendBytes(bool isUDP, byte[] data)
        {
            try
            {
                SocketDebug(10);
                if (!OwningConnection.IsConnected)
                {
                    SocketDebug(11);
                    Log.LogMsg("DuplexBlockingTransit can't send bytes. OwningConnection is not connected.");
                    return;
                }

                if (isUDP)
                {
                    SocketDebug(12);
                    OwningConnection.MyUDPSocket.SendTo(data, OwningConnection.UDPSendTarget);
                    // Log.LogMsg("Testy UDP SEND COMPLETE. Is owning connection still alive? " + OwningConnection.IsAlive);
                }
                else
                {
                    //OwningConnection.MyTCPSocket.Blocking = true;
                    SocketDebug(15);
                    OwningConnection.MyTCPSocket.Send(data);
                    SocketDebug(16);
                }

                SocketDebug(17);
                OwningConnection.SentBytes(data);
                OwningConnection.PacketSent();
                SocketDebug(18);
            }
            catch (Exception e)
            {
                Log.LogMsg("Error SendBytes. " + e.Message);
                OwningConnection.KillConnection("Send error. " + e.Message);
            }
        }
Пример #3
0
        /// <summary>
        /// Gets called when a send operation resolves.
        /// </summary>
        private void OnSendResolved(SocketAsyncEventArgs args, SockState state)
        {
            //// Log.LogMsg("Testy 13");
            try
            {
                OwningConnection.SentBytes(state.PacketBufferPointer.Position);
                bool isUDP = (state.Flags & PacketFlags.UDP) != 0;
                //Log.LogMsg("==>#### Async SEND Op Completed - #" + ((SockState)args.UserToken).ID.ToString() + "#");
                if (args.SocketError == SocketError.Success)
                {
                    //// Log.LogMsg("Testy 14");
                    state.PacketBufferPointer.Advance(args.BytesTransferred);
                    if (state.PacketBufferPointer.Position >= state.PacketBuffer.Length)
                    {
                        OwningConnection.PacketSent();
                        // Done sending packet.
                        state.Reset();
                        //Log.LogMsg("==>Done sending packet. Sent " + state.PacketBufferPointer.Position.ToString() + " bytes.");

                        // done sending packet, see if we have anything in the queue ready to go
                        bool             more  = false;
                        Queue <NetQItem> sendQ = isUDP ? m_SendQueueUDP : m_SendQueue;
                        lock (sendQ)
                        {
                            if (sendQ.Count > 0)
                            {
                                NetQItem itm = sendQ.Dequeue();
                                state.PacketBuffer = itm.Data;
                                state.Flags        = itm.Flags;
                                more = true;
                            }
                            else
                            {
                                // release the sending lock
                                if (isUDP)
                                {
                                    //Log.LogMsg("UDP send queue emptied.");
                                    Interlocked.Exchange(ref m_SendingUDP, 0);
                                }
                                else
                                {
                                    //Log.LogMsg("TCP send queue emptied.");
                                    Interlocked.Exchange(ref m_Sending, 0);
                                }
                            }
                        }

                        if (more)
                        {
                            //// Log.LogMsg("Testy 15");
                            SendBuffer(args, state, isUDP);
                        }

                        return;
                    }
                    else
                    {
                        //// Log.LogMsg("Testy 16");
                        // not done sending.  send again.
                        //Log.LogMsg("==>Continuing send.  " + state.PacketBufferPointer.Position.ToString() + " / " + state.PacketBuffer.Length.ToString() + " sent so far.");
                        SendBuffer(args, state, isUDP);
                    }
                }
                else
                {
                    //If we are in this else-statement, there was a socket error.
                    OwningConnection.KillConnection("Error sending packet. " + args.SocketError.ToString());
                }
            }
            catch (Exception ex)
            {
                //// Log.LogMsg("Testy 17");
                Log.LogMsg("Failed to ProcessSend. " + ex.Message);
                OwningConnection.KillConnection("Send error. " + ex.Message);
            }
        }
Пример #4
0
        /// <summary>
        /// We dont usually send everything all at once in the buffer.  This method sends what's in the buffer out in a piece by piece fashion.
        /// </summary>
        private void SendBuffer(SocketAsyncEventArgs args, SockState state, bool isUDP)
        {
            try
            {
#if !SILVERLIGHT
                //Log.LogMsg("==>$$$$ Async SEND op started - #" + ((SockState)args.UserToken).ID.ToString() + "#");
                if (!OwningConnection.BlockingMode)
#endif
                {
                    int toSend = state.PacketBuffer.Length - state.PacketBufferPointer.Position;
                    if (toSend > state.BufferBlockLength)
                    {
                        toSend = state.BufferBlockLength;
                    }

                    args.SetBuffer(state.BufferBlockOffset, toSend);
                    Util.Copy(state.PacketBuffer, state.PacketBufferPointer.Position, args.Buffer, state.BufferBlockOffset, toSend);

                    Socket socket = OwningConnection.MyTCPSocket;
#if !SILVERLIGHT
                    if (isUDP)
                    {
                        socket = OwningConnection.MyUDPSocket;
                        //Log.LogMsg("UDP Send Target = " + SendTarget.ToString());
                        args.RemoteEndPoint = OwningConnection.UDPSendTarget;
                        if (!socket.SendToAsync(args))
                        {
                            //// Log.LogMsg("Testy 7");
                            OnSendResolved(args, state);
                        }
                    }
                    else
#endif
                    {
                        if (!socket.SendAsync(args))
                        {
                            //// Log.LogMsg("Testy 8");
                            OnSendResolved(args, state);
                        }
                    }
                }
#if !SILVERLIGHT
                else
                {
                    if (isUDP)
                    {
                        //// Log.LogMsg("Testy 9");
                        OwningConnection.MyUDPSocket.SendTo(state.PacketBuffer, OwningConnection.UDPSendTarget);
                    }
                    else
                    {
                        //// Log.LogMsg("Testy 10");
                        OwningConnection.MyTCPSocket.Blocking = true;
                        OwningConnection.MyTCPSocket.Send(state.PacketBuffer);
                    }

                    //// Log.LogMsg("Testy 11");
                    OwningConnection.SentBytes(state.PacketBuffer);
                    OwningConnection.PacketSent();
                }
#endif
            }
            catch (Exception e)
            {
                //// Log.LogMsg("Testy 12");
                Log.LogMsg("Error SendBuffer. " + e.Message);
                OwningConnection.KillConnection("Send error. " + e.Message);
            }
        }