Exemplo n.º 1
0
        private Socket SendingSocketCreator(IPEndPoint target)
        {
            var s = new Socket(target.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                s.Connect(target);
                // Prep the socket so it will reset on close and won't Nagle
                s.LingerState = new LingerOption(true, 0);
                s.NoDelay     = true;
                WriteConnectionPreemble(s, Constants.SiloDirectConnectionId); // Identifies this client as a direct silo-to-silo socket
                // Start an asynch receive off of the socket to detect closure
                var foo = new byte[4];
                s.BeginReceive(foo, 0, 1, SocketFlags.None, ReceiveCallback,
                               new Tuple <Socket, IPEndPoint, SocketManager>(s, target, this));
                NetworkingStatisticsGroup.OnOpenedSendingSocket();
            }
            catch (Exception)
            {
                try
                {
                    s.Close();
                }
                catch (Exception)
                {
                    // ignore
                }
                throw;
            }
            return(s);
        }
Exemplo n.º 2
0
        private Socket SendingSocketCreator(IPEndPoint target)
        {
            var s = new Socket(target.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                s.EnableFastpath();
                Connect(s, target, connectionTimeout);
                // Prep the socket so it will reset on close and won't Nagle
                s.LingerState = new LingerOption(true, 0);
                s.NoDelay     = true;
                WriteConnectionPreamble(s, Constants.SiloDirectConnectionId); // Identifies this client as a direct silo-to-silo socket
                // Start an asynch receive off of the socket to detect closure
                var receiveAsyncEventArgs = new SocketAsyncEventArgs
                {
                    BufferList = new List <ArraySegment <byte> > {
                        new ArraySegment <byte>(new byte[4])
                    },
                    UserToken = new Tuple <Socket, IPEndPoint, SocketManager>(s, target, this)
                };
                receiveAsyncEventArgs.Completed += ReceiveCallback;
                bool receiveCompleted = s.ReceiveAsync(receiveAsyncEventArgs);
                NetworkingStatisticsGroup.OnOpenedSendingSocket();
                if (!receiveCompleted)
                {
                    ReceiveCallback(this, receiveAsyncEventArgs);
                }
            }
            catch (Exception)
            {
                try
                {
                    s.Dispose();
                }
                catch (Exception)
                {
                    // ignore
                }
                throw;
            }
            return(s);
        }