Beispiel #1
0
        public bool Flush(ServerSock socket)
        {
            try
            {
                if (socket == null || !socket.active)
                {
                    return(false);
                }

                if (buffers[socket.whoAmI].Count < 1)
                {
                    return(false);
                }

                byte[] buff = buffers[socket.whoAmI].GetBytes(BytesPerUpdate);
                if (buff == null)
                {
                    return(false);
                }

                if (SendBytes(socket, buff))
                {
                    buffers[socket.whoAmI].Pop(buff.Length);
                    return(true);
                }
            }
            catch (Exception e)
            {
                Log.ConsoleError(e.ToString());
            }
            return(false);
        }
Beispiel #2
0
 public bool SendBytes(ServerSock socket, byte[] buffer, int offset, int count)
 {
     try
     {
         if (socket.tcpClient.Client != null && socket.tcpClient.Client.Poll(0, SelectMode.SelectWrite))
         {
             if (Main.runningMono)
             {
                 socket.networkStream.Write(buffer, offset, count);
             }
             else
             {
                 socket.tcpClient.Client.Send(buffer, offset, count, SocketFlags.None);
             }
             return(true);
         }
     }
     catch (ObjectDisposedException)
     {
     }
     catch (SocketException)
     {
     }
     catch (IOException)
     {
     }
     return(false);
 }
Beispiel #3
0
 public static void OnSocketReset(ServerSock socket)
 {
     if (ServerHooks.SocketReset != null)
     {
         ServerHooks.SocketReset(socket);
     }
 }
Beispiel #4
0
        internal void InvokeServerSocketReset(ServerSock socket)
        {
            SocketResetEventArgs args = new SocketResetEventArgs
            {
                Socket = socket
            };

            this.ServerSocketReset.Invoke(args);
        }
Beispiel #5
0
        public bool SendBytes(ServerSock socket, byte[] buffer, int offset, int count)
        {
            try
            {
                if (socket.tcpClient.Client != null && socket.tcpClient.Client.Poll(0, SelectMode.SelectWrite))
                {
                    if (ServerApi.RunningMono && !ServerApi.UseAsyncSocketsInMono)
                    {
                        socket.networkStream.Write(buffer, offset, count);
                    }
                    else
                    {
                        socket.networkStream.BeginWrite(buffer, offset, count, socket.ServerWriteCallBack, socket.networkStream);
                    }
                    return(true);
                }
            }
            catch (ObjectDisposedException e)
            {
                Log.Warn(e.ToString());
            }
            catch (SocketException e)
            {
                switch ((uint)e.ErrorCode)
                {
                case 0x80004005:
                case 10053:
                    break;

                default:
                    Log.Warn(e.ToString());
                    break;
                }
            }
            catch (IOException e)
            {
                if (e.InnerException is SocketException)
                {
                    switch (((SocketException)e.InnerException).SocketErrorCode)
                    {
                    case SocketError.Shutdown:
                    case SocketError.ConnectionReset:
                        break;

                    default:
                        Log.Warn(e.ToString());
                        break;
                    }
                }
                else
                {
                    Log.Warn(e.ToString());
                }
            }
            return(false);
        }
Beispiel #6
0
        /// <summary>
        /// Send bytes to client using packetbuffering if available
        /// </summary>
        /// <param name="client">socket to send to</param>
        /// <param name="bytes">bytes to send</param>
        /// <returns>False on exception</returns>
        public static bool SendBytes(ServerSock client, byte[] bytes)
        {
            if (PacketBuffer != null)
            {
                PacketBuffer.BufferBytes(client, bytes);
                return(true);
            }

            return(SendBytesBufferless(client, bytes));
        }
Beispiel #7
0
        public static bool OnSendBytes(ServerSock socket, byte[] buffer, int offset, int count)
        {
            if (NetHooks.SendBytes == null)
            {
                return(false);
            }
            HandledEventArgs handledEventArgs = new HandledEventArgs();

            NetHooks.SendBytes(socket, buffer, offset, count, handledEventArgs);
            return(handledEventArgs.Handled);
        }
Beispiel #8
0
        public static bool OnSendBytes(ServerSock socket, byte[] buffer, int offset, int count)
        {
            if (SendBytes == null)
            {
                return(false);
            }

            var args = new HandledEventArgs();

            SendBytes(socket, buffer, offset, count, args);
            return(args.Handled);
        }
