Exemple #1
0
        private void ServerThread(object data)
        {
            ServerStartInfo info          = data as ServerStartInfo;
            NetworkStream   networkStream = info.ClientSocket.GetStream();

            while (!serverClosed)
            {
                try
                {
                    byte[] bytesFrom = new byte[1048576];
                    networkStream.Read(bytesFrom, 0, info.ClientSocket.ReceiveBufferSize);
                    if (bytesFrom.Length > 0)
                    {
                        OnDataRecieved?.Invoke(this, new DataRecievedEventArgs(bytesFrom, info.ClientSocket.ReceiveBufferSize, (byte)StringTerminator));
                    }
                }
                catch (System.IO.IOException)
                {
                    info.ClientSocket.Close();
                    return;
                }
                catch (Exception ex)
                {
                    info.ClientSocket.Close();
                    StopListening(ex.Message + Environment.NewLine + ex.StackTrace);
                    return;
                }
            }
        }
Exemple #2
0
        private void ServerThread_DoWork(object sender, DoWorkEventArgs e)
        {
            ServerStartInfo info     = e.Argument as ServerStartInfo;
            int             requests = 0;

            if (serverThread.CancellationPending)
            {
                e.Cancel = true;
                e.Result = new ServerClosedResult("Canceled", info);
                return;
            }

            while (true)
            {
                if (serverThread.CancellationPending)
                {
                    e.Cancel = true;
                    e.Result = new ServerClosedResult("Canceled", info);
                    return;
                }
                try
                {
                    NetworkStream networkStream  = info.ClientSocket.GetStream();
                    byte[]        bytesToRecieve = new byte[sizeof(int) + 2];

                    if (serverThread.CancellationPending)
                    {
                        e.Cancel = true;
                        e.Result = new ServerClosedResult("Canceled", info);
                        return;
                    }
                    networkStream.Read(bytesToRecieve, 0, sizeof(int) + 2);

                    //That mean this comes from reply
                    if (bytesToRecieve[2] == 'o' && bytesToRecieve[3] == 'k' && bytesToRecieve[4] == 0)
                    {
                        if (replyBuffer != null)
                        {
                            networkStream.Write(replyBuffer, 0, replyBuffer.Length);
                            replyBuffer = null;
                        }
                        else
                        {
                            #pragma warning disable
                            throw new ExecutionEngineException("An error while getting the reply buffer. This may be due to the sending of 'ok' over the network when a message length was expected");
                            #pragma warning restore
                        }
                    }
                    else
                    {
                        if (bytesToRecieve[0] == 0)
                        {
                            continue;
                        }
                        networkStream.Write(new byte[] { (byte)'o', (byte)'k' }, 0, 2);

                        int byt = BitConverter.ToInt32(new byte[] { bytesToRecieve[2], bytesToRecieve[3], bytesToRecieve[4], bytesToRecieve[5] }, 0);
                        System.Diagnostics.Debug.WriteLine(byt);
                        byte[] bytesFrom = new byte[byt];
                        networkStream.Read(bytesFrom, 0, byt);

                        if (serverThread.CancellationPending)
                        {
                            e.Cancel = true;
                            e.Result = new ServerClosedResult("Canceled", info);
                            return;
                        }
                        serverThread.ReportProgress(requests++, new ServerThreadState(bytesFrom, info.ClientSocket.ReceiveBufferSize, ServerState.Running));

                        if (serverThread.CancellationPending)
                        {
                            e.Cancel = true;
                            e.Result = new ServerClosedResult("Canceled", info);
                            return;
                        }
                    }
                    networkStream.Flush();
                }
                catch (Exception ex)
                {
                    info.ClientSocket.Close();
                    info.ServerSocket.Stop();
                    e.Result = new ServerClosedResult("An exception occured in the server thread", ex, info);

                    e.Cancel = true;
                    return;
                }
            }
        }
Exemple #3
0
 public ServerClosedResult(String message, ServerStartInfo info) : this(info)
 {
     this.Message = message;
 }
Exemple #4
0
 public ServerClosedResult(ServerStartInfo info) : base(info.ServerSocket, info.ClientSocket)
 {
 }
Exemple #5
0
 public ServerStartInfo(TcpListener serverSocket, TcpClient clientSocket)
 {
     this.ServerSocket = serverSocket;
     this.ClientSocket = clientSocket;
     Instance          = this;
 }
Exemple #6
0
 public ServerClosedResult(String message, Exception exception, ServerStartInfo info) : this(info)
 {
     this.Message        = message;
     this.InnerException = exception;
 }