Exemplo n.º 1
0
 /// <summary>
 ///  Метод асинхронного получения сообщений
 /// </summary>
 /// <param name="result"></param>
 private void ReceiveCallback(IAsyncResult result)
 {
     if (ServerSocket != null && IsListeningStatus)
     {
         try
         {
             EndPoint point_remote = (EndPoint)result.AsyncState;
             int      read_size    = ServerSocket.EndReceiveFrom(result, ref point_remote);
             if (read_size > 0)
             {
                 byte[] receive_data = BufferCollector.Resize(Buffer, read_size);
                 CallReceive(point_remote, receive_data);
             }
         }
         catch (Exception ex)
         {
             CallErrorServer(ServerErrorType.ReceiveDataError, ex.Message);
         }
         finally
         {
             Buffer = new byte[SizeBuffer];
             EndPoint localEndPoint = new IPEndPoint(IPAddress.Any, Port);
             ServerSocket.BeginReceiveFrom(Buffer, 0, Buffer.Length, SocketFlags.None, ref localEndPoint,
                                           new AsyncCallback(ReceiveCallback), localEndPoint);
         }
     }
     else
     {
         Stop();
     }
 }
		public void Receive()
		{
			try
			{
				var state = new ControlState()
				{
					CurrentSocket = ServerSocket,
					BufferSize = MaxBufferSize,
					Buffer = new byte[MaxBufferSize],
					StreamBuffer = new MemoryStream()
				};

				var endpoint = new IPEndPoint(IPAddress.Any, 0) as EndPoint;

				ServerSocket.BeginReceiveFrom(state.Buffer, 0, MaxBufferSize, SocketFlags.None, ref endpoint,
					OnReceiveFromCallback, state);
				OnReportingStatus(StatusCode.Info, "Started receiving bytes via UDP socket");
			}
			catch (ObjectDisposedException)
			{
			}
			catch (SocketException socketException)
			{
				OnCaughtException(socketException, EventCode.Receive);
			}
			catch (Exception e)
			{
				OnCaughtException(e, EventCode.Other);
			}
		}
Exemplo n.º 3
0
 public void Receive()
 {
     try
     {
         var state = new ControlState
         {
             CurrentSocket = ServerSocket,
             BufferSize    = MaxBufferSize,
             Buffer        = new byte[MaxBufferSize],
             StreamBuffer  = new MemoryStream()
         };
         var endPoint = new IPEndPoint(IPAddress.Any, 0) as EndPoint;
         ServerSocket.BeginReceiveFrom(state.Buffer, 0, state.Buffer.Length, 0, ref endPoint,
                                       OnReceiveFromCallback, state);
         OnReportingStatus(StatusCode.Info,
                           $"Stared receiving {(_acceptBroadcast ? "broadcast" : "multicast")} bytes via UDP socket");
     }
     catch (ObjectDisposedException)
     {
     }
     catch (SocketException socketException)
     {
         OnCaughtException(socketException, EventCode.Receive);
     }
     catch (Exception exception)
     {
         OnCaughtException(exception, EventCode.Other);
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Запуск сервера
 /// </summary>
 /// <param name="port">порт сервера для прослушивания</param>
 public void Start(int port)
 {
     Port = port;
     try
     {
         if (SetupServerSocket())
         {
             EndPoint localEndPoint = new IPEndPoint(IPAddress.Any, Port);
             Buffer            = new byte[SizeBuffer];
             IsListeningStatus = true;
             CallStatus(ServerStatus.Start);
             ServerSocket.BeginReceiveFrom(Buffer, 0, Buffer.Length, SocketFlags.None, ref localEndPoint, new AsyncCallback(ReceiveCallback), localEndPoint);
         }
         else
         {
             CallErrorServer(ServerErrorType.StartListenError, "Failed to create socket");
         }
     }
     catch (Exception exс)
     {
         CallErrorServer(ServerErrorType.StartListenError, exс.Message);
     }
 }