Beispiel #9
0
        internal bool InvokeNetSendBytes(ServerSock socket, byte[] buffer, int offset, int count)
        {
            SendBytesEventArgs args = new SendBytesEventArgs
            {
                Socket = socket,
                Buffer = buffer,
                Offset = offset,
                Count  = count
            };

            this.NetSendBytes.Invoke(args);
            return(args.Handled);
        }
Beispiel #10
0
 /// <summary>
 /// Send bytes to a client ignoring the packet buffer
 /// </summary>
 /// <param name="client">socket to send to</param>
 /// <param name="bytes">bytes to send</param>
 /// <returns>False on exception</returns>
 public static bool SendBytesBufferless(ServerSock client, byte[] bytes)
 {
     try
     {
         if (client.tcpClient.Connected)
         {
             client.networkStream.Write(bytes, 0, bytes.Length);
         }
         return(true);
     }
     catch (Exception ex)
     {
         Log.Warn("This is a normal exception");
         Log.Warn(ex.ToString());
     }
     return(false);
 }
Beispiel #11
0
        public void BufferBytes(ServerSock socket, byte[] buffer, int offset, int count)
        {
            lock (buffers[socket.whoAmI])
            {
#if DEBUG_NET
                int size = (count - offset);
                var pt   = buffer[offset + 4];

                Packets[pt]++;
                Bytes[pt]      += size;
                Compressed[pt] += Compress(buffer, offset, count);
#endif
                using (var ms = new MemoryStream(buffer, offset, count))
                {
                    buffers[socket.whoAmI].AddRange(ms.ToArray());
                }
            }
        }
Beispiel #12
0
        public bool SendBytes(ServerSock socket, byte[] buffer, int offset, int count)
        {
            try
            {
                if (socket.tcpClient.Client != null && socket.tcpClient.Client.Poll(0, SelectMode.SelectWrite))
                {
                    if (Main.runningMono)
                    {
                        socket.networkStream.Write(buffer, offset, count);
                    }
                    else
                    {
                        socket.tcpClient.Client.Send(buffer, offset, count, SocketFlags.None);
                    }
                    return(true);
                }
            }
            catch (ObjectDisposedException e)
            {
                Log.Warn(e.ToString());
            }
            catch (SocketException e)
            {
                switch ((uint)e.ErrorCode)
                {
                case 0x80004005:
                case 10053:
                    break;

                default:
                    Log.Warn(e.ToString());
                    break;
                }
            }
            catch (IOException e)
            {
                Log.Warn(e.ToString());
            }
            return(false);
        }
Beispiel #13
0
        public void BufferBytes(ServerSock socket, byte[] buffer, int offset, int count)
        {
            lock (buffers[socket.whoAmI])
            {
#if DEBUG_NET
                int size = (count - offset);
                var pt   = buffer[offset + 4];

                Packets[pt]++;
                Bytes[pt]      += size;
                Compressed[pt] += Compress(buffer, offset, count);
#endif
                using (var ms = new MemoryStream(buffer, offset, count))
                {
                    buffers[socket.whoAmI].AddRange(ms.ToArray());
                }

                if (TShock.Config.EnableMaxBytesInBuffer && buffers[socket.whoAmI].Count > TShock.Config.MaxBytesInBuffer)
                {
                    buffers[socket.whoAmI].Clear();
                    socket.kill = true;
                }
            }
        }
 public void setSocket(ServerSock Socket)
 {
     socket = Socket;
 }
Beispiel #15
0
 public void BufferBytes(ServerSock socket, byte[] buffer)
 {
     BufferBytes(socket, buffer, 0, buffer.Length);
 }
Beispiel #16
0
 public bool SendBytes(ServerSock socket, byte[] buffer)
 {
     return(SendBytes(socket, buffer, 0, buffer.Length));
 }
Beispiel #17
0
 void ServerHooks_SocketReset(ServerSock socket)
 {
     buffers[socket.whoAmI] = new PacketBuffer();
 }
Beispiel #18
0
 void ServerHooks_SendBytes(ServerSock socket, byte[] buffer, int offset, int count, HandledEventArgs e)
 {
     e.Handled = true;
     BufferBytes(socket, buffer, offset, count);
 }