コード例 #1
0
        protected void Loop()
        {
            while (IsAlive)// && IsServerAlive)
            {
                try
                {
                    ReceiveDone.Reset();

                    ByteBuffer buffer = new ByteBuffer();

                    Socket.BeginReceive(buffer.Array, buffer.Position, buffer.Capacity, SocketFlags.None, new AsyncCallback(OnReceive), buffer);

                    ReceiveDone.WaitOne();
                }

                catch (Exception e)
                {
                    Log.SkipLine();
                    Log.Error(e);
                    Log.SkipLine();
                    throw;
                }
            }
            Dispose();

            StopServer();
        }
コード例 #2
0
        public int Read(Byte[] Data)
        {
            ReadSize = 0;

            SendSocketDataAvailableNotification = false;
            if (IsConnected && (_SocketClient.Available > 0))
            {
                try
                {
#if ASYNC_RECEIVE
                    ReceiveDone.Reset();
                    int ByteToRead = Math.Min(Data.Length, _SocketClient.Available);
                    _SocketClient.BeginReceive(Data, 0, ByteToRead, SocketFlags.None, new AsyncCallback(ReadCallBack), _SocketClient);
                    ReceiveDone.WaitOne();
#else
                    ReadSize = _SocketClient.Receive(Data);
#endif
                }
#if DEBUG_SOCKET
                catch (Exception e)
                {
                    AccessoryLibrary.ShowException(e);
                    ReceiveDone.Set();
                    ReadSize = -1;
                }
#else
                catch (Exception ex)
                {
                    ReceiveDone.Set();
                    ReadSize = -1;
                    logger.Error(ex.ToString() + "\n");
                }
#endif
            }

            /*
             * in the calling procedure
             *
             * After calling Read()
             * ResetDataAvailableNotification() must be called again
             * to enable data notification
             *
             */

            return(ReadSize);
        }
コード例 #3
0
        protected ClientHandler(Socket socket, string title = "Client", params object[] args)
        {
            Title = title;

            Log.Inform("Preparing {0} ...", Title.ToLower());

            Socket         = socket;
            Cryptograph    = new TCryptograph();
            ReceivalBuffer = new ByteBuffer()
            {
                Limit = 0
            };
            IsAlive = true;

            Prepare(args);

            Log.SkipLine();
            Log.Success(string.Format("{0} connected from {1}.", Title, RemoteEndPoint.Address));
            Log.SkipLine();

            Initialize();

            Register();

            while (IsAlive && IsServerAlive)
            {
                ReceiveDone.Reset();

                try
                {
                    Socket.BeginReceive(ReceivalBuffer.Array, ReceivalBuffer.Limit, ReceivalBuffer.Capacity - ReceivalBuffer.Limit, SocketFlags.None, new AsyncCallback(OnReceive), null);
                }
                catch (Exception e)
                {
                    Log.Error(e);
                    Stop();
                }

                ReceiveDone.WaitOne();
            }

            Dispose();
        }
コード例 #4
0
ファイル: server.cs プロジェクト: bumbarasx/chat
 public StateObject(Socket socket)
 {
     WorkSocket = socket;
     KeysDone.Reset();
     ReceiveDone.Reset();
 }